From 15386f0b4d301c1ab935c3e996eda59151a16899 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sat, 20 Jul 2024 20:21:50 +0200 Subject: [PATCH] Improved type annotations --- fin_depo/__init__.py | 2 ++ fin_depo/data.py | 6 +++--- fin_depo/defi_partisia_blockchain.py | 9 +++++---- fin_depo/investbank_nordnet.py | 8 +++++--- test/test_kraken.py | 3 ++- test/test_kucoin.py | 3 ++- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/fin_depo/__init__.py b/fin_depo/__init__.py index 637e601..a4252f8 100644 --- a/fin_depo/__init__.py +++ b/fin_depo/__init__.py @@ -25,6 +25,8 @@ __all__ = [ 'defi_kucoin', 'defi_partisia_blockchain', 'investbank_nordnet', + '__version__', ] from . import defi_kraken, defi_kucoin, defi_partisia_blockchain, investbank_nordnet +from ._version import __version__ diff --git a/fin_depo/data.py b/fin_depo/data.py index ed291ad..338c0a9 100644 --- a/fin_depo/data.py +++ b/fin_depo/data.py @@ -10,7 +10,7 @@ from fin_defs import Asset @enforce_typing.enforce_types -@dataclasses.dataclass +@dataclasses.dataclass(frozen=True) class Depo(abc.ABC): """A depository tracking some amount of assets. @@ -38,7 +38,7 @@ class Depo(abc.ABC): @enforce_typing.enforce_types -@dataclasses.dataclass +@dataclasses.dataclass(frozen=True) class DepoSingle(Depo): """Base level of depository.""" @@ -52,7 +52,7 @@ class DepoSingle(Depo): @enforce_typing.enforce_types -@dataclasses.dataclass +@dataclasses.dataclass(frozen=True) class DepoGroup(Depo): """Nested depository.""" diff --git a/fin_depo/defi_partisia_blockchain.py b/fin_depo/defi_partisia_blockchain.py index 7a85d6f..1f149b1 100644 --- a/fin_depo/defi_partisia_blockchain.py +++ b/fin_depo/defi_partisia_blockchain.py @@ -7,6 +7,7 @@ import json import logging from collections.abc import Mapping from decimal import Decimal +from typing import Any import fin_defs import requests @@ -55,9 +56,9 @@ class PbcClient: def get_json( self, url: str, - data: Mapping[str, str] = frozendict(), + data: Mapping[str, Any] = frozendict(), method='POST', - ) -> tuple[dict, datetime.datetime]: + ) -> tuple[Any, datetime.datetime]: headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', @@ -80,10 +81,10 @@ class PbcClient: raise Exception(msg) return (json_data, date) - def determine_coins(self) -> list[dict[str, str]]: + def determine_coins(self) -> list[dict[str, Any]]: data: dict = {'path': []} - url = URL_ACCOUNT_PLUGIN_GLOBAL.format( + url: str = URL_ACCOUNT_PLUGIN_GLOBAL.format( hostname=HOSTNAME, shard='', ) diff --git a/fin_depo/investbank_nordnet.py b/fin_depo/investbank_nordnet.py index 3dd12fd..08e2e04 100644 --- a/fin_depo/investbank_nordnet.py +++ b/fin_depo/investbank_nordnet.py @@ -11,7 +11,9 @@ I am grateful for his pioneering work. import datetime import logging +from collections.abc import Mapping from decimal import Decimal +from typing import Any import fin_defs import requests @@ -41,7 +43,7 @@ def asset_from_instrument_json(json) -> fin_defs.Asset | None: return fin_defs.Stock(symbol, fin_defs.EXCHANGES_BY_IDS[exchange_id]) -EMPTY_DICT: dict[str, str | int] = frozendict() +EMPTY_DICT: Mapping[str, str | int] = frozendict() class NordnetDepoFetcher(DepoFetcher): @@ -66,7 +68,7 @@ class NordnetDepoFetcher(DepoFetcher): self.password: str = self.assert_param('password', str, password) self.is_logged_in = False - def _get_json(self, url: str, params: dict[str, str | int] = EMPTY_DICT) -> dict: + def _get_json(self, url: str, params: Mapping[str, str | int] = EMPTY_DICT) -> Any: if not url.startswith(API_ROOT): msg = f'Given url must be located below API ROOT: {url}' raise ValueError(msg) @@ -80,7 +82,7 @@ class NordnetDepoFetcher(DepoFetcher): response.raise_for_status() return json - def login(self): + def login(self) -> None: """Performs authentication with the login server if not already logged in. Does not need to be manually called; most methods that require this information will do it themselves. diff --git a/test/test_kraken.py b/test/test_kraken.py index eda798b..62a7fc1 100644 --- a/test/test_kraken.py +++ b/test/test_kraken.py @@ -1,7 +1,7 @@ import pytest import requests -from fin_depo import data, defi_kraken +from fin_depo import defi_kraken from . import secrets @@ -10,6 +10,7 @@ needs_secrets = pytest.mark.skipif( reason='Secret kraken_USERNAME required', ) + @needs_secrets def test_get_depo(): session = requests.Session() diff --git a/test/test_kucoin.py b/test/test_kucoin.py index 25b1e2e..2f217d5 100644 --- a/test/test_kucoin.py +++ b/test/test_kucoin.py @@ -1,7 +1,7 @@ import pytest import requests -from fin_depo import data, defi_kucoin +from fin_depo import defi_kucoin from . import secrets @@ -10,6 +10,7 @@ needs_secrets = pytest.mark.skipif( reason='Secret kucoin_USERNAME required', ) + @needs_secrets def test_get_depo(): session = requests.Session()