diff --git a/flake8_encodings/__init__.py b/flake8_encodings/__init__.py index a650e9b..5b8924f 100644 --- a/flake8_encodings/__init__.py +++ b/flake8_encodings/__init__.py @@ -38,6 +38,7 @@ import ast import configparser import pathlib +import sys import tempfile from typing import TYPE_CHECKING, Callable, Iterator, List, Optional, Tuple, Type @@ -92,10 +93,20 @@ def mode_is_binary(mode: ast.AST) -> Optional[bool]: if isinstance(mode, ast.Constant): # pragma: no cover ( None: if "encoding" not in kwargs: self.report_error(node, ENC003 if unknown_mode else ENC001) - elif isinstance(kwargs["encoding"], (ast.Constant, ast.NameConstant)): + elif isinstance(kwargs["encoding"], _constant_nameconstant): if kwargs["encoding"].value is None: self.report_error(node, ENC004 if unknown_mode else ENC002) @@ -157,11 +168,12 @@ def visit_Call(self, node: ast.Call) -> None: # noqa: D102 self.check_open_encoding(node) return - if isinstance(node.func.value, ast.Str): # pragma: no cover - # Attribute on a string - return self.generic_visit(node) + if not _skip_312_deprecations: # pragma: no cover (py312+) + if isinstance(node.func.value, ast.Str): # pragma: no cover + # Attribute on a string + return self.generic_visit(node) - elif isinstance(node.func.value, ast.BinOp): # pragma: no cover + if isinstance(node.func.value, ast.BinOp): # pragma: no cover # TODO # Expressions such as (tmp_pathplus / "code.py").write_text(example_source) return self.generic_visit(node) @@ -226,7 +238,7 @@ def check_configparser_encoding(self, node: ast.Call) -> None: if "encoding" not in kwargs: self.report_error(node, ENC011) - elif isinstance(kwargs["encoding"], (ast.Constant, ast.NameConstant)): + elif isinstance(kwargs["encoding"], _constant_nameconstant): if kwargs["encoding"].value is None: self.report_error(node, ENC012) @@ -275,7 +287,7 @@ def check_pathlib_encoding(self, node: ast.Call, method_name: str) -> None: if "encoding" not in kwargs: self.report_error(node, no_encoding) - elif isinstance(kwargs["encoding"], (ast.Constant, ast.NameConstant)): + elif isinstance(kwargs["encoding"], _constant_nameconstant): if kwargs["encoding"].value is None: self.report_error(node, encoding_none) @@ -295,11 +307,12 @@ def visit_Call(self, node: ast.Call) -> None: # noqa: D102 self.check_open_encoding(node) return - if isinstance(node.func.value, ast.Str): # pragma: no cover - # Attribute on a string - return self.generic_visit(node) + if not _skip_312_deprecations: # pragma: no cover (py312+) + if isinstance(node.func.value, ast.Str): # pragma: no cover + # Attribute on a string + return self.generic_visit(node) - elif isinstance(node.func.value, ast.BinOp): # pragma: no cover + if isinstance(node.func.value, ast.BinOp): # pragma: no cover # TODO # Expressions such as (tmp_pathplus / "code.py").write_text(example_source) return self.generic_visit(node)