Skip to content

Commit 343ac13

Browse files
authored
enable mypy over tests (pyca#5721)
* clean mypy with tests dir * remove most no_type_check annotations * le sigh * remove unneeded comments
1 parent 4883791 commit 343ac13

35 files changed

Lines changed: 309 additions & 186 deletions

tests/doubles.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,36 @@
33
# for complete details.
44

55

6-
from cryptography import utils
76
from cryptography.hazmat.primitives import hashes, serialization
87
from cryptography.hazmat.primitives.asymmetric import padding
98
from cryptography.hazmat.primitives.ciphers import CipherAlgorithm
109
from cryptography.hazmat.primitives.ciphers.modes import Mode
1110

1211

13-
@utils.register_interface(CipherAlgorithm)
14-
class DummyCipherAlgorithm(object):
12+
class DummyCipherAlgorithm(CipherAlgorithm):
1513
name = "dummy-cipher"
1614
block_size = 128
1715
key_size = None
1816

1917

20-
@utils.register_interface(Mode)
21-
class DummyMode(object):
18+
class DummyMode(Mode):
2219
name = "dummy-mode"
2320

2421
def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
2522
pass
2623

2724

28-
@utils.register_interface(hashes.HashAlgorithm)
29-
class DummyHashAlgorithm(object):
25+
class DummyHashAlgorithm(hashes.HashAlgorithm):
3026
name = "dummy-hash"
3127
block_size = None
32-
digest_size = None
28+
digest_size = 32
3329

3430

35-
@utils.register_interface(serialization.KeySerializationEncryption)
36-
class DummyKeySerializationEncryption(object):
31+
class DummyKeySerializationEncryption(
32+
serialization.KeySerializationEncryption
33+
):
3734
pass
3835

3936

40-
@utils.register_interface(padding.AsymmetricPadding)
41-
class DummyAsymmetricPadding(object):
37+
class DummyAsymmetricPadding(padding.AsymmetricPadding):
4238
name = "dummy-padding"

tests/hazmat/backends/test_openssl_memleak.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def assert_no_memory_leaks(s, argv=[]):
147147
stdout=subprocess.PIPE,
148148
stderr=subprocess.PIPE,
149149
)
150+
assert proc.stdout is not None
151+
assert proc.stderr is not None
150152
try:
151153
proc.wait()
152154
if proc.returncode == 255:

tests/hazmat/primitives/test_aead.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626

2727

28-
class FakeData(object):
28+
class FakeData(bytes):
2929
def __len__(self):
3030
return 2 ** 32 + 1
3131

@@ -71,7 +71,7 @@ def test_generate_key(self):
7171

7272
def test_bad_key(self, backend):
7373
with pytest.raises(TypeError):
74-
ChaCha20Poly1305(object())
74+
ChaCha20Poly1305(object()) # type:ignore[arg-type]
7575

7676
with pytest.raises(ValueError):
7777
ChaCha20Poly1305(b"0" * 31)
@@ -215,7 +215,7 @@ def test_invalid_tag_length(self, backend):
215215
AESCCM(key, tag_length=2)
216216

217217
with pytest.raises(TypeError):
218-
AESCCM(key, tag_length="notanint")
218+
AESCCM(key, tag_length="notanint") # type:ignore[arg-type]
219219

220220
def test_invalid_nonce_length(self, backend):
221221
key = AESCCM.generate_key(128)
@@ -298,14 +298,14 @@ def test_params_not_bytes(self, nonce, data, associated_data, backend):
298298

299299
def test_bad_key(self, backend):
300300
with pytest.raises(TypeError):
301-
AESCCM(object())
301+
AESCCM(object()) # type:ignore[arg-type]
302302

303303
with pytest.raises(ValueError):
304304
AESCCM(b"0" * 31)
305305

306306
def test_bad_generate_key(self, backend):
307307
with pytest.raises(TypeError):
308-
AESCCM.generate_key(object())
308+
AESCCM.generate_key(object()) # type:ignore[arg-type]
309309

310310
with pytest.raises(ValueError):
311311
AESCCM.generate_key(129)
@@ -430,14 +430,14 @@ def test_invalid_nonce_length(self, length, backend):
430430

431431
def test_bad_key(self, backend):
432432
with pytest.raises(TypeError):
433-
AESGCM(object())
433+
AESGCM(object()) # type:ignore[arg-type]
434434

435435
with pytest.raises(ValueError):
436436
AESGCM(b"0" * 31)
437437

438438
def test_bad_generate_key(self, backend):
439439
with pytest.raises(TypeError):
440-
AESGCM.generate_key(object())
440+
AESGCM.generate_key(object()) # type:ignore[arg-type]
441441

