38
38
import ast
39
39
import configparser
40
40
import pathlib
41
+ import sys
41
42
import tempfile
42
43
from typing import TYPE_CHECKING , Callable , Iterator , List , Optional , Tuple , Type
43
44
@@ -92,10 +93,20 @@ def mode_is_binary(mode: ast.AST) -> Optional[bool]:
92
93
93
94
if isinstance (mode , ast .Constant ): # pragma: no cover (<py38)
94
95
return 'b' in mode .value
95
- elif isinstance (mode , ast .Str ): # pragma: no cover (py38+)
96
- return 'b' in mode .s
97
96
else :
98
- return None
97
+ if sys .version_info < (3 , 12 ): # pragma: no cover (py312+)
98
+ if isinstance (mode , ast .Str ): # pragma: no cover (py38+)
99
+ return 'b' in mode .s
100
+
101
+ return None
102
+
103
+
104
+ if sys .version_info < (3 , 12 ): # pragma: no cover (py312+)
105
+ _constant_nameconstant = (ast .Constant , ast .NameConstant )
106
+ _skip_312_deprecations = False
107
+ else : # pragma: no cover (<py312)
108
+ _constant_nameconstant = ast .Constant
109
+ _skip_312_deprecations = True
99
110
100
111
101
112
class Visitor (flake8_helper .Visitor ):
@@ -135,7 +146,7 @@ def check_open_encoding(self, node: ast.Call) -> None:
135
146
if "encoding" not in kwargs :
136
147
self .report_error (node , ENC003 if unknown_mode else ENC001 )
137
148
138
- elif isinstance (kwargs ["encoding" ], ( ast . Constant , ast . NameConstant ) ):
149
+ elif isinstance (kwargs ["encoding" ], _constant_nameconstant ):
139
150
if kwargs ["encoding" ].value is None :
140
151
self .report_error (node , ENC004 if unknown_mode else ENC002 )
141
152
@@ -157,11 +168,12 @@ def visit_Call(self, node: ast.Call) -> None: # noqa: D102
157
168
self .check_open_encoding (node )
158
169
return
159
170
160
- if isinstance (node .func .value , ast .Str ): # pragma: no cover
161
- # Attribute on a string
162
- return self .generic_visit (node )
171
+ if not _skip_312_deprecations : # pragma: no cover (py312+)
172
+ if isinstance (node .func .value , ast .Str ): # pragma: no cover
173
+ # Attribute on a string
174
+ return self .generic_visit (node )
163
175
164
- elif isinstance (node .func .value , ast .BinOp ): # pragma: no cover
176
+ if isinstance (node .func .value , ast .BinOp ): # pragma: no cover
165
177
# TODO
166
178
# Expressions such as (tmp_pathplus / "code.py").write_text(example_source)
167
179
return self .generic_visit (node )
@@ -226,7 +238,7 @@ def check_configparser_encoding(self, node: ast.Call) -> None:
226
238
if "encoding" not in kwargs :
227
239
self .report_error (node , ENC011 )
228
240
229
- elif isinstance (kwargs ["encoding" ], ( ast . Constant , ast . NameConstant ) ):
241
+ elif isinstance (kwargs ["encoding" ], _constant_nameconstant ):
230
242
if kwargs ["encoding" ].value is None :
231
243
self .report_error (node , ENC012 )
232
244
@@ -275,7 +287,7 @@ def check_pathlib_encoding(self, node: ast.Call, method_name: str) -> None:
275
287
if "encoding" not in kwargs :
276
288
self .report_error (node , no_encoding )
277
289
278
- elif isinstance (kwargs ["encoding" ], ( ast . Constant , ast . NameConstant ) ):
290
+ elif isinstance (kwargs ["encoding" ], _constant_nameconstant ):
279
291
if kwargs ["encoding" ].value is None :
280
292
self .report_error (node , encoding_none )
281
293
@@ -295,11 +307,12 @@ def visit_Call(self, node: ast.Call) -> None: # noqa: D102
295
307
self .check_open_encoding (node )
296
308
return
297
309
298
- if isinstance (node .func .value , ast .Str ): # pragma: no cover
299
- # Attribute on a string
300
- return self .generic_visit (node )
310
+ if not _skip_312_deprecations : # pragma: no cover (py312+)
311
+ if isinstance (node .func .value , ast .Str ): # pragma: no cover
312
+ # Attribute on a string
313
+ return self .generic_visit (node )
301
314
302
- elif isinstance (node .func .value , ast .BinOp ): # pragma: no cover
315
+ if isinstance (node .func .value , ast .BinOp ): # pragma: no cover
303
316
# TODO
304
317
# Expressions such as (tmp_pathplus / "code.py").write_text(example_source)
305
318
return self .generic_visit (node )
0 commit comments