Skip to content

Commit eef57df

Browse files
authored
fix: Make sure to visit returns not just the argument annotations
1 parent 40c43b5 commit eef57df

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

flake8_type_checking/checker.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ def visit_FunctionDef(self, node: FunctionDef) -> None:
291291
if hasattr(argument, 'annotation') and argument.annotation:
292292
self.visit(argument.annotation)
293293

294+
if node.returns:
295+
self.visit(node.returns)
296+
294297
def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> None:
295298
"""Remove and map function arguments and returns."""
296299
if self._function_is_wrapped_by_validate_arguments(node):
@@ -299,6 +302,9 @@ def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> None:
299302
if hasattr(argument, 'annotation') and argument.annotation:
300303
self.visit(argument.annotation)
301304

305+
if node.returns:
306+
self.visit(node.returns)
307+
302308

303309
class SQLAlchemyAnnotationVisitor(AnnotationVisitor):
304310
"""Adds any names in the annotation to mapped names."""
@@ -548,24 +554,13 @@ def handle_fastapi_decorator(self, node: AsyncFunctionDef | FunctionDef) -> None
548554
549555
To achieve this, we just visit the annotations to register them as "uses".
550556
"""
551-
for path in [node.args.args, node.args.kwonlyargs]:
557+
for path in [node.args.args, node.args.kwonlyargs, node.args.posonlyargs]:
552558
for argument in path:
553559
if hasattr(argument, 'annotation') and argument.annotation:
554560
self.visit(argument.annotation)
555-
if (
556-
hasattr(node.args, 'kwarg')
557-
and node.args.kwarg
558-
and hasattr(node.args.kwarg, 'annotation')
559-
and node.args.kwarg.annotation
560-
):
561-
self.visit(node.args.kwarg.annotation)
562-
if (
563-
hasattr(node.args, 'vararg')
564-
and node.args.vararg
565-
and hasattr(node.args.vararg, 'annotation')
566-
and node.args.vararg.annotation
567-
):
568-
self.visit(node.args.vararg.annotation)
561+
562+
if node.returns:
563+
self.visit(node.returns)
569564

570565

571566
class FunctoolsSingledispatchMixin:
@@ -627,6 +622,9 @@ def handle_singledispatch_decorator(self, node: FunctionDef | AsyncFunctionDef)
627622
if hasattr(argument, 'annotation') and argument.annotation:
628623
self.visit(argument.annotation)
629624

625+
if node.returns:
626+
self.visit(node.returns)
627+
630628

631629
@dataclass
632630
class ImportName:

tests/test_fastapi_decorators.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import pytest
1111

12-
from flake8_type_checking.constants import TC002
1312
from tests.conftest import _get_error
1413

1514
defaults = {'type_checking_fastapi_enabled': True}
@@ -56,9 +55,7 @@ def test_api_router_decorated_function_return_type(fdef):
5655
return None
5756
'''
5857
)
59-
assert _get_error(example, error_code_filter='TC001,TC002,TC003', **defaults) == {
60-
'5:0 ' + TC002.format(module='app.types.CustomType')
61-
}
58+
assert _get_error(example, error_code_filter='TC001,TC002,TC003', **defaults) == set()
6259

6360

6461
@pytest.mark.parametrize('fdef', ['def', 'async def'])

0 commit comments

Comments
 (0)