442442
with pytest.raises(ValueError):
443443
AESGCM.generate_key(129)

tests/hazmat/primitives/test_block.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,28 @@ def test_gcm(self):
194194
class TestModesRequireBytes(object):
195195
def test_cbc(self):
196196
with pytest.raises(TypeError):
197-
modes.CBC([1] * 16)
197+
modes.CBC([1] * 16) # type:ignore[arg-type]
198198

199199
def test_cfb(self):
200200
with pytest.raises(TypeError):
201-
modes.CFB([1] * 16)
201+
modes.CFB([1] * 16) # type:ignore[arg-type]
202202

203203
def test_cfb8(self):
204204
with pytest.raises(TypeError):
205-
modes.CFB8([1] * 16)
205+
modes.CFB8([1] * 16) # type:ignore[arg-type]
206206

207207
def test_ofb(self):
208208
with pytest.raises(TypeError):
209-
modes.OFB([1] * 16)
209+
modes.OFB([1] * 16) # type:ignore[arg-type]
210210

211211
def test_ctr(self):
212212
with pytest.raises(TypeError):
213-
modes.CTR([1] * 16)
213+
modes.CTR([1] * 16) # type:ignore[arg-type]
214214

215215
def test_gcm_iv(self):
216216
with pytest.raises(TypeError):
217-
modes.GCM([1] * 16)
217+
modes.GCM([1] * 16) # type:ignore[arg-type]
218218

219219
def test_gcm_tag(self):
220220
with pytest.raises(TypeError):
221-
modes.GCM(b"\x00" * 16, [1] * 16)
221+
modes.GCM(b"\x00" * 16, [1] * 16) # type:ignore[arg-type]

tests/hazmat/primitives/test_chacha20.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def test_invalid_nonce(self):
6666
algorithms.ChaCha20(b"0" * 32, b"0")
6767

6868
with pytest.raises(TypeError):
69-
algorithms.ChaCha20(b"0" * 32, object())
69+
algorithms.ChaCha20(b"0" * 32, object()) # type:ignore[arg-type]
7070

7171
def test_invalid_key_type(self):
7272
with pytest.raises(TypeError, match="key must be bytes"):
73-
algorithms.ChaCha20("0" * 32, b"0" * 16)
73+
algorithms.ChaCha20("0" * 32, b"0" * 16) # type:ignore[arg-type]

tests/hazmat/primitives/test_ciphers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_invalid_key_size(self):
4545

4646
def test_invalid_key_type(self):
4747
with pytest.raises(TypeError, match="key must be bytes"):
48-
AES("0" * 32)
48+
AES("0" * 32) # type: ignore[arg-type]
4949

5050

5151
class TestAESXTS(object):
@@ -59,7 +59,7 @@ def test_invalid_key_size_with_mode(self, mode, backend):
5959

6060
def test_xts_tweak_not_bytes(self):
6161
with pytest.raises(TypeError):
62-
modes.XTS(32)
62+
modes.XTS(32) # type: ignore[arg-type]
6363

6464
def test_xts_tweak_too_small(self):
6565
with pytest.raises(ValueError):
@@ -93,7 +93,7 @@ def test_invalid_key_size(self):
9393

9494
def test_invalid_key_type(self):
9595
with pytest.raises(TypeError, match="key must be bytes"):
96-
Camellia("0" * 32)
96+
Camellia("0" * 32) # type: ignore[arg-type]
9797

9898

9999
class TestTripleDES(object):
@@ -108,7 +108,7 @@ def test_invalid_key_size(self):
108108

109109
def test_invalid_key_type(self):
110110
with pytest.raises(TypeError, match="key must be bytes"):
111-
TripleDES("0" * 16)
111+
TripleDES("0" * 16) # type: ignore[arg-type]
112112

113113

114114
class TestBlowfish(object):
@@ -126,7 +126,7 @@ def test_invalid_key_size(self):
126126

127127
def test_invalid_key_type(self):
128128
with pytest.raises(TypeError, match="key must be bytes"):
129-
Blowfish("0" * 8)
129+
Blowfish("0" * 8) # type: ignore[arg-type]
130130

131131

132132
class TestCAST5(object):
@@ -144,7 +144,7 @@ def test_invalid_key_size(self):
144144

145145
def test_invalid_key_type(self):
146146
with pytest.raises(TypeError, match="key must be bytes"):
147-
CAST5("0" * 10)
147+
CAST5("0" * 10) # type: ignore[arg-type]
148148

149149

150150
class TestARC4(object):
@@ -170,7 +170,7 @@ def test_invalid_key_size(self):
170170

