Skip to content

Commit 210f5cb

Browse files
authored
Merge pull request #53 from snok/sondrelg/tc005-bugfix
Correct TC005 logic
2 parents 2a465a9 + 417800d commit 210f5cb

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

flake8_type_checking/checker.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ def _in_type_checking_block(self, node: ast.AST) -> bool:
100100
if not self.type_checking_blocks and not self.empty_type_checking_blocks:
101101
return False
102102

103-
# The list of empty type checking blocks is maintained for TC005
104-
# If we find an import within one of these, we simply move it into self.type_checking_blocks
105-
for index, empty_type_checking_block in enumerate(list(self.empty_type_checking_blocks)):
106-
if empty_type_checking_block[0] <= node.lineno <= empty_type_checking_block[1]:
107-
if isinstance(node, (ast.Import, ast.ImportFrom)):
108-
self.type_checking_blocks.append(empty_type_checking_block)
109-
self.empty_type_checking_blocks.pop(index)
110-
else:
111-
return True
112-
113103
return any(
114104
type_checking_block[0] <= node.lineno <= type_checking_block[1]
115105
for type_checking_block in self.type_checking_blocks + self.empty_type_checking_blocks
@@ -126,9 +116,11 @@ def visit_If(self, node: ast.If) -> Any:
126116
# Just set the lineno of the first element in the else block - 1
127117
start_of_else_block = node.orelse[0].lineno - 1
128118

129-
# empty_type_checking_blocks' items are moved to self.type_checking_blocks
130-
# as soon as we discover an import within one of the "empty" blocks
131-
if (node.end_lineno or node.lineno) - node.lineno == 1:
119+
# Type checking blocks that only contain 'pass' are appended to an empty-type-checking-block list
120+
# and flagged with TC005 errors.
121+
if ((node.end_lineno or node.lineno) - node.lineno == 1) and (
122+
len(node.body) == 1 and isinstance(node.body[0], ast.Pass)
123+
):
132124
self.empty_type_checking_blocks.append(
133125
(node.lineno, start_of_else_block or node.end_lineno or node.lineno, node.col_offset)
134126
)

tests/test_tc005.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ class Test:
6868
),
6969
set(),
7070
),
71+
(
72+
textwrap.dedent(
73+
"""
74+
from typing import TYPE_CHECKING
75+
from typing import List
76+
77+
if TYPE_CHECKING:
78+
x: List
79+
"""
80+
),
81+
set(),
82+
),
7183
]
7284

7385

0 commit comments

Comments
 (0)