Skip to content

Commit f6a68eb

Browse files
committed
Remove Optional and Union, fix Type
1 parent 8edee46 commit f6a68eb

File tree

6 files changed

+46
-52
lines changed

6 files changed

+46
-52
lines changed

src/cryptojwt/jwe/fernet.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import base64
22
import os
3-
from typing import Optional, Union
43

54
from cryptography.fernet import Fernet
65
from cryptography.hazmat.primitives import hashes
@@ -15,12 +14,12 @@
1514
class FernetEncrypter(Encrypter):
1615
def __init__(
1716
self,
18-
password: Optional[str] = None,
19-
salt: Optional[bytes] = "",
20-
key: Optional[bytes] = None,
21-
hash_alg: Optional[str] = "SHA256",
22-
digest_size: Optional[int] = 0,
23-
iterations: Optional[int] = DEFAULT_ITERATIONS,
17+
password: str | None = None,
18+
salt: bytes | None = "",
19+
key: bytes | None = None,
20+
hash_alg: str | None = "SHA256",
21+
digest_size: int | None = 0,
22+
iterations: int | None = DEFAULT_ITERATIONS,
2423
):
2524
Encrypter.__init__(self)
2625

@@ -45,14 +44,14 @@ def __init__(
4544

4645
self.core = Fernet(self.key)
4746

48-
def encrypt(self, msg: Union[str, bytes], **kwargs) -> bytes:
47+
def encrypt(self, msg: str | bytes, **kwargs) -> bytes:
4948
text = as_bytes(msg)
5049
# Padding to block size of AES
5150
if len(text) % 16:
5251
text += b" " * (16 - len(text) % 16)
5352
return self.core.encrypt(as_bytes(text))
5453

55-
def decrypt(self, msg: Union[str, bytes], **kwargs) -> bytes:
54+
def decrypt(self, msg: str | bytes, **kwargs) -> bytes:
5655
dec_text = self.core.decrypt(as_bytes(msg))
5756
dec_text = dec_text.rstrip(b" ")
5857
return dec_text

src/cryptojwt/jwt.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import uuid
88
from collections.abc import MutableMapping
99
from json import JSONDecodeError
10-
from typing import Optional
1110

1211
from .exception import HeaderError, VerificationError
1312
from .jwe.jwe import JWE, factory as jwe_factory
@@ -84,15 +83,15 @@ def __init__(
8483
encrypt: bool = False,
8584
enc_enc: str = "A128GCM",
8685
enc_alg: str = "RSA-OAEP-256",
87-
msg_cls: Optional[MutableMapping] = None,
88-
iss2msg_cls: Optional[dict[str, str]] = None,
89-
skew: Optional[int] = 15,
90-
allowed_sign_algs: Optional[list[str]] = None,
91-
allowed_enc_algs: Optional[list[str]] = None,
92-
allowed_enc_encs: Optional[list[str]] = None,
93-
allowed_max_lifetime: Optional[int] = None,
94-
zip: Optional[str] = "",
95-
typ2msg_cls: Optional[dict] = None,
86+
msg_cls: type[MutableMapping] | None = None,
87+
iss2msg_cls: dict[str, str] | None = None,
88+
skew: int | None = 15,
89+
allowed_sign_algs: list[str] | None = None,
90+
allowed_enc_algs: list[str] | None = None,
91+
allowed_enc_encs: list[str] | None = None,
92+
allowed_max_lifetime: int | None = None,
93+
zip: str | None = "",
94+
typ2msg_cls: dict | None = None,
9695
):
9796
self.key_jar = key_jar # KeyJar instance
9897
self.iss = iss # My identifier
@@ -209,13 +208,13 @@ def message(self, signing_key, **kwargs):
209208

210209
def pack(
211210
self,
212-
payload: Optional[dict] = None,
213-
kid: Optional[str] = "",
214-
issuer_id: Optional[str] = "",
215-
recv: Optional[str] = "",
216-
aud: Optional[str] = None,
217-
iat: Optional[int] = None,
218-
jws_headers: Optional[dict[str, str]] = None,
211+
payload: dict | None = None,
212+
kid: str | None = "",
213+
issuer_id: str | None = "",
214+
recv: str | None = "",
215+
aud: str | None = None,
216+
iat: int | None = None,
217+
jws_headers: dict[str, str] | None = None,
219218
**kwargs,
220219
) -> str:
221220
"""

src/cryptojwt/key_bundle.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import time
1010
from datetime import datetime
1111
from functools import cmp_to_key
12-
from typing import Optional
1312

1413
import requests
1514

@@ -808,7 +807,7 @@ def difference(self, bundle):
808807

809808
return [k for k in self.keys() if k not in bundle]
810809

811-
def dump(self, exclude_attributes: Optional[list[str]] = None):
810+
def dump(self, exclude_attributes: list[str] | None = None):
812811
if exclude_attributes is None:
813812
exclude_attributes = []
814813

src/cryptojwt/key_issuer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import logging
33
import os
4-
from typing import Optional
54

65
from requests import request
76

@@ -345,7 +344,7 @@ def __len__(self):
345344
nr += len(kb)
346345
return nr
347346

348-
def dump(self, exclude_attributes: Optional[list[str]] = None) -> dict:
347+
def dump(self, exclude_attributes: list[str] | None = None) -> dict:
349348
"""
350349
Returns the content as a dictionary.
351350

src/cryptojwt/key_jar.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import json
33
import logging
4-
from typing import Optional
54

65
from requests import request
76

@@ -64,7 +63,7 @@ def _issuer_ids(self) -> list[str]:
6463
return list(self._issuers.keys())
6564

6665
@deprecated_alias(issuer="issuer_id", owner="issuer_id")
67-
def _get_issuer(self, issuer_id: str) -> Optional[KeyIssuer]:
66+
def _get_issuer(self, issuer_id: str) -> KeyIssuer | None:
6867
"""
6968
Return the KeyIssuer instance that has name == issuer_id
7069
@@ -623,8 +622,8 @@ def __len__(self):
623622

624623
def _dump_issuers(
625624
self,
626-
exclude_issuers: Optional[list[str]] = None,
627-
exclude_attributes: Optional[list[str]] = None,
625+
exclude_issuers: list[str] | None = None,
626+
exclude_attributes: list[str] | None = None,
628627
):
629628
_issuers = {}
630629
for _id, _issuer in self._issuers.items():
@@ -635,8 +634,8 @@ def _dump_issuers(
635634

636635
def dump(
637636
self,
638-
exclude_issuers: Optional[list[str]] = None,
639-
exclude_attributes: Optional[list[str]] = None,
637+
exclude_issuers: list[str] | None = None,
638+
exclude_attributes: list[str] | None = None,
640639
) -> dict:
641640
"""
642641
Returns the key jar content as dictionary
@@ -667,7 +666,7 @@ def dump(
667666

668667
return info
669668

670-
def dumps(self, exclude_issuers: Optional[list[str]] = None):
669+
def dumps(self, exclude_issuers: list[str] | None = None):
671670
"""
672671
Returns a JSON representation of the key jar
673672
@@ -680,8 +679,8 @@ def dumps(self, exclude_issuers: Optional[list[str]] = None):
680679
def load(
681680
self,
682681
info: dict,
683-
init_args: Optional[dict] = None,
684-
load_args: Optional[dict] = None,
682+
init_args: dict | None = None,
683+
load_args: dict | None = None,
685684
):
686685
"""
687686

src/cryptojwt/tools/keyconv.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import json
77
from binascii import hexlify
88
from getpass import getpass
9-
from typing import Optional
109

1110
from cryptography.hazmat.primitives import serialization
1211

@@ -35,9 +34,9 @@ def jwk_from_file(filename: str, private: bool = True) -> JWK:
3534

3635
def pem2rsa(
3736
filename: str,
38-
kid: Optional[str] = None,
37+
kid: str | None = None,
3938
private: bool = False,
40-
passphrase: Optional[str] = None,
39+
passphrase: str | None = None,
4140
) -> JWK:
4241
"""Convert RSA key from PEM to JWK"""
4342
if private:
@@ -51,9 +50,9 @@ def pem2rsa(
5150

5251
def pem2ec(
5352
filename: str,
54-
kid: Optional[str] = None,
53+
kid: str | None = None,
5554
private: bool = False,
56-
passphrase: Optional[str] = None,
55+
passphrase: str | None = None,
5756
) -> JWK:
5857
"""Convert EC key from PEM to JWK"""
5958
if private:
@@ -67,9 +66,9 @@ def pem2ec(
6766

6867
def pem2okp(
6968
filename: str,
70-
kid: Optional[str] = None,
69+
kid: str | None = None,
7170
private: bool = False,
72-
passphrase: Optional[str] = None,
71+
passphrase: str | None = None,
7372
) -> JWK:
7473
"""Convert OKP key from PEM to JWK"""
7574
if private:
@@ -90,10 +89,10 @@ def bin2jwk(filename: str, kid: str) -> JWK:
9089

9190
def pem2jwk(
9291
filename: str,
93-
kid: Optional[str] = None,
94-
kty: Optional[str] = None,
92+
kid: str | None = None,
93+
kty: str | None = None,
9594
private: bool = False,
96-
passphrase: Optional[str] = None,
95+
passphrase: str | None = None,
9796
) -> JWK:
9897
"""Read PEM from filename and return JWK"""
9998
with open(filename) as file:
@@ -144,7 +143,7 @@ def export_jwk(
144143
jwk: JWK,
145144
private: bool = False,
146145
encrypt: bool = False,
147-
passphrase: Optional[str] = None,
146+
passphrase: str | None = None,
148147
) -> bytes:
149148
"""Export JWK as PEM/bin"""
150149

@@ -177,7 +176,7 @@ def export_jwk(
177176
return serialized
178177

179178

180-
def output_jwk(jwk: JWK, private: bool = False, filename: Optional[str] = None) -> None:
179+
def output_jwk(jwk: JWK, private: bool = False, filename: str | None = None) -> None:
181180
"""Output JWK to file"""
182181
serialized = jwk.serialize(private=private)
183182
if filename is not None:
@@ -187,7 +186,7 @@ def output_jwk(jwk: JWK, private: bool = False, filename: Optional[str] = None)
187186
print(json.dumps(serialized, indent=4))
188187

189188

190-
def output_bytes(data: bytes, binary: bool = False, filename: Optional[str] = None) -> None:
189+
def output_bytes(data: bytes, binary: bool = False, filename: str | None = None) -> None:
191190
"""Output data to file"""
192191
if filename is not None:
193192
with open(filename, mode="wb") as file:

0 commit comments

Comments
 (0)