171171
def test_invalid_key_type(self):
172172
with pytest.raises(TypeError, match="key must be bytes"):
173-
ARC4("0" * 10)
173+
ARC4("0" * 10) # type: ignore[arg-type]
174174

175175

176176
class TestIDEA(object):
@@ -184,7 +184,7 @@ def test_invalid_key_size(self):
184184

185185
def test_invalid_key_type(self):
186186
with pytest.raises(TypeError, match="key must be bytes"):
187-
IDEA("0" * 16)
187+
IDEA("0" * 16) # type: ignore[arg-type]
188188

189189

190190
class TestSEED(object):
@@ -198,7 +198,7 @@ def test_invalid_key_size(self):
198198

199199
def test_invalid_key_type(self):
200200
with pytest.raises(TypeError, match="key must be bytes"):
201-
SEED("0" * 16)
201+
SEED("0" * 16) # type: ignore[arg-type]
202202

203203

204204
def test_invalid_backend():

tests/hazmat/primitives/test_cmac.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_aes_verify(self, backend, params):
8080

8181
cmac = CMAC(AES(binascii.unhexlify(key)), backend)
8282
cmac.update(binascii.unhexlify(message))
83-
assert cmac.verify(binascii.unhexlify(output)) is None
83+
cmac.verify(binascii.unhexlify(output))
8484

8585
@pytest.mark.supported(
8686
only_if=lambda backend: backend.cmac_algorithm_supported(
@@ -122,7 +122,7 @@ def test_3des_verify(self, backend, params):
122122

123123
cmac = CMAC(TripleDES(binascii.unhexlify(key)), backend)
124124
cmac.update(binascii.unhexlify(message))
125-
assert cmac.verify(binascii.unhexlify(output)) is None
125+
cmac.verify(binascii.unhexlify(output))
126126

127127
@pytest.mark.supported(
128128
only_if=lambda backend: backend.cmac_algorithm_supported(
@@ -145,7 +145,7 @@ def test_invalid_verify(self, backend):
145145
def test_invalid_algorithm(self, backend):
146146
key = b"0102030405"
147147
with pytest.raises(TypeError):
148-
CMAC(ARC4(key), backend)
148+
CMAC(ARC4(key), backend) # type: ignore[arg-type]
149149

150150
@pytest.mark.supported(
151151
only_if=lambda backend: backend.cmac_algorithm_supported(
@@ -181,10 +181,10 @@ def test_verify_reject_unicode(self, backend):
181181
cmac = CMAC(AES(key), backend)
182182

183183
with pytest.raises(TypeError):
184-
cmac.update("")
184+
cmac.update("") # type: ignore[arg-type]
185185

186186
with pytest.raises(TypeError):
187-
cmac.verify("")
187+
cmac.verify("") # type: ignore[arg-type]
188188

189189
@pytest.mark.supported(
190190
only_if=lambda backend: backend.cmac_algorithm_supported(

tests/hazmat/primitives/test_constant_time.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
class TestConstantTimeBytesEq(object):
1212
def test_reject_unicode(self):
1313
with pytest.raises(TypeError):
14-
constant_time.bytes_eq(b"foo", "foo")
14+
constant_time.bytes_eq(b"foo", "foo") # type: ignore[arg-type]
1515

1616
with pytest.raises(TypeError):
17-
constant_time.bytes_eq("foo", b"foo")
17+
constant_time.bytes_eq("foo", b"foo") # type: ignore[arg-type]
1818

1919
with pytest.raises(TypeError):
20-
constant_time.bytes_eq("foo", "foo")
20+
constant_time.bytes_eq("foo", "foo") # type: ignore[arg-type]
2121

2222
def test_compares(self):
2323
assert constant_time.bytes_eq(b"foo", b"foo") is True

tests/hazmat/primitives/test_dh.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import binascii
77
import itertools
88
import os
9+
import typing
910

1011
import pytest
1112

@@ -203,7 +204,7 @@ def test_convert_to_numbers(self, backend, with_q):
203204
)[0]
204205
p = int(vector["p"], 16)
205206
g = int(vector["g"], 16)
206-
q = int(vector["q"], 16)
207+
q: typing.Optional[int] = int(vector["q"], 16)
207208
else:
208209
parameters = backend.generate_dh_private_key_and_parameters(2, 512)
209210

@@ -388,6 +389,7 @@ def test_load_256bit_key_from_pkcs8(self, backend):
388389
mode="rb",
389390
)
390391
key = serialization.load_pem_private_key(data, None, backend)
392+
assert isinstance(key, dh.DHPrivateKey)
391393
assert key.key_size == 256
392394

393395
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)