Skip to content

Commit

Permalink
feat: ship py.typed to allow type checking
Browse files Browse the repository at this point in the history
The py.typed needs to be present in the module to make type checking
work (see PEP 561). In order to make it possible, the module can no
longer be a single file, but needs to be installed in a directory.
  • Loading branch information
nijel committed Jan 9, 2025
1 parent a0c11ed commit 82f21d1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/fiobank/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .exceptions import ThrottlingError
from .fiobank import FioBank


__all__ = ("FioBank", "ThrottlingError")
8 changes: 8 additions & 0 deletions src/fiobank/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations


class ThrottlingError(Exception):
"""Throttling error raised when the API is being used too fast."""

def __str__(self) -> str:
return "Token can be used only once per 30s"
36 changes: 2 additions & 34 deletions fiobank.py → src/fiobank/fiobank.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from collections.abc import Generator
from datetime import date, datetime
from decimal import Decimal
from typing import Any, Callable

import requests
from tenacity import (
Expand All @@ -15,39 +14,8 @@
wait_random_exponential,
)


__all__ = ("FioBank", "ThrottlingError")


def coerce_amount(value: int | float) -> Decimal:
if isinstance(value, int):
return Decimal(value)
if isinstance(value, float):
return Decimal(str(value))
raise ValueError(value)


def coerce_date(value: datetime | date | str) -> date:
if isinstance(value, datetime):
return value.date()
if isinstance(value, date):
return value
return datetime.strptime(value[:10], "%Y-%m-%d").date()


def sanitize_value(value: Any, convert: Callable | None = None) -> Any:
if isinstance(value, str):
value = value.strip() or None
if convert and value is not None:
return convert(value)
return value


class ThrottlingError(Exception):
"""Throttling error raised when the API is being used too fast."""

def __str__(self) -> str:
return "Token can be used only once per 30s"
from .excetpions import ThrottlingError
from .utils import coerce_date, sanitize_value


class FioBank:
Expand Down
Empty file added src/fiobank/py.typed
Empty file.
29 changes: 29 additions & 0 deletions src/fiobank/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from datetime import date, datetime
from decimal import Decimal
from typing import Any, Callable


def coerce_amount(value: int | float) -> Decimal:
if isinstance(value, int):
return Decimal(value)
if isinstance(value, float):
return Decimal(str(value))
raise ValueError(value)


def coerce_date(value: datetime | date | str) -> date:
if isinstance(value, datetime):
return value.date()
if isinstance(value, date):
return value
return datetime.strptime(value[:10], "%Y-%m-%d").date()


def sanitize_value(value: Any, convert: Callable | None = None) -> Any:
if isinstance(value, str):
value = value.strip() or None
if convert and value is not None:
return convert(value)
return value

0 comments on commit 82f21d1

Please sign in to comment.