Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure to visit returns not just the argument annotations #198

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions flake8_type_checking/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ def visit_FunctionDef(self, node: FunctionDef) -> None:
if hasattr(argument, 'annotation') and argument.annotation:
self.visit(argument.annotation)

if node.returns:
self.visit(node.returns)

def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> None:
"""Remove and map function arguments and returns."""
if self._function_is_wrapped_by_validate_arguments(node):
Expand All @@ -299,6 +302,9 @@ def visit_AsyncFunctionDef(self, node: AsyncFunctionDef) -> None:
if hasattr(argument, 'annotation') and argument.annotation:
self.visit(argument.annotation)

if node.returns:
self.visit(node.returns)


class SQLAlchemyAnnotationVisitor(AnnotationVisitor):
"""Adds any names in the annotation to mapped names."""
Expand Down Expand Up @@ -548,24 +554,13 @@ def handle_fastapi_decorator(self, node: AsyncFunctionDef | FunctionDef) -> None

To achieve this, we just visit the annotations to register them as "uses".
"""
for path in [node.args.args, node.args.kwonlyargs]:
for path in [node.args.args, node.args.kwonlyargs, node.args.posonlyargs]:
for argument in path:
if hasattr(argument, 'annotation') and argument.annotation:
self.visit(argument.annotation)
if (
hasattr(node.args, 'kwarg')
and node.args.kwarg
and hasattr(node.args.kwarg, 'annotation')
and node.args.kwarg.annotation
):
self.visit(node.args.kwarg.annotation)
if (
hasattr(node.args, 'vararg')
and node.args.vararg
and hasattr(node.args.vararg, 'annotation')
and node.args.vararg.annotation
):
self.visit(node.args.vararg.annotation)

if node.returns:
self.visit(node.returns)


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

if node.returns:
self.visit(node.returns)


@dataclass
class ImportName:
Expand Down
5 changes: 1 addition & 4 deletions tests/test_fastapi_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import pytest

from flake8_type_checking.constants import TC002
from tests.conftest import _get_error

defaults = {'type_checking_fastapi_enabled': True}
Expand Down Expand Up @@ -56,9 +55,7 @@ def test_api_router_decorated_function_return_type(fdef):
return None
'''
)
assert _get_error(example, error_code_filter='TC001,TC002,TC003', **defaults) == {
'5:0 ' + TC002.format(module='app.types.CustomType')
}
assert _get_error(example, error_code_filter='TC001,TC002,TC003', **defaults) == set()


@pytest.mark.parametrize('fdef', ['def', 'async def'])
Expand Down
Loading