Skip to content

Commit f558199

Browse files
authored
Try running an extra ruff rule (#9402)
* Try running an extra ruff rule I think `from __future__ import annotations` means this is fine, even on older Python * Enable UP007 * Enable UP038
1 parent de7d0e4 commit f558199

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+504
-605
lines changed

noxfile.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pathlib
1111
import re
1212
import sys
13-
import typing
1413
import uuid
1514

1615
import nox
@@ -227,7 +226,7 @@ def rust(session: nox.Session) -> None:
227226

228227
def process_rust_coverage(
229228
session: nox.Session,
230-
rust_binaries: typing.List[str],
229+
rust_binaries: list[str],
231230
prof_raw_location: pathlib.Path,
232231
) -> None:
233232
# Hitting weird issues merging Windows and Linux Rust coverage, so just

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ exclude_lines = [
136136
]
137137

138138
[tool.ruff]
139-
# UP006: Minimum Python 3.9
140-
# UP007, UP038: Minimum Python 3.10
141-
ignore = ['N818', 'UP006', 'UP007', 'UP038']
139+
ignore = ['N818']
142140
select = ['E', 'F', 'I', 'N', 'W', 'UP', 'RUF']
143141
line-length = 79
144142

src/_cffi_src/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import platform
99
import sys
10-
import typing
1110

1211
from cffi import FFI
1312

@@ -21,7 +20,7 @@
2120
def build_ffi_for_binding(
2221
module_name: str,
2322
module_prefix: str,
24-
modules: typing.List[str],
23+
modules: list[str],
2524
):
2625
"""
2726
Modules listed in ``modules`` should have the following attributes:

src/cryptography/exceptions.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616

1717
class UnsupportedAlgorithm(Exception):
18-
def __init__(
19-
self, message: str, reason: typing.Optional[_Reasons] = None
20-
) -> None:
18+
def __init__(self, message: str, reason: _Reasons | None = None) -> None:
2119
super().__init__(message)
2220
self._reason = reason
2321

@@ -44,7 +42,7 @@ class InvalidSignature(Exception):
4442

4543
class InternalError(Exception):
4644
def __init__(
47-
self, msg: str, err_code: typing.List[rust_openssl.OpenSSLError]
45+
self, msg: str, err_code: list[rust_openssl.OpenSSLError]
4846
) -> None:
4947
super().__init__(msg)
5048
self.err_code = err_code

src/cryptography/fernet.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class InvalidToken(Exception):
2727
class Fernet:
2828
def __init__(
2929
self,
30-
key: typing.Union[bytes, str],
30+
key: bytes | str,
3131
backend: typing.Any = None,
3232
) -> None:
3333
try:
@@ -80,9 +80,7 @@ def _encrypt_from_parts(
8080
hmac = h.finalize()
8181
return base64.urlsafe_b64encode(basic_parts + hmac)
8282

83-
def decrypt(
84-
self, token: typing.Union[bytes, str], ttl: typing.Optional[int] = None
85-
) -> bytes:
83+
def decrypt(self, token: bytes | str, ttl: int | None = None) -> bytes:
8684
timestamp, data = Fernet._get_unverified_token_data(token)
8785
if ttl is None:
8886
time_info = None
@@ -91,7 +89,7 @@ def decrypt(
9189
return self._decrypt_data(data, timestamp, time_info)
9290

9391
def decrypt_at_time(
94-
self, token: typing.Union[bytes, str], ttl: int, current_time: int
92+
self, token: bytes | str, ttl: int, current_time: int
9593
) -> bytes:
9694
if ttl is None:
9795
raise ValueError(
@@ -100,16 +98,14 @@ def decrypt_at_time(
10098
timestamp, data = Fernet._get_unverified_token_data(token)
10199
return self._decrypt_data(data, timestamp, (ttl, current_time))
102100

103-
def extract_timestamp(self, token: typing.Union[bytes, str]) -> int:
101+
def extract_timestamp(self, token: bytes | str) -> int:
104102
timestamp, data = Fernet._get_unverified_token_data(token)
105103
# Verify the token was not tampered with.
106104
self._verify_signature(data)
107105
return timestamp
108106

109107
@staticmethod
110-
def _get_unverified_token_data(
111-
token: typing.Union[bytes, str]
112-
) -> typing.Tuple[int, bytes]:
108+
def _get_unverified_token_data(token: bytes | str) -> tuple[int, bytes]:
113109
if not isinstance(token, (str, bytes)):
114110
raise TypeError("token must be bytes or str")
115111

@@ -139,7 +135,7 @@ def _decrypt_data(
139135
self,
140136
data: bytes,
141137
timestamp: int,
142-
time_info: typing.Optional[typing.Tuple[int, int]],
138+
time_info: tuple[int, int] | None,
143139
) -> bytes:
144140
if time_info is not None:
145141
ttl, current_time = time_info
@@ -186,7 +182,7 @@ def encrypt(self, msg: bytes) -> bytes:
186182
def encrypt_at_time(self, msg: bytes, current_time: int) -> bytes:
187183
return self._fernets[0].encrypt_at_time(msg, current_time)
188184

189-
def rotate(self, msg: typing.Union[bytes, str]) -> bytes:
185+
def rotate(self, msg: bytes | str) -> bytes:
190186
timestamp, data = Fernet._get_unverified_token_data(msg)
191187
for f in self._fernets:
192188
try:
@@ -200,9 +196,7 @@ def rotate(self, msg: typing.Union[bytes, str]) -> bytes:
200196
iv = os.urandom(16)
201197
return self._fernets[0]._encrypt_from_parts(p, timestamp, iv)
202198

203-
def decrypt(
204-
self, msg: typing.Union[bytes, str], ttl: typing.Optional[int] = None
205-
) -> bytes:
199+
def decrypt(self, msg: bytes | str, ttl: int | None = None) -> bytes:
206200
for f in self._fernets:
207201
try:
208202
return f.decrypt(msg, ttl)
@@ -211,7 +205,7 @@ def decrypt(
211205
raise InvalidToken
212206

213207
def decrypt_at_time(
214-
self, msg: typing.Union[bytes, str], ttl: int, current_time: int
208+
self, msg: bytes | str, ttl: int, current_time: int
215209
) -> bytes:
216210
for f in self._fernets:
217211
try:

src/cryptography/hazmat/_oid.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
from __future__ import annotations
66

7-
import typing
8-
97
from cryptography.hazmat.bindings._rust import (
108
ObjectIdentifier as ObjectIdentifier,
119
)
@@ -124,9 +122,7 @@ class SignatureAlgorithmOID:
124122
GOSTR3410_2012_WITH_3411_2012_512 = ObjectIdentifier("1.2.643.7.1.1.3.3")
125123

126124

127-
_SIG_OIDS_TO_HASH: typing.Dict[
128-
ObjectIdentifier, typing.Optional[hashes.HashAlgorithm]
129-
] = {
125+
_SIG_OIDS_TO_HASH: dict[ObjectIdentifier, hashes.HashAlgorithm | None] = {
130126
SignatureAlgorithmOID.RSA_WITH_MD5: hashes.MD5(),
131127
SignatureAlgorithmOID.RSA_WITH_SHA1: hashes.SHA1(),
132128
SignatureAlgorithmOID._RSA_WITH_SHA1: hashes.SHA1(),

src/cryptography/hazmat/backends/openssl/aead.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _encrypt(
6262
cipher: _AEADTypes,
6363
nonce: bytes,
6464
data: bytes,
65-
associated_data: typing.List[bytes],
65+
associated_data: list[bytes],
6666
tag_length: int,
6767
ctx: typing.Any = None,
6868
) -> bytes:
@@ -81,7 +81,7 @@ def _decrypt(
8181
cipher: _AEADTypes,
8282
nonce: bytes,
8383
data: bytes,
84-
associated_data: typing.List[bytes],
84+
associated_data: list[bytes],
8585
tag_length: int,
8686
ctx: typing.Any = None,
8787
) -> bytes:
@@ -99,7 +99,7 @@ def _evp_aead_create_ctx(
9999
backend: Backend,
100100
cipher: _AEADTypes,
101101
key: bytes,
102-
tag_len: typing.Optional[int] = None,
102+
tag_len: int | None = None,
103103
):
104104
aead_cipher = _evp_aead_get_cipher(backend, cipher)
105105
assert aead_cipher is not None
@@ -132,7 +132,7 @@ def _evp_aead_encrypt(
132132
cipher: _AEADTypes,
133133
nonce: bytes,
134134
data: bytes,
135-
associated_data: typing.List[bytes],
135+
associated_data: list[bytes],
136136
tag_length: int,
137137
ctx: typing.Any,
138138
) -> bytes:
@@ -173,7 +173,7 @@ def _evp_aead_decrypt(
173173
cipher: _AEADTypes,
174174
nonce: bytes,
175175
data: bytes,
176-
associated_data: typing.List[bytes],
176+
associated_data: list[bytes],
177177
tag_length: int,
178178
ctx: typing.Any,
179179
) -> bytes:
@@ -269,7 +269,7 @@ def _evp_cipher_aead_setup(
269269
cipher_name: bytes,
270270
key: bytes,
271271
nonce: bytes,
272-
tag: typing.Optional[bytes],
272+
tag: bytes | None,
273273
tag_len: int,
274274
operation: int,
275275
):
@@ -375,7 +375,7 @@ def _evp_cipher_encrypt(
375375
cipher: _AEADTypes,
376376
nonce: bytes,
377377
data: bytes,
378-
associated_data: typing.List[bytes],
378+
associated_data: list[bytes],
379379
tag_length: int,
380380
ctx: typing.Any = None,
381381
) -> bytes:
@@ -427,7 +427,7 @@ def _evp_cipher_decrypt(
427427
cipher: _AEADTypes,
428428
nonce: bytes,
429429
data: bytes,
430-
associated_data: typing.List[bytes],
430+
associated_data: list[bytes],
431431
tag_length: int,
432432
ctx: typing.Any = None,
433433
) -> bytes:

src/cryptography/hazmat/backends/openssl/backend.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Backend:
9191
# disallowed algorithms are still present in OpenSSL. They just error if
9292
# you try to use them. To avoid that we allowlist the algorithms in
9393
# FIPS 140-3. This isn't ideal, but FIPS 140-3 is trash so here we are.
94-
_fips_aead: typing.ClassVar[typing.Set[bytes]] = {
94+
_fips_aead: typing.ClassVar[set[bytes]] = {
9595
b"aes-128-ccm",
9696
b"aes-192-ccm",
9797
b"aes-256-ccm",
@@ -136,8 +136,8 @@ def __init__(self) -> None:
136136
self._lib = self._binding.lib
137137
self._fips_enabled = rust_openssl.is_fips_enabled()
138138

139-
self._cipher_registry: typing.Dict[
140-
typing.Tuple[typing.Type[CipherAlgorithm], typing.Type[Mode]],
139+
self._cipher_registry: dict[
140+
tuple[type[CipherAlgorithm], type[Mode]],
141141
typing.Callable,
142142
] = {}
143143
self._register_default_ciphers()
@@ -155,7 +155,7 @@ def __repr__(self) -> str:
155155
def openssl_assert(
156156
self,
157157
ok: bool,
158-
errors: typing.Optional[typing.List[rust_openssl.OpenSSLError]] = None,
158+
errors: list[rust_openssl.OpenSSLError] | None = None,
159159
) -> None:
160160
return binding._openssl_assert(self._lib, ok, errors=errors)
161161

@@ -327,7 +327,7 @@ def create_symmetric_decryption_ctx(
327327
def pbkdf2_hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
328328
return self.hmac_supported(algorithm)
329329

330-
def _consume_errors(self) -> typing.List[rust_openssl.OpenSSLError]:
330+
def _consume_errors(self) -> list[rust_openssl.OpenSSLError]:
331331
return rust_openssl.capture_error_stack()
332332

333333
def _bn_to_int(self, bn) -> int:
@@ -685,7 +685,7 @@ def create_cmac_ctx(self, algorithm: BlockCipherAlgorithm) -> _CMACContext:
685685
def load_pem_private_key(
686686
self,
687687
data: bytes,
688-
password: typing.Optional[bytes],
688+
password: bytes | None,
689689
unsafe_skip_rsa_key_validation: bool,
690690
) -> PrivateKeyTypes:
691691
return self._load_key(
@@ -740,7 +740,7 @@ def load_pem_public_key(self, data: bytes) -> PublicKeyTypes:
740740
def load_der_private_key(
741741
self,
742742
data: bytes,
743-
password: typing.Optional[bytes],
743+
password: bytes | None,
744744
unsafe_skip_rsa_key_validation: bool,
745745
) -> PrivateKeyTypes:
746746
# OpenSSL has a function called d2i_AutoPrivateKey that in theory
@@ -1173,7 +1173,7 @@ def dh_supported(self) -> bool:
11731173
return not self._lib.CRYPTOGRAPHY_IS_BORINGSSL
11741174

11751175
def dh_parameters_supported(
1176-
self, p: int, g: int, q: typing.Optional[int] = None
1176+
self, p: int, g: int, q: int | None = None
11771177
) -> bool:
11781178
try:
11791179
rust_openssl.dh.from_parameter_numbers(
@@ -1247,11 +1247,11 @@ def _zeroed_null_terminated_buf(self, data):
12471247
self._zero_data(self._ffi.cast("uint8_t *", buf), data_len)
12481248

12491249
def load_key_and_certificates_from_pkcs12(
1250-
self, data: bytes, password: typing.Optional[bytes]
1251-
) -> typing.Tuple[
1252-
typing.Optional[PrivateKeyTypes],
1253-
typing.Optional[x509.Certificate],
1254-
typing.List[x509.Certificate],
1250+
self, data: bytes, password: bytes | None
1251+
) -> tuple[
1252+
PrivateKeyTypes | None,
1253+
x509.Certificate | None,
1254+
list[x509.Certificate],
12551255
]:
12561256
pkcs12 = self.load_pkcs12(data, password)
12571257
return (
@@ -1261,7 +1261,7 @@ def load_key_and_certificates_from_pkcs12(
12611261
)
12621262

12631263
def load_pkcs12(
1264-
self, data: bytes, password: typing.Optional[bytes]
1264+
self, data: bytes, password: bytes | None
12651265
) -> PKCS12KeyAndCertificates:
12661266
if password is not None:
12671267
utils._check_byteslike("password", password)
@@ -1337,10 +1337,10 @@ def load_pkcs12(
13371337

13381338
def serialize_key_and_certificates_to_pkcs12(
13391339
self,
1340-
name: typing.Optional[bytes],
1341-
key: typing.Optional[PKCS12PrivateKeyTypes],
1342-
cert: typing.Optional[x509.Certificate],
1343-
cas: typing.Optional[typing.List[_PKCS12CATypes]],
1340+
name: bytes | None,
1341+
key: PKCS12PrivateKeyTypes | None,
1342+
cert: x509.Certificate | None,
1343+
cas: list[_PKCS12CATypes] | None,
13441344
encryption_algorithm: serialization.KeySerializationEncryption,
13451345
) -> bytes:
13461346
password = None
@@ -1503,7 +1503,7 @@ def pkcs7_supported(self) -> bool:
15031503

15041504
def load_pem_pkcs7_certificates(
15051505
self, data: bytes
1506-
) -> typing.List[x509.Certificate]:
1506+
) -> list[x509.Certificate]:
15071507
utils._check_bytes("data", data)
15081508
bio = self._bytes_to_bio(data)
15091509
p7 = self._lib.PEM_read_bio_PKCS7(
@@ -1518,7 +1518,7 @@ def load_pem_pkcs7_certificates(
15181518

15191519
def load_der_pkcs7_certificates(
15201520
self, data: bytes
1521-
) -> typing.List[x509.Certificate]:
1521+
) -> list[x509.Certificate]:
15221522
utils._check_bytes("data", data)
15231523
bio = self._bytes_to_bio(data)
15241524
p7 = self._lib.d2i_PKCS7_bio(bio.bio, self._ffi.NULL)
@@ -1529,7 +1529,7 @@ def load_der_pkcs7_certificates(
15291529
p7 = self._ffi.gc(p7, self._lib.PKCS7_free)
15301530
return self._load_pkcs7_certificates(p7)
15311531

1532-
def _load_pkcs7_certificates(self, p7) -> typing.List[x509.Certificate]:
1532+
def _load_pkcs7_certificates(self, p7) -> list[x509.Certificate]:
15331533
nid = self._lib.OBJ_obj2nid(p7.type)
15341534
self.openssl_assert(nid != self._lib.NID_undef)
15351535
if nid != self._lib.NID_pkcs7_signed:

src/cryptography/hazmat/backends/openssl/ciphers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, backend: Backend, cipher, mode, operation: int) -> None:
2424
self._cipher = cipher
2525
self._mode = mode
2626
self._operation = operation
27-
self._tag: typing.Optional[bytes] = None
27+
self._tag: bytes | None = None
2828

2929
if isinstance(self._cipher, ciphers.BlockCipherAlgorithm):
3030
self._block_size_bytes = self._cipher.block_size // 8
@@ -277,5 +277,5 @@ def authenticate_additional_data(self, data: bytes) -> None:
277277
self._backend.openssl_assert(res != 0)
278278

279279
@property
280-
def tag(self) -> typing.Optional[bytes]:
280+
def tag(self) -> bytes | None:
281281
return self._tag

0 commit comments

Comments
 (0)