-
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 |
|---|---|---|
| @@ -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"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix: await the coroutine so the DeprecationWarning is actually emitted (matches CI failure).
+import asyncio
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, patch, AsyncMock
@@
- client.derivative_stream_api.stream_positions = MagicMock()
+ client.derivative_stream_api.stream_positions = AsyncMock()
@@
- client.listen_derivative_positions_updates(
- callback=mock_callback, market_ids=["market_1"], subaccount_ids=["subaccount_1"]
- )
+ asyncio.run(
+ client.listen_derivative_positions_updates(
+ callback=mock_callback, market_ids=["market_1"], subaccount_ids=["subaccount_1"]
+ )
+ )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Find the deprecation warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| deprecation_warnings = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for w in warning_list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if issubclass(w.category, DeprecationWarning) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| and "This method is deprecated. Use listen_derivative_positions_v2_updates instead" in str(w.message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Should have exactly one warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(deprecation_warnings) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| warning = deprecation_warnings[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Check warning message contains migration advice | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert "Use listen_derivative_positions_v2_updates 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_indexer_client_deprecation_warnings.py" in warning.filename | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # 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"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Assert the awaited call on AsyncMock for accuracy. Since - 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
warnfunction. The code useswarn()but there's no corresponding import statement visible. This will cause a NameError at runtime.