Skip to content

Commit 98cd1f3

Browse files
authored
Closes #43: Add type hints in the code base (#108)
Introduce typing across the whole code base. Use `ty` to perform type checks. Fix a reference issue when passing the token from `refresh_authentication()`.
1 parent b9695be commit 98cd1f3

File tree

14 files changed

+585
-166
lines changed

14 files changed

+585
-166
lines changed

.github/workflows/publish.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ jobs:
1616
- name: Set up Python
1717
uses: actions/setup-python@v6
1818
with:
19-
python-version: "3.9"
19+
python-version: "3.10"
2020

21-
- name: Install Poetry
22-
run: pipx install poetry
23-
24-
- name: Set PyPI secret
25-
run: poetry config pypi-token.pypi ${{ secrets.PYPI_API_TOKEN }}
21+
- name: Install uv
22+
uses: astral-sh/setup-uv@v7
2623

2724
- name: Build package
28-
run: poetry build
25+
run: uv build
2926

3027
- name: List build
3128
run: ls -la dist/
3229

3330
- name: Publish package in PyPI
34-
run: poetry publish
31+
run: uv publish --token ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/tests.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ jobs:
2727
with:
2828
python-version: ${{ matrix.python }}
2929

30-
- name: Install Poetry
31-
run: pipx install poetry
30+
- name: Install uv
31+
uses: astral-sh/setup-uv@v7
3232

33-
- name: Validate pyproject.toml and poetry.lock
34-
run: poetry check
33+
- name: Validate pyproject.toml and uv.lock
34+
run: uv lock --locked --offline --no-cache --check
3535

3636
- name: Install dependencies
37-
run: poetry install --extras=dev
37+
run: uv sync --locked --all-groups
3838

3939
- name: Run format and lint
4040
run: |
41-
poetry run ruff format --check --diff .
42-
poetry run ruff check --diff .
41+
uv run ruff format --check --diff .
42+
uv run ruff check --diff .
43+
44+
- name: Run type checking
45+
run: uv run ty check .
4346

4447
- name: Run Tests
45-
run: poetry run pytest
48+
run: uv run pytest

poetry.lock

Lines changed: 54 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyixapi/core/api.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
from typing import Any
23

34
import requests
45

@@ -44,14 +45,14 @@ class API(object):
4445

4546
def __init__(
4647
self,
47-
url,
48-
key,
49-
secret,
50-
access_token="",
51-
refresh_token="",
52-
user_agent=f"pyixapi/{__version__}",
53-
proxies=None,
54-
):
48+
url: str,
49+
key: str,
50+
secret: str,
51+
access_token: str = "",
52+
refresh_token: str = "",
53+
user_agent: str = f"pyixapi/{__version__}",
54+
proxies: dict[str, str] | None = None,
55+
) -> None:
5556
self.url = url.rstrip("/")
5657
self.key = key
5758
self.secret = secret
@@ -106,18 +107,18 @@ def version(self) -> int:
106107
return version
107108

108109
@property
109-
def accounts(self):
110+
def accounts(self) -> Endpoint:
110111
return Endpoint(self, "customers" if self.version == 1 else "accounts", model=Account)
111112

112113
@property
113-
def product_offerings(self):
114+
def product_offerings(self) -> Endpoint:
114115
return Endpoint(
115116
self,
116117
"products" if self.version == 1 else "product-offerings",
117118
model=ProductOffering,
118119
)
119120

120-
def authenticate(self):
121+
def authenticate(self) -> Record | None:
121122
"""
122123
Authenticate and generate a pair of tokens.
123124
@@ -147,13 +148,16 @@ def authenticate(self):
147148

148149
return Record(r, self, self.auth)
149150

150-
def refresh_authentication(self):
151+
def refresh_authentication(self) -> Record:
151152
"""
152153
Prolong authentication by refreshing the tokens pair.
153154
"""
155+
if not self.refresh_token:
156+
raise ValueError("No refresh token available to refresh authentication")
157+
154158
r = Request(
155159
cat(self.url, "auth", "refresh"),
156-
token=self.refresh_token.encoded,
160+
token=self.refresh_token,
157161
http_session=self.http_session,
158162
user_agent=self.user_agent,
159163
proxies=self.proxies,
@@ -164,7 +168,7 @@ def refresh_authentication(self):
164168

165169
return Record(r, self, self.auth)
166170

167-
def health(self):
171+
def health(self) -> dict[str, Any]:
168172
"""
169173
Get the health information from IX-API.
170174

pyixapi/core/endpoint.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
15
from pyixapi.core.query import Request, RequestError
26
from pyixapi.core.response import Record, RecordSet
37
from pyixapi.core.util import cat
48

9+
if TYPE_CHECKING:
10+
from pyixapi.core.api import API
11+
512

613
class Endpoint(object):
714
"""
@@ -11,16 +18,16 @@ class Endpoint(object):
1118
object.
1219
"""
1320

14-
def __init__(self, api, name, model=None):
15-
self.return_obj = model if model else Record
21+
def __init__(self, api: API, name: str, model: type[Record] | None = None) -> None:
22+
self.return_obj: type[Record] = model if model else Record
1623
self.api = api
1724
self.url = cat(api.url, name)
1825
self.name = name
1926

20-
def __str__(self):
27+
def __str__(self) -> str:
2128
return self.url
2229

23-
def all(self):
30+
def all(self) -> RecordSet:
2431
"""
2532
Return all objects from an endpoint.
2633
"""
@@ -33,7 +40,7 @@ def all(self):
3340
)
3441
return RecordSet(self, r)
3542

36-
def filter(self, *args, **kwargs):
43+
def filter(self, *args: Any, **kwargs: Any) -> RecordSet:
3744
"""
3845
Query the list of a given endpoint. Also take named arguments that match the
3946
usable filters on a given endpoint.
@@ -48,7 +55,7 @@ def filter(self, *args, **kwargs):
4855
)
4956
return RecordSet(self, r)
5057

51-
def get(self, *args, **kwargs):
58+
def get(self, *args: Any, **kwargs: Any) -> Record | None:
5259
"""
5360
Return a single object from an endpoint.
5461
"""
@@ -89,7 +96,7 @@ def get(self, *args, **kwargs):
8996
else:
9097
raise e
9198

92-
def create(self, *args, **kwargs):
99+
def create(self, *args: Any, **kwargs: Any) -> Record:
93100
"""
94101
Creates an object on an endpoint.
95102

0 commit comments

Comments
 (0)