mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
[flake8-type-checking] Fix false positives for typing.Annotated
(#14311)
This commit is contained in:
parent
f789b12705
commit
89aa804b2d
7 changed files with 51 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
||||
|
@ -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 |
|
||||
|
|
|
@ -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 |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue