Skip to content

Commit c9a088f

Browse files
committed
Don't use deprecated node.s on Python 3.8+
1 parent a6c60c2 commit c9a088f

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

flake8_strftime/__init__.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
STRFTIME001 = "STRFTIME001 Linux-specific strftime code used."
5252
STRFTIME002 = "STRFTIME002 Windows-specific strftime code used."
5353

54+
if sys.version_info >= (3, 12): # pragma: no cover (<py312)
55+
StrOrConstant = ast.Constant
56+
else: # pragma: no cover (py312+)
57+
StrOrConstant = Union[ast.Str, ast.Constant]
58+
5459

5560
class Visitor(flake8_helper.Visitor):
5661
"""
@@ -85,34 +90,48 @@ def visit_Constant(self, node: ast.Constant) -> None:
8590
:param node: The node being visited
8691
"""
8792

88-
if not isinstance(node.s, str):
89-
return
93+
if sys.version_info < (3, 8): # pragma: no cover (PY38+)
94+
if not isinstance(node.s, str):
95+
return
96+
else: # pragma: no cover (<PY38)
97+
if not isinstance(node.value, str):
98+
return
9099

91100
self._check_linux(node)
92101
self._check_windows(node)
93102

94-
def _check_linux(self, node: Union[ast.Str, ast.Constant]) -> None:
103+
def _check_linux(self, node: StrOrConstant) -> None:
95104
"""
96105
Perform the check for Linux-specific codes.
97106
98107
:param node: The node being visited
99108
"""
100109

101-
for match in self._linux_re.finditer(node.s): # pylint: disable=use-list-copy
110+
if sys.version_info < (3, 8): # pragma: no cover (PY38+)
111+
node_value = node.s
112+
else: # pragma: no cover (<PY38)
113+
node_value = node.value
114+
115+
for match in self._linux_re.finditer(node_value): # pylint: disable=use-list-copy
102116
self.errors.append((
103117
node.lineno,
104118
node.col_offset + match.span()[0],
105119
STRFTIME001, # pylint: disable=loop-global-usage
106120
))
107121

108-
def _check_windows(self, node: Union[ast.Str, ast.Constant]) -> None:
122+
def _check_windows(self, node: StrOrConstant) -> None:
109123
"""
110124
Perform the check for Windows-specific codes.
111125
112126
:param node: The node being visited
113127
"""
114128

115-
for match in self._win_re.finditer(node.s): # pylint: disable=use-list-copy
129+
if sys.version_info < (3, 8): # pragma: no cover (PY38+)
130+
node_value = node.s
131+
else: # pragma: no cover (<PY38)
132+
node_value = node.value
133+
134+
for match in self._win_re.finditer(node_value): # pylint: disable=use-list-copy
116135
self.errors.append((
117136
node.lineno,
118137
node.col_offset + match.span()[0],

0 commit comments

Comments
 (0)