Skip to content

Commit 0aad6f2

Browse files
authored
Merge pull request #55 from nijel/pytyped
feat: ship py.typed to allow type checking
2 parents a0c11ed + c4fdf98 commit 0aad6f2

7 files changed

+46
-36
lines changed

src/fiobank/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .exceptions import ThrottlingError
2+
from .fiobank import FioBank
3+
4+
5+
__all__ = ("FioBank", "ThrottlingError")

src/fiobank/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
4+
class ThrottlingError(Exception):
5+
"""Throttling error raised when the API is being used too fast."""
6+
7+
def __str__(self) -> str:
8+
return "Token can be used only once per 30s"

fiobank.py src/fiobank/fiobank.py

+2-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from collections.abc import Generator
66
from datetime import date, datetime
77
from decimal import Decimal
8-
from typing import Any, Callable
98

109
import requests
1110
from tenacity import (
@@ -15,39 +14,8 @@
1514
wait_random_exponential,
1615
)
1716

18-
19-
__all__ = ("FioBank", "ThrottlingError")
20-
21-
22-
def coerce_amount(value: int | float) -> Decimal:
23-
if isinstance(value, int):
24-
return Decimal(value)
25-
if isinstance(value, float):
26-
return Decimal(str(value))
27-
raise ValueError(value)
28-
29-
30-
def coerce_date(value: datetime | date | str) -> date:
31-
if isinstance(value, datetime):
32-
return value.date()
33-
if isinstance(value, date):
34-
return value
35-
return datetime.strptime(value[:10], "%Y-%m-%d").date()
36-
37-
38-
def sanitize_value(value: Any, convert: Callable | None = None) -> Any:
39-
if isinstance(value, str):
40-
value = value.strip() or None
41-
if convert and value is not None:
42-
return convert(value)
43-
return value
44-
45-
46-
class ThrottlingError(Exception):
47-
"""Throttling error raised when the API is being used too fast."""
48-
49-
def __str__(self) -> str:
50-
return "Token can be used only once per 30s"
17+
from .exceptions import ThrottlingError
18+
from .utils import coerce_date, sanitize_value
5119

5220

5321
class FioBank:

src/fiobank/py.typed

Whitespace-only changes.

src/fiobank/utils.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
3+
from datetime import date, datetime
4+
from decimal import Decimal
5+
from typing import Any, Callable
6+
7+
8+
def coerce_amount(value: int | float) -> Decimal:
9+
if isinstance(value, int):
10+
return Decimal(value)
11+
if isinstance(value, float):
12+
return Decimal(str(value))
13+
raise ValueError(value)
14+
15+
16+
def coerce_date(value: datetime | date | str) -> date:
17+
if isinstance(value, datetime):
18+
return value.date()
19+
if isinstance(value, date):
20+
return value
21+
return datetime.strptime(value[:10], "%Y-%m-%d").date()
22+
23+
24+
def sanitize_value(value: Any, convert: Callable | None = None) -> Any:
25+
if isinstance(value, str):
26+
value = value.strip() or None
27+
if convert and value is not None:
28+
return convert(value)
29+
return value

tests/test_coerce_date.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from fiobank import coerce_date
5+
from fiobank.utils import coerce_date
66

77

88
@pytest.mark.parametrize(

tests/test_sanitize_value.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from fiobank import sanitize_value
5+
from fiobank.utils import sanitize_value
66

77

88
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)