Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ship py.typed to allow type checking #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 .exceptions 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
2 changes: 1 addition & 1 deletion tests/test_coerce_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from fiobank import coerce_date
from fiobank.utils import coerce_date


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sanitize_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from fiobank import sanitize_value
from fiobank.utils import sanitize_value


@pytest.mark.parametrize(
Expand Down
Loading