Skip to content

Commit cb468c8

Browse files
committed
typehint code (and use absolute imports)
1 parent 856b3a8 commit cb468c8

30 files changed

+880
-507
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: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,38 @@
22
:mod:`pkcs11` defines a high-level, "Pythonic" interface to PKCS#11.
33
"""
44

5-
from .constants import * # noqa: F403
6-
from .exceptions import * # noqa: F403
7-
from .mechanisms import * # noqa: F403
8-
from .types import * # noqa: F403
9-
from .util import dh, dsa, ec, rsa, x509 # noqa: F401
5+
import typing
6+
7+
from pkcs11.constants import (
8+
Attribute,
9+
CertificateType,
10+
MechanismFlag,
11+
ObjectClass,
12+
SlotFlag,
13+
TokenFlag,
14+
UserType,
15+
)
16+
from pkcs11.exceptions import * # noqa: F403
17+
from pkcs11.mechanisms import KDF, MGF, KeyType, Mechanism
18+
from pkcs11.types import (
19+
Certificate,
20+
DomainParameters,
21+
Library,
22+
MechanismInfo,
23+
PrivateKey,
24+
PublicKey,
25+
SecretKey,
26+
Session,
27+
Slot,
28+
Token,
29+
)
30+
from pkcs11.util import dh, dsa, ec, rsa, x509
1031

1132
_so = None
1233
_lib = None
1334

1435

15-
def lib(so):
36+
def lib(so: str) -> Library:
1637
"""
1738
Wrap the main library call coming from Cython with a preemptive
1839
dynamic loading.
@@ -22,15 +43,44 @@ def lib(so):
2243

2344
if _lib:
2445
if _so != so:
25-
raise AlreadyInitialized( # noqa: F405
26-
"Already initialized with %s" % so
27-
)
46+
raise AlreadyInitialized("Already initialized with %s" % so) # noqa: F405
2847
else:
2948
return _lib
3049

31-
from . import _pkcs11
50+
from . import _pkcs11 # type: ignore[attr-defined]
3251

33-
_lib = _pkcs11.lib(so)
52+
_lib = typing.cast(Library, _pkcs11.lib(so))
3453
_so = so
3554

3655
return _lib
56+
57+
58+
__all__ = [
59+
"KDF",
60+
"MGF",
61+
"Attribute",
62+
"Certificate",
63+
"CertificateType",
64+
"DomainParameters",
65+
"KeyType",
66+
"Library",
67+
"Mechanism",
68+
"MechanismFlag",
69+
"MechanismInfo",
70+
"ObjectClass",
71+
"PrivateKey",
72+
"PublicKey",
73+
"SecretKey",
74+
"Session",
75+
"Slot",
76+
"SlotFlag",
77+
"Token",
78+
"TokenFlag",
79+
"UserType",
80+
"dh",
81+
"dsa",
82+
"ec",
83+
"lib",
84+
"rsa",
85+
"x509",
86+
]

pkcs11/_errors.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Map from CKR return codes to Python exceptions.
33
"""
44

5-
from .exceptions import *
5+
from pkcs11.exceptions import *
66

77
cdef ERROR_MAP = {
88
CKR_ATTRIBUTE_TYPE_INVALID: AttributeTypeInvalid,

pkcs11/_pkcs11.pyx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ for Sphinx/Jedi/etc, as this module is not importable without having the
88
library loaded.
99
"""
1010

11-
from __future__ import (absolute_import, unicode_literals,
12-
print_function, division)
13-
1411
from cython.view cimport array
1512
from cpython.mem cimport PyMem_Malloc, PyMem_Free
1613

@@ -19,16 +16,25 @@ IF UNAME_SYSNAME == "Windows":
1916
ELSE:
2017
from posix cimport dlfcn
2118

22-
from ._pkcs11_defn cimport *
19+
from pkcs11._pkcs11_defn cimport *
2320
include '_errors.pyx'
2421
include '_utils.pyx'
2522

26-
from . import types
27-
from .defaults import *
28-
from .exceptions import *
29-
from .constants import *
30-
from .mechanisms import *
31-
from .types import (
23+
from pkcs11 import types
24+
from pkcs11.defaults import (
25+
DEFAULT_DERIVE_MECHANISMS,
26+
DEFAULT_ENCRYPT_MECHANISMS,
27+
DEFAULT_GENERATE_MECHANISMS,
28+
DEFAULT_KEY_CAPABILITIES,
29+
DEFAULT_MECHANISM_PARAMS,
30+
DEFAULT_PARAM_GENERATE_MECHANISMS,
31+
DEFAULT_SIGN_MECHANISMS,
32+
DEFAULT_WRAP_MECHANISMS,
33+
)
34+
from pkcs11.exceptions import ArgumentsBad
35+
from pkcs11.constants import DEFAULT, Attribute, MechanismFlag, ObjectClass, UserType, TokenFlag
36+
from pkcs11.mechanisms import KeyType, Mechanism
37+
from pkcs11.types import (
3238
_CK_UTF8CHAR_to_str,
3339
_CK_VERSION_to_tuple,
3440
_CK_MECHANISM_TYPE_to_enum,

pkcs11/_utils.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Type wrangling utility functions.
33
"""
44

5-
from .constants import *
6-
from .mechanisms import *
5+
from pkcs11.defaults import ATTRIBUTE_TYPES
76

87

98
cdef CK_BYTE_buffer(length):

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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

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

11-
from .constants import (
12+
from pkcs11.constants import (
1213
Attribute,
1314
CertificateType,
1415
MechanismFlag,
1516
ObjectClass,
1617
)
17-
from .mechanisms import MGF, KeyType, Mechanism
18+
from pkcs11.mechanisms import MGF, KeyType, Mechanism
1819

1920
DEFAULT_GENERATE_MECHANISMS = {
2021
KeyType.AES: Mechanism.AES_KEY_GEN,
@@ -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

pkcs11/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)