Skip to content

Commit

Permalink
Add overloads for better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Nov 19, 2024
1 parent b1fcaa3 commit badaee1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 88
ignore = E203, W503
extend-ignore = E203,W503,E701
9 changes: 9 additions & 0 deletions fido2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
Mapping,
Sequence,
Tuple,
overload,
)

import abc
Expand Down Expand Up @@ -463,6 +464,14 @@ def _get_extension_results(self, assertion):
return ClientExtensionOutputs(extension_outputs)


@overload
def _cbor_list(values: Sequence) -> list: ...


@overload
def _cbor_list(values: None) -> None: ...


def _cbor_list(values):
if not values:
return None
Expand Down
4 changes: 2 additions & 2 deletions fido2/ctap2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class AssertionResponse(_CborDataObject):
(only set for the first response, if > 1).
"""

credential: Dict[str, Any]
credential: Mapping[str, Any]
auth_data: AuthenticatorData
signature: bytes
user: Optional[Dict[str, Any]] = None
Expand All @@ -154,7 +154,7 @@ def verify(self, client_param: bytes, public_key: CoseKey):

@classmethod
def from_ctap1(
cls, app_param: bytes, credential: Dict[str, Any], authentication
cls, app_param: bytes, credential: Mapping[str, Any], authentication
) -> "AssertionResponse":
"""Create an AssertionResponse from a CTAP1 SignatureData instance.
Expand Down
13 changes: 12 additions & 1 deletion fido2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
TypeVar,
Hashable,
get_type_hints,
overload,
Type,
)
import struct
import warnings
Expand Down Expand Up @@ -168,6 +170,7 @@ def read(self, size: Optional[int] = -1) -> bytes:


_T = TypeVar("_T", bound=Hashable)
_S = TypeVar("_S", bound="_DataClassMapping")


class _DataClassMapping(Mapping[_T, Any]):
Expand Down Expand Up @@ -252,8 +255,16 @@ def _parse_value(cls, t, value):
# Convert to enum values, other wrappers
return t(value)

@overload
@classmethod
def from_dict(cls, data: Optional[Mapping[_T, Any]]):
def from_dict(cls: Type[_S], data: None) -> None: ...

@overload
@classmethod
def from_dict(cls: Type[_S], data: Mapping[_T, Any]) -> _S: ...

@classmethod
def from_dict(cls, data):
if data is None:
return None
if isinstance(data, cls):
Expand Down
6 changes: 3 additions & 3 deletions fido2/webauthn.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def _parse_value(cls, t, value):
return super(_JsonDataObject, cls)._parse_value(t, value)

@classmethod
def from_dict(cls, data: Optional[Mapping[str, Any]]):
def from_dict(cls, data):
webauthn_json_mapping.warn()
return super().from_dict(data)

Expand Down Expand Up @@ -566,7 +566,7 @@ def __getitem__(self, key):
return super().__getitem__(key)

@classmethod
def from_dict(cls, data: Optional[Mapping[str, Any]]):
def from_dict(cls, data):
if data is not None and not webauthn_json_mapping.enabled:
value = dict(data)
value["clientDataJSON"] = value.pop("clientData", None)
Expand Down Expand Up @@ -596,7 +596,7 @@ def __getitem__(self, key):
return super().__getitem__(key)

@classmethod
def from_dict(cls, data: Optional[Mapping[str, Any]]):
def from_dict(cls, data):
if data is not None and not webauthn_json_mapping.enabled:
value = dict(data)
value["clientDataJSON"] = value.pop("clientData", None)
Expand Down

0 comments on commit badaee1

Please sign in to comment.