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

[flake8-type-checking] Fix false positives for typing.Annotated #14311

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ def f():

def func(self) -> DataFrame | list[Series]:
pass


def f():
from typing import Annotated

from fastapi import Depends

from .foo import get_foo

def test_annotated_non_typing_reference(user: Annotated[str, Depends(get_foo)]):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ def f():

def test_optional_literal_no_removal(arg: Optional[Literal["red", "blue"]]):
pass


def f():
from typing import Annotated

from fastapi import Depends

from .foo import get_foo

def test_annotated_non_typing_reference(user: Annotated[str, Depends(get_foo)]):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ def f():
def test_attribute_typing_literal(arg: models.AbstractBaseUser[Literal["admin"]]):
pass


def f():
from typing import Annotated

from fastapi import Depends

from .foo import get_foo

def test_annotated_non_typing_reference(user: Annotated[str, Depends(get_foo)]):
pass

11 changes: 7 additions & 4 deletions crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ use crate::rules::flake8_type_checking::settings::Settings;
/// context (with quoting enabled).
pub(crate) fn is_typing_reference(reference: &ResolvedReference, settings: &Settings) -> bool {
reference.in_type_checking_block()
|| reference.in_typing_only_annotation()
|| reference.in_complex_string_type_definition()
|| reference.in_simple_string_type_definition()
|| (settings.quote_annotations && reference.in_runtime_evaluated_annotation())
// if we're not in a type checking block, we necessarily need to be within a
// type definition to be considered a typing reference
|| (reference.in_type_definition()
&& (reference.in_typing_only_annotation()
|| reference.in_complex_string_type_definition()
|| reference.in_simple_string_type_definition()
|| (settings.quote_annotations && reference.in_runtime_evaluated_annotation())))
}

/// Returns `true` if the [`Binding`] represents a runtime-required import.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ quote.py:89:24: TCH002 [*] Move third-party import `pandas.DataFrame` into a typ
91 |- def func(self) -> DataFrame | list[Series]:
94 |+ def func(self) -> "DataFrame | list[Series]":
92 95 | pass
93 96 |
94 97 |

quote.py:89:35: TCH002 [*] Move third-party import `pandas.Series` into a type-checking block
|
Expand Down Expand Up @@ -407,3 +409,5 @@ quote.py:89:35: TCH002 [*] Move third-party import `pandas.Series` into a type-c
91 |- def func(self) -> DataFrame | list[Series]:
94 |+ def func(self) -> "DataFrame | list[Series]":
92 95 | pass
93 96 |
94 97 |
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,5 @@ quote3.py:40:37: TCH002 [*] Move third-party import `django.contrib.auth.models`
42 |- def test_attribute_typing_literal(arg: models.AbstractBaseUser[Literal["admin"]]):
45 |+ def test_attribute_typing_literal(arg: 'models.AbstractBaseUser[Literal["admin"]]'):
43 46 | pass
44 47 |
44 47 |
45 48 |
5 changes: 5 additions & 0 deletions crates/ruff_python_semantic/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl ResolvedReference {
.intersects(SemanticModelFlags::DEFERRED_TYPE_DEFINITION)
}

/// Return `true` if the context is in any kind of type definition.
pub const fn in_type_definition(&self) -> bool {
self.flags.intersects(SemanticModelFlags::TYPE_DEFINITION)
}

/// Return `true` if the context is in a type-checking block.
pub const fn in_type_checking_block(&self) -> bool {
self.flags
Expand Down
Loading