Skip to content

Commit

Permalink
Merge pull request #119 from gocardless/stricter-types
Browse files Browse the repository at this point in the history
Stricter type checking
  • Loading branch information
danielroseman authored Aug 5, 2021
2 parents 3685a60 + 78cd52a commit d436c45
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.3 - August 2, 2021
- stricter type checking

## 2.0.2 - July 25, 2021
- add py.typed file to indicate to mypy that the library is fully type annotated.

Expand Down
3 changes: 2 additions & 1 deletion business/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Init."""
# mypy: ignore-errors


def get_version():
def get_version() -> str:
"""Get package version."""
try:
from importlib.metadata import version
Expand Down
17 changes: 9 additions & 8 deletions business/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from threading import RLock
from typing import List, Optional, Union
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union

import yaml
from dateutil.parser import parse as dateutil_parse
Expand All @@ -12,31 +12,32 @@

day_interval = datetime.timedelta(days=1)
INPUT_TYPES = Union[str, datetime.date]
T = TypeVar('T')


class Mutex:
class Mutex(Generic[T]):
"""Helper class for thread-safe locking."""

def __init__(self, obj):
def __init__(self, obj: T) -> None:
"""Initialise reentrant lock."""
super().__init__()
self.__obj = obj
self.lock = RLock()

def __enter__(self):
def __enter__(self) -> T:
"""Acquire lock on entering."""
self.lock.acquire()
return self.__obj

def __exit__(self, *args, **kwargs):
def __exit__(self, *args: Any, **kwargs: Any) -> None:
"""Release lock on exit."""
self.lock.release()


class Calendar:
"""Calendar class."""

_cache = Mutex(dict())
_cache: Mutex[Dict[str, "Calendar"]] = Mutex(dict())

load_paths: List[str] = []

Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(
raise ValueError(f"Extra working dates cannot be on working days: {d}")

@classmethod
def load(cls, calendar_str: str):
def load(cls, calendar_str: str) -> "Calendar":
"""Load a scheme calendar YAML file.
>>> %timeit -n 100 Calendar.load('bacs')
Expand Down Expand Up @@ -105,7 +106,7 @@ def load(cls, calendar_str: str):
)

@classmethod
def load_cache(cls, calendar_str: str):
def load_cache(cls, calendar_str: str) -> "Calendar":
"""Load a scheme calendar YAML file with cache.
>>> %timeit Calendar.load_cache('bacs')
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "business-python"
version = "2.0.2"
version = "2.0.3"
description = "Date calculations based on business calendars."
authors = ["GoCardless <[email protected]>"]
readme = "README.md"
Expand Down Expand Up @@ -53,3 +53,6 @@ target-version = ['py36']
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
strict = true

0 comments on commit d436c45

Please sign in to comment.