forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand scope of
quoted-annotation
rule (astral-sh#5766)
## Summary Previously, the `quoted-annotation` rule only removed quotes when `from __future__ import annotations` was present. However, there are some other cases in which this is also safe -- for example: ```python def foo(): x: "MyClass" ``` We already model these in the semantic model, so this PR just expands the scope of the rule to handle those.
- Loading branch information
1 parent
6df6872
commit 1777683
Showing
8 changed files
with
699 additions
and
561 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...esources/test/fixtures/pyupgrade/UP037.py → ...ources/test/fixtures/pyupgrade/UP037_0.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
crates/ruff/resources/test/fixtures/pyupgrade/UP037_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""A mirror of UP037_0.py, without `from __future__ import annotations`.""" | ||
|
||
from typing import ( | ||
Annotated, | ||
Callable, | ||
List, | ||
Literal, | ||
NamedTuple, | ||
Tuple, | ||
TypeVar, | ||
TypedDict, | ||
cast, | ||
) | ||
|
||
from mypy_extensions import Arg, DefaultArg, DefaultNamedArg, NamedArg, VarArg | ||
|
||
|
||
def foo(var: "MyClass") -> "MyClass": | ||
x: "MyClass" | ||
|
||
|
||
def foo(*, inplace: "bool"): | ||
pass | ||
|
||
|
||
def foo(*args: "str", **kwargs: "int"): | ||
pass | ||
|
||
|
||
x: Tuple["MyClass"] | ||
|
||
x: Callable[["MyClass"], None] | ||
|
||
|
||
class Foo(NamedTuple): | ||
x: "MyClass" | ||
|
||
|
||
class D(TypedDict): | ||
E: TypedDict("E", foo="int", total=False) | ||
|
||
|
||
class D(TypedDict): | ||
E: TypedDict("E", {"foo": "int"}) | ||
|
||
|
||
x: Annotated["str", "metadata"] | ||
|
||
x: Arg("str", "name") | ||
|
||
x: DefaultArg("str", "name") | ||
|
||
x: NamedArg("str", "name") | ||
|
||
x: DefaultNamedArg("str", "name") | ||
|
||
x: DefaultNamedArg("str", name="name") | ||
|
||
x: VarArg("str") | ||
|
||
x: List[List[List["MyClass"]]] | ||
|
||
x: NamedTuple("X", [("foo", "int"), ("bar", "str")]) | ||
|
||
x: NamedTuple("X", fields=[("foo", "int"), ("bar", "str")]) | ||
|
||
x: NamedTuple(typename="X", fields=[("foo", "int")]) | ||
|
||
X: MyCallable("X") | ||
|
||
|
||
# OK | ||
class D(TypedDict): | ||
E: TypedDict("E") | ||
|
||
|
||
x: Annotated[()] | ||
|
||
x: DefaultNamedArg(name="name", quox="str") | ||
|
||
x: DefaultNamedArg(name="name") | ||
|
||
x: NamedTuple("X", [("foo",), ("bar",)]) | ||
|
||
x: NamedTuple("X", ["foo", "bar"]) | ||
|
||
x: NamedTuple() | ||
|
||
x: Literal["foo", "bar"] | ||
|
||
x = cast(x, "str") | ||
|
||
|
||
def foo(x, *args, **kwargs): | ||
... | ||
|
||
|
||
def foo(*, inplace): | ||
... | ||
|
||
|
||
x: Annotated[1:2] = ... | ||
|
||
x = TypeVar("x", "str", "int") | ||
|
||
x = cast("str", x) | ||
|
||
X = List["MyClass"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.