Skip to content

Commit

Permalink
Merge pull request #13 from multani/py.typed
Browse files Browse the repository at this point in the history
Mark the library as being typed
  • Loading branch information
joakimnordling authored Sep 19, 2023
2 parents 09b1fc8 + ada4094 commit 6383924
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pyjwt_key_fetcher/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class AsyncKeyFetcher:
def __init__(
self,
valid_issuers: Optional[Iterable] = None,
valid_issuers: Optional[Iterable[str]] = None,
http_client: Optional[HTTPClient] = None,
cache_ttl: int = 3600,
cache_maxsize: int = 32,
Expand Down Expand Up @@ -56,7 +56,7 @@ def get_kid(token: str) -> str:
"""
jwt_headers = jwt.get_unverified_header(token)
try:
kid = jwt_headers["kid"]
kid: str = jwt_headers["kid"]
except KeyError:
raise JWTFormatError("Missing 'kid' in header")
return kid
Expand All @@ -83,7 +83,7 @@ def get_issuer(token: str) -> str:
"""
payload = jwt.decode(token, options={"verify_signature": False})
try:
issuer = payload["iss"]
issuer: str = payload["iss"]
except KeyError:
raise JWTFormatError("Missing 'iss' in payload")

Expand Down
4 changes: 2 additions & 2 deletions pyjwt_key_fetcher/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DefaultHTTPClient(HTTPClient):
A default client implemented using aiohttp.
"""

def __init__(self):
def __init__(self) -> None:
self.session = aiohttp.ClientSession()

async def get_json(self, url: str) -> Dict[str, Any]:
Expand All @@ -46,7 +46,7 @@ async def get_json(self, url: str) -> Dict[str, Any]:

try:
async with self.session.get(url) as resp:
data = await resp.json()
data: Dict[str, Any] = await resp.json()
if resp.status != 200:
raise JWTHTTPFetchError(f"Failed to fetch or decode {url}")
except (aiohttp.ClientError, JSONDecodeError) as e:
Expand Down
9 changes: 4 additions & 5 deletions pyjwt_key_fetcher/key.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import collections.abc
from typing import Any, Dict, Iterator
from typing import Any, Dict, Iterator, Mapping

from jwt import PyJWK


class Key(collections.abc.Mapping):
class Key(Mapping[str, Any]):
"""
Wrapper for the JWT key and algorithm.
"""
Expand Down Expand Up @@ -32,10 +31,10 @@ def __repr__(self) -> str:
f"{self.__kid}>, algorithms={self.algorithms})"
)

def __getitem__(self, item):
def __getitem__(self, item: str) -> Any:
return self.dct.__getitem__(item)

def __iter__(self) -> Iterator:
def __iter__(self) -> Iterator[str]:
return self.dct.__iter__()

def __len__(self) -> int:
Expand Down
8 changes: 5 additions & 3 deletions pyjwt_key_fetcher/provider.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional
from typing import Any, Callable, Dict, Optional
from uuid import uuid4

import aiocache # type: ignore
Expand All @@ -12,7 +12,9 @@
from pyjwt_key_fetcher.key import Key


def key_builder(f, *args, **kwargs) -> str:
def key_builder(
f: Callable[..., Any], *args: "Provider", **kwargs: Dict[str, Any]
) -> str:
"""
Custom key builder for aiocache that uses the self.uuid instead of serializing
self to something that contains some memory address that might get reused later,
Expand Down Expand Up @@ -82,7 +84,7 @@ async def _get_jwks_uri(self) -> str:
"""
conf = await self.get_configuration()
try:
jwks_uri = conf["jwks_uri"]
jwks_uri: str = conf["jwks_uri"]
except KeyError as e:
raise JWTProviderConfigError("Missing 'jwks_uri' in configuration") from e
return jwks_uri
Expand Down
Empty file added pyjwt_key_fetcher/py.typed
Empty file.
2 changes: 1 addition & 1 deletion pyjwt_key_fetcher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def unsigned_int_to_urlsafe_b64(i: int) -> str:
Encode unsigned integers as urlsafe base64 strings.
"""

def byte_len(n):
def byte_len(n: int) -> int:
length = 0
while n > 0:
length += 1
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ types-cachetools = "^5.3.0"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
exclude = ["pyjwt_key_fetcher/tests"]
strict = true
# enable once aiocache is fully typed
disallow_untyped_decorators = false

0 comments on commit 6383924

Please sign in to comment.