25
25
from ._compat import IS_PY38_OR_GREATER
26
26
27
27
28
+ # Avoid DeprecationWarnings under Python 3.12 and up
29
+ if IS_PY38_OR_GREATER :
30
+ astStr = ast .Constant
31
+ astNum = ast .Constant
32
+ else : # pragma: no cover
33
+ astStr = ast .Str
34
+ astNum = ast .Num
35
+
28
36
# For AugAssign the operator must be converted to a string.
29
37
IOPERATOR_TO_STR = {
30
38
ast .Add : '+=' ,
@@ -272,7 +280,7 @@ def gen_unpack_spec(self, tpl):
272
280
"""
273
281
spec = ast .Dict (keys = [], values = [])
274
282
275
- spec .keys .append (ast . Str ('childs' ))
283
+ spec .keys .append (astStr ('childs' ))
276
284
spec .values .append (ast .Tuple ([], ast .Load ()))
277
285
278
286
# starred elements in a sequence do not contribute into the min_len.
@@ -292,12 +300,12 @@ def gen_unpack_spec(self, tpl):
292
300
293
301
elif isinstance (val , ast .Tuple ):
294
302
el = ast .Tuple ([], ast .Load ())
295
- el .elts .append (ast . Num (idx - offset ))
303
+ el .elts .append (astNum (idx - offset ))
296
304
el .elts .append (self .gen_unpack_spec (val ))
297
305
spec .values [0 ].elts .append (el )
298
306
299
- spec .keys .append (ast . Str ('min_len' ))
300
- spec .values .append (ast . Num (min_len ))
307
+ spec .keys .append (astStr ('min_len' ))
308
+ spec .values .append (astNum (min_len ))
301
309
302
310
return spec
303
311
@@ -903,7 +911,7 @@ def visit_Attribute(self, node):
903
911
node = self .node_contents_visit (node )
904
912
new_node = ast .Call (
905
913
func = ast .Name ('_getattr_' , ast .Load ()),
906
- args = [node .value , ast . Str (node .attr )],
914
+ args = [node .value , astStr (node .attr )],
907
915
keywords = [])
908
916
909
917
copy_locations (new_node , node )
@@ -1107,7 +1115,7 @@ def visit_AugAssign(self, node):
1107
1115
value = ast .Call (
1108
1116
func = ast .Name ('_inplacevar_' , ast .Load ()),
1109
1117
args = [
1110
- ast . Str (IOPERATOR_TO_STR [type (node .op )]),
1118
+ astStr (IOPERATOR_TO_STR [type (node .op )]),
1111
1119
ast .Name (node .target .id , ast .Load ()),
1112
1120
node .value
1113
1121
],
0 commit comments