Skip to content

Commit 8b5345a

Browse files
committed
typehint code (and use absolute imports)
1 parent 122e05d commit 8b5345a

26 files changed

+829
-478
lines changed

.github/workflows/quality.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ jobs:
2626
run: uv run ruff format --diff .
2727

2828
- name: ruff check
29-
run: uv run ruff check --diff .
29+
run: uv run ruff check --diff .
30+
31+
- name: mypy
32+
run: uv run mypy .

pkcs11/__init__.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@
22
:mod:`pkcs11` defines a high-level, "Pythonic" interface to PKCS#11.
33
"""
44

5+
import typing
6+
57
from .constants import * # noqa: F403
68
from .exceptions import * # noqa: F403
79
from .mechanisms import * # noqa: F403
8-
from .types import * # noqa: F403
10+
from .types import (
11+
Certificate,
12+
DomainParameters,
13+
Library,
14+
MechanismInfo,
15+
PrivateKey,
16+
PublicKey,
17+
SecretKey,
18+
Session,
19+
Slot,
20+
Token,
21+
)
922
from .util import dh, dsa, ec, rsa, x509 # noqa: F401
1023

1124
_so = None
1225
_lib = None
1326

1427

15-
def lib(so):
28+
def lib(so: str) -> Library:
1629
"""
1730
Wrap the main library call coming from Cython with a preemptive
1831
dynamic loading.
@@ -28,9 +41,24 @@ def lib(so):
2841
else:
2942
return _lib
3043

31-
from . import _pkcs11
44+
from . import _pkcs11 # type: ignore[attr-defined]
3245

33-
_lib = _pkcs11.lib(so)
46+
_lib = typing.cast(Library, _pkcs11.lib(so))
3447
_so = so
3548

3649
return _lib
50+
51+
52+
__all__ = [
53+
"Certificate",
54+
"DomainParameters",
55+
"Library",
56+
"MechanismInfo",
57+
"PrivateKey",
58+
"PublicKey",
59+
"SecretKey",
60+
"Session",
61+
"Slot",
62+
"Token",
63+
"lib",
64+
]

pkcs11/constants.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
use these classes.
66
"""
77

8-
try:
9-
from enum import IntEnum, IntFlag, unique
10-
except ImportError:
11-
from aenum import IntEnum, IntFlag, unique
12-
8+
from enum import IntEnum, IntFlag, unique
139

1410
DEFAULT = object()
1511
"""Sentinel value used in templates.
@@ -58,7 +54,7 @@ class ObjectClass(IntEnum):
5854

5955
_VENDOR_DEFINED = 0x80000000
6056

61-
def __repr__(self):
57+
def __repr__(self) -> str:
6258
return "<ObjectClass.%s>" % self.name
6359

6460

@@ -344,7 +340,7 @@ class Attribute(IntEnum):
344340

345341
_VENDOR_DEFINED = 0x80000000
346342

347-
def __repr__(self):
343+
def __repr__(self) -> str:
348344
return "<Attribute.%s>" % self.name
349345

350346

pkcs11/defaults.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from datetime import datetime
99
from struct import Struct
10+
from typing import Any, Callable
1011

1112
from .constants import (
1213
Attribute,
@@ -35,17 +36,18 @@
3536
_SIGNING = MechanismFlag.SIGN | MechanismFlag.VERIFY
3637
_WRAPPING = MechanismFlag.WRAP | MechanismFlag.UNWRAP
3738

38-
DEFAULT_KEY_CAPABILITIES = {
39+
DEFAULT_KEY_CAPABILITIES: dict[KeyType, MechanismFlag] = {
3940
KeyType.AES: _ENCRYPTION | _SIGNING | _WRAPPING,
4041
KeyType.DES2: _ENCRYPTION | _SIGNING | _WRAPPING,
4142
KeyType.DES3: _ENCRYPTION | _SIGNING | _WRAPPING,
4243
KeyType.DH: MechanismFlag.DERIVE,
4344
KeyType.DSA: _SIGNING,
4445
KeyType.EC: _SIGNING | MechanismFlag.DERIVE,
4546
KeyType.RSA: _ENCRYPTION | _SIGNING | _WRAPPING,
46-
KeyType.GENERIC_SECRET: 0,
47+
KeyType.GENERIC_SECRET: 0, # type: ignore[dict-item]
4748
KeyType.EC_EDWARDS: _SIGNING,
4849
}
50+
4951
"""
5052
Default capabilities for generating keys.
5153
"""
@@ -125,11 +127,11 @@
125127
_biginteger = _bytes
126128

127129

128-
def _enum(type_):
130+
def _enum(type_: type[Any]) -> tuple[Callable[[Any], bytes], Callable[[Any], Any]]:
129131
"""Factory to pack/unpack intos into IntEnums."""
130132
pack, unpack = _ulong
131133

132-
return (lambda v: pack(int(v)), lambda v: type_(unpack(v)))
134+
return (lambda v: pack(int(v)), lambda v: type_(unpack(v))) # type: ignore[no-untyped-call]
133135

134136

135137
ATTRIBUTE_TYPES = {

pkcs11/mechanisms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class KeyType(IntEnum):
101101

102102
_VENDOR_DEFINED = 0x80000000
103103

104-
def __repr__(self):
104+
def __repr__(self) -> str:
105105
return "<KeyType.%s>" % self.name
106106

107107

@@ -709,7 +709,7 @@ class Mechanism(IntEnum):
709709

710710
_VENDOR_DEFINED = 0x80000000
711711

712-
def __repr__(self):
712+
def __repr__(self) -> str:
713713
return "<Mechanism.%s>" % self.name
714714

715715

@@ -729,7 +729,7 @@ class KDF(IntEnum):
729729
SHA512 = 0x00000008
730730
CPDIVERSIFY = 0x00000009
731731

732-
def __repr__(self):
732+
def __repr__(self) -> str:
733733
return "<KDF.%s>" % self.name
734734

735735

@@ -744,5 +744,5 @@ class MGF(IntEnum):
744744
SHA512 = 0x00000004
745745
SHA224 = 0x00000005
746746

747-
def __repr__(self):
747+
def __repr__(self) -> str:
748748
return "<MGF.%s>" % self.name

0 commit comments

Comments
 (0)