Skip to content
New issue

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

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

Already on GitHub? # to your account

Enforce stricter type checking #5

Merged
merged 4 commits into from
Feb 10, 2024
Merged
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
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.9
hooks:
# Run the linter.
- id: ruff
args: ["--fix"]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.350
hooks:
- id: pyright
15 changes: 14 additions & 1 deletion findmy/accessory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import logging
from datetime import datetime, timedelta
from typing import Generator
from typing import Generator, overload

from typing_extensions import override

from .keys import KeyGenerator, KeyPair, KeyType
from .util import crypto
Expand Down Expand Up @@ -141,15 +143,26 @@ def _generate_keys(self, start: int, stop: int | None) -> Generator[KeyPair, Non

ind += 1

@override
def __iter__(self) -> KeyGenerator:
self._iter_ind = -1
return self

@override
def __next__(self) -> KeyPair:
self._iter_ind += 1

return self._get_keypair(self._iter_ind)

@overload
def __getitem__(self, val: int) -> KeyPair:
...

@overload
def __getitem__(self, val: slice) -> Generator[KeyPair, None, None]:
...

@override
def __getitem__(self, val: int | slice) -> KeyPair | Generator[KeyPair, None, None]:
if isinstance(val, int):
if val < 0:
Expand Down
11 changes: 9 additions & 2 deletions findmy/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Generator, Generic, TypeVar, overload

from cryptography.hazmat.primitives.asymmetric import ec
from typing_extensions import override

from .util import crypto

Expand Down Expand Up @@ -49,10 +50,12 @@ def hashed_adv_key_b64(self) -> str:
"""Return the hashed advertised (public) key as a base64-encoded string."""
return base64.b64encode(self.hashed_adv_key_bytes).decode("ascii")

@override
def __hash__(self) -> int:
return crypto.bytes_to_int(self.adv_key_bytes)

def __eq__(self, other: HasPublicKey) -> bool:
@override
def __eq__(self, other: object) -> bool:
if not isinstance(other, HasPublicKey):
return NotImplemented

Expand Down Expand Up @@ -107,6 +110,7 @@ def private_key_b64(self) -> str:
return base64.b64encode(self.private_key_bytes).decode("ascii")

@property
@override
def adv_key_bytes(self) -> bytes:
"""Return the advertised (public) key as bytes."""
key_bytes = self._priv_key.public_key().public_numbers().x
Expand All @@ -116,6 +120,7 @@ def dh_exchange(self, other_pub_key: ec.EllipticCurvePublicKey) -> bytes:
"""Do a Diffie-Hellman key exchange using another EC public key."""
return self._priv_key.exchange(ec.ECDH(), other_pub_key)

@override
def __repr__(self) -> str:
return f'KeyPair(public_key="{self.adv_key_b64}", type={self.key_type})'

Expand All @@ -135,11 +140,13 @@ def __next__(self) -> K:
return NotImplemented

@overload
@abstractmethod
def __getitem__(self, val: int) -> K:
...

@overload
def __getitem__(self, slc: slice) -> Generator[K, None, None]:
@abstractmethod
def __getitem__(self, val: slice) -> Generator[K, None, None]:
...

@abstractmethod
Expand Down
Loading
Loading