Skip to content

Commit e3861dd

Browse files
committed
Stricter mypy setup
* Move config to pyproject.toml * Turn on useful mypy options * Don't check tests with mypy: too much to fix right now * Add py.typed to announce this project is type annotated * Fix various annotation issues Issues remain in hash module Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent d2f8663 commit e3861dd

File tree

8 files changed

+48
-46
lines changed

8 files changed

+48
-46
lines changed

mypy.ini

Lines changed: 0 additions & 37 deletions
This file was deleted.

pyproject.toml

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ include = [
6262
"/securesystemslib",
6363
"/requirements*.txt",
6464
"/tox.ini",
65-
"/mypy.ini",
6665
"/CHANGELOG.md",
6766
"/.coveragerc",
6867
]
@@ -87,4 +86,41 @@ indent-width = 4
8786
"tests/*" = [
8887
"S", # bandit: Not running bandit on tests
8988
"E501" # line-too-long
90-
]
89+
]
90+
91+
[tool.mypy]
92+
warn_unused_configs = "True"
93+
warn_redundant_casts = "True"
94+
warn_unused_ignores = "True"
95+
warn_unreachable = "True"
96+
strict_equality = "True"
97+
disallow_untyped_defs = "True"
98+
show_error_codes = "True"
99+
100+
exclude = [
101+
"^securesystemslib/_vendor/",
102+
"^securesystemslib/_gpg/"
103+
]
104+
105+
[[tool.mypy.overrides]]
106+
module = [
107+
# let's not install typeshed annotations for GCPSigner
108+
"google.*",
109+
# Suppress error messages for non-annotating dependencies
110+
"PyKCS11.*",
111+
"asn1crypto.*",
112+
"sigstore_protobuf_specs.*",
113+
"pyspx.*",
114+
"azure.*",
115+
"boto3.*",
116+
"botocore.*",
117+
"hvac.*",
118+
]
119+
ignore_missing_imports = "True"
120+
121+
[[tool.mypy.overrides]]
122+
module = [
123+
"securesystemslib._gpg.*",
124+
"securesystemslib._vendor.*",
125+
]
126+
follow_imports = "skip"

securesystemslib/py.typed

Whitespace-only changes.

securesystemslib/signer/_hsm_signer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
_PYKCS11LIB = None
6565

6666

67-
def PYKCS11LIB(): # noqa: N802
67+
def PYKCS11LIB(): # type: ignore[no-untyped-def] # noqa: N802
6868
"""Pseudo-singleton to load shared library using PYKCS11LIB envvar only once."""
6969
global _PYKCS11LIB # noqa: PLW0603
7070
if _PYKCS11LIB is None:

securesystemslib/signer/_key.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
SECP256R1,
2727
SECP384R1,
2828
SECP521R1,
29+
EllipticCurve,
2930
EllipticCurvePublicKey,
3031
)
3132
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
@@ -346,11 +347,13 @@ def _verify_ed25519_fallback(self, signature: bytes, data: bytes) -> None:
346347
def _verify(self, signature: bytes, data: bytes) -> None:
347348
"""Helper to verify signature using pyca/cryptography (default)."""
348349

349-
def _validate_type(key, type_):
350+
def _validate_type(key: object, type_: type) -> None:
350351
if not isinstance(key, type_):
351352
raise ValueError(f"bad key {key} for {self.scheme}")
352353

353-
def _validate_curve(key, curve):
354+
def _validate_curve(
355+
key: EllipticCurvePublicKey, curve: type[EllipticCurve]
356+
) -> None:
354357
if not isinstance(key.curve, curve):
355358
raise ValueError(f"bad curve {key.curve} for {self.scheme}")
356359

securesystemslib/signer/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from securesystemslib.hash import digest
1010

1111

12-
def compute_default_keyid(keytype: str, scheme, keyval: dict[str, Any]) -> str:
12+
def compute_default_keyid(keytype: str, scheme: str, keyval: dict[str, Any]) -> str:
1313
"""Return sha256 hexdigest of the canonical json of the key."""
1414
data: str | None = encode_canonical(
1515
{

securesystemslib/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from abc import ABCMeta, abstractmethod
2626
from collections.abc import Iterator
2727
from contextlib import contextmanager
28-
from typing import IO, BinaryIO
28+
from typing import IO, Any, BinaryIO
2929

3030
from securesystemslib import exceptions
3131

@@ -189,7 +189,7 @@ class FilesystemBackend(StorageBackendInterface):
189189
# objects.
190190
_instance = None
191191

192-
def __new__(cls, *args, **kwargs):
192+
def __new__(cls, *args: Any, **kwargs: Any) -> FilesystemBackend:
193193
if cls._instance is None:
194194
cls._instance = object.__new__(cls, *args, **kwargs)
195195
return cls._instance

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ commands =
6969
ruff format --diff {[testenv:lint]lint_dirs}
7070
ruff check {[testenv:lint]lint_dirs}
7171

72-
mypy {[testenv:lint]lint_dirs}
72+
mypy securesystemslib
7373
zizmor --persona=pedantic -q .
7474

7575
[testenv:fix]

0 commit comments

Comments
 (0)