Skip to content

Commit 07ab176

Browse files
Refs #1662: Condition usedforsecurity support on Py3.9+ (#1665)
1 parent 257f4e7 commit 07ab176

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

astroid/brain/brain_hashlib.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from astroid.brain.helpers import register_module_extender
66
from astroid.builder import parse
7+
from astroid.const import PY39_PLUS
78
from astroid.manager import AstroidManager
89

910

1011
def _hashlib_transform():
11-
signature = "value='', usedforsecurity=True"
12+
maybe_usedforsecurity = ", usedforsecurity=True" if PY39_PLUS else ""
13+
signature = f"value=''{maybe_usedforsecurity}"
1214
template = """
1315
class %(name)s(object):
1416
def __init__(self, %(signature)s): pass
@@ -32,12 +34,12 @@ def digest_size(self):
3234
algorithms_with_signature = dict.fromkeys(
3335
["md5", "sha1", "sha224", "sha256", "sha384", "sha512"], signature
3436
)
35-
blake2b_signature = "data=b'', *, digest_size=64, key=b'', salt=b'', \
37+
blake2b_signature = f"data=b'', *, digest_size=64, key=b'', salt=b'', \
3638
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
37-
node_depth=0, inner_size=0, last_node=False, usedforsecurity=True"
38-
blake2s_signature = "data=b'', *, digest_size=32, key=b'', salt=b'', \
39+
node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
40+
blake2s_signature = f"data=b'', *, digest_size=32, key=b'', salt=b'', \
3941
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
40-
node_depth=0, inner_size=0, last_node=False, usedforsecurity=True"
42+
node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
4143
new_algorithms = dict.fromkeys(
4244
["sha3_224", "sha3_256", "sha3_384", "sha3_512", "shake_128", "shake_256"],
4345
signature,

tests/unittest_brain.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import astroid
1919
from astroid import MANAGER, bases, builder, nodes, objects, test_utils, util
2020
from astroid.bases import Instance
21+
from astroid.const import PY39_PLUS
2122
from astroid.exceptions import (
2223
AttributeInferenceError,
2324
InferenceError,
@@ -75,8 +76,11 @@ def _assert_hashlib_class(self, class_obj: ClassDef) -> None:
7576
self.assertIn("hexdigest", class_obj)
7677
self.assertIn("block_size", class_obj)
7778
self.assertIn("digest_size", class_obj)
78-
self.assertEqual(len(class_obj["__init__"].args.args), 3)
79-
self.assertEqual(len(class_obj["__init__"].args.defaults), 2)
79+
# usedforsecurity was added in Python 3.9, see 8e7174a9
80+
self.assertEqual(len(class_obj["__init__"].args.args), 3 if PY39_PLUS else 2)
81+
self.assertEqual(
82+
len(class_obj["__init__"].args.defaults), 2 if PY39_PLUS else 1
83+
)
8084
self.assertEqual(len(class_obj["update"].args.args), 2)
8185
self.assertEqual(len(class_obj["digest"].args.args), 1)
8286
self.assertEqual(len(class_obj["hexdigest"].args.args), 1)

0 commit comments

Comments
 (0)