Skip to content

Commit

Permalink
feat: check typing with mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
derlin committed Mar 16, 2024
1 parent 0c8a8cd commit 5a69c35
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
lint:
docker run --rm -it -e LINT_FOLDER_PYTHON=keycloak -v $(CURDIR):/app divio/lint /bin/lint ${ARGS} --run=python

mypy:
mypy keycloak

test:
coverage erase
docker compose up -d --wait keycloak
Expand Down
18 changes: 11 additions & 7 deletions keycloak/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def utcnow():
class Connection(ABC):
@abstractmethod
def token(self) -> str:
pass # NOCOV
pass # NOCOV


@frozen
Expand All @@ -44,16 +44,19 @@ def expires_at(self) -> datetime:
def refresh_expires_at(self) -> datetime | None:
if not self.refresh_token:
return None
assert self.refresh_expires_in
return self.created_at + timedelta(seconds=self.refresh_expires_in)

def has_refresh_token(self, _now: Callable[[], datetime] = utcnow) -> bool:
return bool(
self.refresh_token
and _now() < (self.refresh_expires_at - timedelta(seconds=2))
)
if (exp := self.refresh_expires_at) and _now() < (
exp - timedelta(seconds=2)
):
return True
return False

@classmethod
def from_dict(cls, data: dict, now: datetime | None = None):
def from_dict(cls, data: dict, now: datetime | None = None) -> "Token":
assert hasattr(cls, "__slots__")
if now:
data["created_at"] = now
return cls(**{k: v for k, v in data.items() if k in cls.__slots__})
Expand All @@ -78,7 +81,7 @@ def auth_url(self) -> str:

@abstractmethod
def _token_exchange_data(self) -> dict:
pass # NOCOV
pass # NOCOV

def _fetch_token(self) -> None:
now = utcnow()
Expand Down Expand Up @@ -111,6 +114,7 @@ def token(self, _now: Callable[[], datetime] = utcnow) -> str:
):
self._fetch_token()

assert self._token
return self._token.access_token


Expand Down
1 change: 1 addition & 0 deletions keycloak/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class HttpException(Exception):

@classmethod
def from_slumber_exception(cls, ex: SlumberHttpBaseException):
assert hasattr(ex, "response")
return cls(
url=ex.response.request.url,
status_code=ex.response.status_code,
Expand Down
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ dependencies = [

[project.optional-dependencies]
dev = [
"build",
"coverage",
"tox",
"build",
"mypy",
"types-requests",
"types-slumber",
]

test = [
"mock",
"pytest",
"requests-mock",
"pytest-cov",
]

Expand All @@ -67,9 +68,13 @@ ignore = [
[tool.setuptools_scm]
write_to = "keycloak/version.py"


[tool.ruff]
extend = "/presets/ruff.toml"
target-version = "py310" # lowest supported version
extend-ignore = [
"S101", # allow asserts
]


[tool.coverage.report]
Expand Down

0 comments on commit 5a69c35

Please sign in to comment.