-
Notifications
You must be signed in to change notification settings - Fork 34
fix/add_depth_to_legacy_async_client_orderbooks_queries #396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "injective-py" | ||
version = "1.11.0" | ||
version = "1.11.1" | ||
description = "Injective Python SDK, with Exchange API Client" | ||
authors = ["Injective Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import warnings | ||
from unittest.mock import MagicMock, patch | ||
|
||
from pyinjective.core.network import Network | ||
|
||
|
||
class TestAsyncClientDeprecationWarnings: | ||
@patch("pyinjective.async_client.asyncio.get_event_loop") | ||
@patch("pyinjective.async_client.ChainGrpcChainStream") | ||
@patch("pyinjective.async_client.ChainGrpcExchangeApi") | ||
@patch("pyinjective.async_client.ChainGrpcDistributionApi") | ||
@patch("pyinjective.async_client.ChainGrpcAuthZApi") | ||
@patch("pyinjective.async_client.ChainGrpcAuthApi") | ||
@patch("pyinjective.async_client.ChainGrpcBankApi") | ||
@patch("pyinjective.async_client.IndexerClient") | ||
def test_async_client_deprecation_warning(self, *mocks): | ||
"""Test that creating an AsyncClient instance raises a deprecation warning with correct details.""" | ||
# Create a mock network to avoid actual network initialization | ||
mock_network = MagicMock(spec=Network) | ||
mock_network.chain_cookie_assistant = MagicMock() | ||
mock_network.create_chain_grpc_channel = MagicMock() | ||
mock_network.create_chain_stream_grpc_channel = MagicMock() | ||
mock_network.official_tokens_list_url = "https://example.com/tokens.json" | ||
|
||
# Import here to avoid early import issues | ||
from pyinjective.async_client import AsyncClient | ||
|
||
# Capture warnings | ||
with warnings.catch_warnings(record=True) as warning_list: | ||
warnings.simplefilter("always") # Ensure all warnings are captured | ||
|
||
# Create AsyncClient instance - this should trigger the deprecation warning | ||
client = AsyncClient(network=mock_network) | ||
|
||
# Find the AsyncClient deprecation warning | ||
async_client_warnings = [ | ||
w | ||
for w in warning_list | ||
if issubclass(w.category, DeprecationWarning) | ||
and "AsyncClient from pyinjective.async_client is deprecated" in str(w.message) | ||
] | ||
|
||
# Should have exactly one warning | ||
assert len(async_client_warnings) == 1 | ||
|
||
warning = async_client_warnings[0] | ||
# Check warning message contains migration advice | ||
assert "Please use AsyncClient from pyinjective.async_client_v2 instead" in str(warning.message) | ||
# Check warning category | ||
assert warning.category == DeprecationWarning | ||
# Check stacklevel is working correctly (should point to this test file) | ||
assert "test_async_client_deprecation_warnings.py" in warning.filename | ||
|
||
# Verify the client was still created successfully | ||
assert client is not None | ||
assert hasattr(client, "network") | ||
assert client.network == mock_network |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,81 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import warnings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from unittest.mock import MagicMock, patch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from pyinjective.core.network import Network | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class TestIndexerClientDeprecationWarnings: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcSpotStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcPortfolioStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcOracleStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcMetaStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcExplorerStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcDerivativeStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcAuctionStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcAccountStream") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcSpotApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcPortfolioApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcOracleApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcMetaApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcInsuranceApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcExplorerApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcDerivativeApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcAuctionApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@patch("pyinjective.indexer_client.IndexerGrpcAccountApi") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def test_listen_derivative_positions_updates_deprecation_warning(self, *mocks): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"""Test that calling listen_derivative_positions_updates raises a deprecation warning.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Create a mock network to avoid actual network initialization | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_network = MagicMock(spec=Network) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_network.exchange_cookie_assistant = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_network.explorer_cookie_assistant = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_network.create_exchange_grpc_channel = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_network.create_explorer_grpc_channel = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Import here to avoid early import issues | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from pyinjective.indexer_client import IndexerClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Create IndexerClient instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client = IndexerClient(network=mock_network) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Mock the derivative_stream_api.stream_positions method to avoid actual streaming | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client.derivative_stream_api.stream_positions = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Capture warnings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
with warnings.catch_warnings(record=True) as warning_list: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
warnings.simplefilter("always") # Ensure all warnings are captured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Mock callback function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mock_callback = MagicMock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Call the deprecated method - this should trigger the deprecation warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client.listen_derivative_positions_updates( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
callback=mock_callback, market_ids=["market_1"], subaccount_ids=["subaccount_1"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
# Create IndexerClient instance | |
client = IndexerClient(network=mock_network) | |
# Mock the derivative_stream_api.stream_positions method to avoid actual streaming | |
client.derivative_stream_api.stream_positions = MagicMock() | |
# Capture warnings | |
with warnings.catch_warnings(record=True) as warning_list: | |
warnings.simplefilter("always") # Ensure all warnings are captured | |
# Mock callback function | |
mock_callback = MagicMock() | |
# Call the deprecated method - this should trigger the deprecation warning | |
client.listen_derivative_positions_updates( | |
callback=mock_callback, market_ids=["market_1"], subaccount_ids=["subaccount_1"] | |
) | |
import asyncio | |
from unittest.mock import MagicMock, patch, AsyncMock | |
# Create IndexerClient instance | |
client = IndexerClient(network=mock_network) | |
# Mock the derivative_stream_api.stream_positions method to avoid actual streaming | |
client.derivative_stream_api.stream_positions = AsyncMock() | |
# Capture warnings | |
with warnings.catch_warnings(record=True) as warning_list: | |
warnings.simplefilter("always") # Ensure all warnings are captured | |
# Mock callback function | |
mock_callback = MagicMock() | |
# Call the deprecated method - this should trigger the deprecation warning | |
asyncio.run( | |
client.listen_derivative_positions_updates( | |
callback=mock_callback, market_ids=["market_1"], subaccount_ids=["subaccount_1"] | |
) | |
) |
🤖 Prompt for AI Agents
In tests/test_indexer_client_deprecation_warnings.py around lines 37 to 53, the
test calls the async IndexerClient.listen_derivative_positions_updates without
awaiting so the coroutine body (and its DeprecationWarning) never runs; change
the test to await the coroutine (e.g., wrap the call in asyncio.run(...) or use
the test event loop) and make client.derivative_stream_api.stream_positions an
AsyncMock so the awaited call executes correctly and the warning is emitted and
captured.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Assert the awaited call on AsyncMock for accuracy.
Since stream_positions
is awaited, assert the awaited call (not just called).
- client.derivative_stream_api.stream_positions.assert_called_once_with(
+ client.derivative_stream_api.stream_positions.assert_awaited_once_with(
callback=mock_callback,
on_end_callback=None,
on_status_callback=None,
market_ids=["market_1"],
subaccount_ids=["subaccount_1"],
)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
# Verify the underlying method was still called (functionality preserved) | |
client.derivative_stream_api.stream_positions.assert_called_once_with( | |
callback=mock_callback, | |
on_end_callback=None, | |
on_status_callback=None, | |
market_ids=["market_1"], | |
subaccount_ids=["subaccount_1"], | |
) | |
# Verify the underlying method was still called (functionality preserved) | |
client.derivative_stream_api.stream_positions.assert_awaited_once_with( | |
callback=mock_callback, | |
on_end_callback=None, | |
on_status_callback=None, | |
market_ids=["market_1"], | |
subaccount_ids=["subaccount_1"], | |
) |
🤖 Prompt for AI Agents
In tests/test_indexer_client_deprecation_warnings.py around lines 74 to 81, the
test currently uses assert_called_once_with on an AsyncMock for stream_positions
even though the method is awaited; replace the call assertion with the
awaited-call assertion by using the AsyncMock's assert_awaited_once_with and
pass the same keyword args (callback=mock_callback, on_end_callback=None,
on_status_callback=None, market_ids=["market_1"],
subaccount_ids=["subaccount_1"]) so the test verifies the awaited invocation
rather than a plain call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing import statement for the
warn
function. The code useswarn()
but there's no corresponding import statement visible. This will cause a NameError at runtime.Copilot uses AI. Check for mistakes.