diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..4d93d50 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,29 @@ +name: Lint + +on: + push: + branches: + - "master" + - "develop" + pull_request: + types: [ready_for_review, synchronize, opened] + +jobs: + flake8: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v7 + - name: Set up Python + uses: actions/setup-python@v7 + with: + python-version: "3.14" + - name: install dependencies + run: | + python3 -m venv venv + . venv/bin/activate + pip install .[test] + - name: run linter + run: | + . venv/bin/activate + make lint diff --git a/client/api/evm.py b/client/api/evm.py index c8189c3..c76aaad 100644 --- a/client/api/evm.py +++ b/client/api/evm.py @@ -5,6 +5,6 @@ class EVM(Resource): def call(self, payload: EvmBodyPartialParams): return self.with_endpoint('evm').request_post('', { - 'jsonrpc': "2.0", + 'jsonrpc': '2.0', **payload, }) diff --git a/client/api/receipts.py b/client/api/receipts.py index 765e39d..811185b 100644 --- a/client/api/receipts.py +++ b/client/api/receipts.py @@ -2,7 +2,12 @@ from client.resource import Resource from client.types import PaginatedResponse, Response -from client.types.receipts import ReceiptContractsQuery, ReceiptQuery, ReceiptResponse, ReceiptsQuery +from client.types.receipts import ( + ReceiptContractsQuery, + ReceiptQuery, + ReceiptResponse, + ReceiptsQuery, +) class Receipts(Resource): @@ -19,7 +24,10 @@ def get( f'receipts/{transaction_hash}', query ) - def contracts(self, query: Optional[ReceiptContractsQuery] = None) -> PaginatedResponse[ReceiptResponse]: + def contracts( + self, + query: Optional[ReceiptContractsQuery] = None, + ) -> PaginatedResponse[ReceiptResponse]: return self.with_endpoint('api').request_get( 'receipts/contracts', query ) diff --git a/client/api/tokens.py b/client/api/tokens.py index 93f5285..01885a0 100644 --- a/client/api/tokens.py +++ b/client/api/tokens.py @@ -21,17 +21,26 @@ class Tokens(Resource): def all(self, query: Optional[TokensQuery] = None) -> PaginatedResponse[TokenResponse]: return self.with_endpoint('api').request_get('tokens', query) - def transfers(self, query: Optional[TokenTransfersQuery] = None) -> TokenPaginatedResponseResults[TokenActionsResponse]: + def transfers( + self, + query: Optional[TokenTransfersQuery] = None, + ) -> TokenPaginatedResponseResults[TokenActionsResponse]: return self.with_endpoint('api').request_get( 'tokens/transfers', query ) - def approvals(self, query: Optional[TokenApprovalsQuery] = None) -> TokenPaginatedResponseResults[TokenActionsResponse]: + def approvals( + self, + query: Optional[TokenApprovalsQuery] = None, + ) -> TokenPaginatedResponseResults[TokenActionsResponse]: return self.with_endpoint('api').request_get( 'tokens/approvals', query ) - def whitelist(self, query: Optional[PaginatedQuery] = None) -> PaginatedResponse[TokenWhitelistResponse]: + def whitelist( + self, + query: Optional[PaginatedQuery] = None, + ) -> PaginatedResponse[TokenWhitelistResponse]: return self.with_endpoint('api').request_get( 'tokens/whitelist', query ) diff --git a/client/api/transactions.py b/client/api/transactions.py index ab6d8d3..2ce8bb6 100644 --- a/client/api/transactions.py +++ b/client/api/transactions.py @@ -14,7 +14,10 @@ class Transactions(Resource): - def all(self, query: Optional[TransactionsQuery] = None) -> PaginatedResponse[TransactionResponse]: + def all( + self, + query: Optional[TransactionsQuery] = None, + ) -> PaginatedResponse[TransactionResponse]: return self.with_endpoint('api').request_get( 'transactions', query ) diff --git a/client/api/wallets.py b/client/api/wallets.py index 9f1e648..1045f78 100644 --- a/client/api/wallets.py +++ b/client/api/wallets.py @@ -2,9 +2,14 @@ from client.resource import Resource from client.types import PaginatedResponse, Response -from client.types.transactions import TransactionResponse, TransactionsQuery -from client.types.wallets import WalletResponse, WalletTokensForQuery, WalletTokensQuery, WalletsQuery from client.types.tokens import TokenActionsResponse, TokenPaginatedResponseResults +from client.types.transactions import TransactionResponse, TransactionsQuery +from client.types.wallets import ( + WalletResponse, + WalletTokensForQuery, + WalletTokensQuery, + WalletsQuery, +) class Wallets(Resource): @@ -65,7 +70,10 @@ def tokens_for( f'wallets/{address}/tokens', query ) - def tokens(self, query: Optional[WalletTokensQuery] = None) -> TokenPaginatedResponseResults[TokenActionsResponse]: + def tokens( + self, + query: Optional[WalletTokensQuery] = None, + ) -> TokenPaginatedResponseResults[TokenActionsResponse]: return self.with_endpoint('api').request_get( 'wallets/tokens', query ) diff --git a/client/client.py b/client/client.py index 86c8353..492f5b8 100644 --- a/client/client.py +++ b/client/client.py @@ -1,11 +1,10 @@ from typing import Union -from client.api.contracts import Contracts -from client.connection import ClientHosts, Connection + from client.api.api_nodes import ApiNodes from client.api.blockchain import Blockchain from client.api.blocks import Blocks from client.api.commits import Commits -from client.api.validators import Validators +from client.api.contracts import Contracts from client.api.evm import EVM from client.api.node import Node from client.api.peers import Peers @@ -13,8 +12,11 @@ from client.api.rounds import Rounds from client.api.tokens import Tokens from client.api.transactions import Transactions +from client.api.validators import Validators from client.api.votes import Votes from client.api.wallets import Wallets +from client.connection import ClientHosts, Connection + class Client(object): diff --git a/client/connection.py b/client/connection.py index 1fd1f35..6567c2c 100644 --- a/client/connection.py +++ b/client/connection.py @@ -1,6 +1,8 @@ from typing import Literal, Optional, TypedDict, Union + import backoff + import requests from client.exceptions import ArkHTTPException @@ -22,6 +24,7 @@ class ClientHosts(TypedDict): transactions: Optional[str] evm: Optional[str] + class Session(requests.Session): def __init__(self, *args, **kwargs): @@ -44,6 +47,7 @@ def prepare_request(self, request): return super().prepare_request(request) + class Connection(object): session: Session hosts: ClientHosts diff --git a/client/resource.py b/client/resource.py index 2a5afb6..a7754e2 100644 --- a/client/resource.py +++ b/client/resource.py @@ -1,8 +1,10 @@ from typing import Literal + from flatten_dict import flatten from client.connection import Connection + class Resource(object): def __init__(self, connection: Connection): diff --git a/client/types/blocks.py b/client/types/blocks.py index 85192a2..2133e7f 100644 --- a/client/types/blocks.py +++ b/client/types/blocks.py @@ -44,7 +44,8 @@ class BlockResponse(TypedDict, total=False): version: int -# Functional form required: keys contain dots (e.g. 'height.from') which are not valid Python identifiers +# Functional form required: keys contain dots (e.g. 'height.from') which are not +# valid Python identifiers BlocksQuery = TypedDict( 'BlocksQuery', { diff --git a/client/types/evm.py b/client/types/evm.py index 14f170e..3ba0f98 100644 --- a/client/types/evm.py +++ b/client/types/evm.py @@ -3,7 +3,7 @@ PayloadData = dict[str, str | int | float] -BlockParameter = Literal["earliest", "latest", "safe", "finalized", "pending"] +BlockParameter = Literal['earliest', 'latest', 'safe', 'finalized', 'pending'] class EvmBodyPartialParams(TypedDict, total=False): diff --git a/client/types/validators.py b/client/types/validators.py index 59f2abd..4999b06 100644 --- a/client/types/validators.py +++ b/client/types/validators.py @@ -1,7 +1,8 @@ from typing import TypedDict -# Functional form required: keys contain dots (e.g. 'blocks.last.hash') which are not valid Python identifiers +# Functional form required: keys contain dots (e.g. 'blocks.last.hash') which are not +# valid Python identifiers ValidatorsQuery = TypedDict( 'ValidatorsQuery', { diff --git a/client/types/wallets.py b/client/types/wallets.py index 2d29f5b..096c42a 100644 --- a/client/types/wallets.py +++ b/client/types/wallets.py @@ -35,7 +35,8 @@ class WalletResponse(TypedDict): tokenCount: int -# Functional form required: keys contain dots (e.g. 'balance.from') which are not valid Python identifiers +# Functional form required: keys contain dots (e.g. 'balance.from') which are not +# valid Python identifiers WalletsQuery = TypedDict( 'WalletsQuery', { diff --git a/setup.cfg b/setup.cfg index dded69d..412908a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,7 @@ [flake8] max-line-length = 100 application-import-names = client +extend-exclude = .venv,venv,build,dist [tool:pytest] addopts = --verbose -s diff --git a/tests/api/test_api_nodes.py b/tests/api/test_api_nodes.py index c3be92f..8ccdf37 100644 --- a/tests/api/test_api_nodes.py +++ b/tests/api/test_api_nodes.py @@ -1,4 +1,5 @@ import responses + from client import Client diff --git a/tests/api/test_blockchain.py b/tests/api/test_blockchain.py index 345175c..5bb3916 100644 --- a/tests/api/test_blockchain.py +++ b/tests/api/test_blockchain.py @@ -1,4 +1,5 @@ import responses + from client import Client diff --git a/tests/api/test_commits.py b/tests/api/test_commits.py index fe6a884..19d10a4 100644 --- a/tests/api/test_commits.py +++ b/tests/api/test_commits.py @@ -1,4 +1,5 @@ import responses + from client import Client diff --git a/tests/api/test_contracts.py b/tests/api/test_contracts.py index 5aefbae..c173676 100644 --- a/tests/api/test_contracts.py +++ b/tests/api/test_contracts.py @@ -1,4 +1,5 @@ import responses + from client import Client @@ -27,4 +28,6 @@ def test_abi_calls_correct_url(): client = Client('http://127.0.0.1:4002/api') client.contracts.abi('consensus', 'some-address') assert len(responses.calls) == 1 - assert responses.calls[0].request.url == 'http://127.0.0.1:4002/api/contracts/consensus/some-address/abi' + assert responses.calls[0].request.url == ( + 'http://127.0.0.1:4002/api/contracts/consensus/some-address/abi' + ) diff --git a/tests/api/test_evm.py b/tests/api/test_evm.py index a3d1876..11591a4 100644 --- a/tests/api/test_evm.py +++ b/tests/api/test_evm.py @@ -1,4 +1,5 @@ import json + import responses from client import Client @@ -15,7 +16,7 @@ def test_eth_call_methods_correct_url(): client = Client('http://127.0.0.1:4002/evm/api') client.evm.call({ 'method': 'eth_call', - 'params': [[{ 'random': 'data' }], 'latest'], + 'params': [[{'random': 'data'}], 'latest'], 'id': None, }) assert len(responses.calls) == 1 @@ -23,6 +24,6 @@ def test_eth_call_methods_correct_url(): assert json.loads(responses.calls[0].request.body.decode()) == { 'jsonrpc': '2.0', 'method': 'eth_call', - 'params': [[{ 'random': 'data' }], 'latest'], + 'params': [[{'random': 'data'}], 'latest'], 'id': None, } diff --git a/tests/api/test_receipts.py b/tests/api/test_receipts.py index b739b09..4cef9ff 100644 --- a/tests/api/test_receipts.py +++ b/tests/api/test_receipts.py @@ -1,4 +1,5 @@ import responses + from client import Client diff --git a/tests/api/test_rounds.py b/tests/api/test_rounds.py index b8b860b..c7e409e 100644 --- a/tests/api/test_rounds.py +++ b/tests/api/test_rounds.py @@ -1,4 +1,5 @@ import responses + from client import Client diff --git a/tests/test_client.py b/tests/test_client.py index 4543f27..9e33d35 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,21 +1,19 @@ -import pytest - from client.client import Client def test_client(): client = Client('http://127.0.0.1:4002') - assert hasattr(client, 'connection') == True - assert hasattr(client, 'api_nodes') == True - assert hasattr(client, 'blockchain') == True - assert hasattr(client, 'blocks') == True - assert hasattr(client, 'commits') == True - assert hasattr(client, 'evm') == True - assert hasattr(client, 'node') == True - assert hasattr(client, 'peers') == True - assert hasattr(client, 'rounds') == True - assert hasattr(client, 'transactions') == True - assert hasattr(client, 'validators') == True - assert hasattr(client, 'votes') == True - assert hasattr(client, 'wallets') == True + assert hasattr(client, 'connection') + assert hasattr(client, 'api_nodes') + assert hasattr(client, 'blockchain') + assert hasattr(client, 'blocks') + assert hasattr(client, 'commits') + assert hasattr(client, 'evm') + assert hasattr(client, 'node') + assert hasattr(client, 'peers') + assert hasattr(client, 'rounds') + assert hasattr(client, 'transactions') + assert hasattr(client, 'validators') + assert hasattr(client, 'votes') + assert hasattr(client, 'wallets') diff --git a/tests/test_connection.py b/tests/test_connection.py index 26dbf07..f427c56 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -165,8 +165,8 @@ def test_http_methods_call_correct_url_with_params_and_return_correct_response(m def test_session_detects_hostname_correctly(): - session = Session(hostname="test.com") - assert session.hostname == "test.com" + session = Session(hostname='test.com') + assert session.hostname == 'test.com' assert isinstance(session, requests.Session) @@ -185,7 +185,7 @@ def test_session_prepends_hostname_to_url(): status=200 ) - session = Session(hostname="http://test.com") + session = Session(hostname='http://test.com') session.get('spongebob') assert len(responses.calls) == 1 diff --git a/tests/test_resource.py b/tests/test_resource.py index 8b22eec..264d933 100644 --- a/tests/test_resource.py +++ b/tests/test_resource.py @@ -20,4 +20,3 @@ def test_request_post_calls_connection_post_with_correct_params(mocker): resource.request_post('spongebob', data={'one': 'two'}, params={'foo': 'bar'}) post_mock.assert_called_once_with('spongebob', data={'one': 'two'}, params={'foo': 'bar'}) -