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

Add all PEP-585 names to UP006 rule #5454

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP006_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ def f(x: typing.Deque[str]) -> None:

def f(x: typing.DefaultDict[str, str]) -> None:
...


def f(x: typing.AbstractSet[str]) -> None:
...


def f(x: typing.Pattern[str]) -> None:
...


def f(x: typing.Sequence[str]) -> None:
...
16 changes: 16 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP006_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@

def f(x: typing.DefaultDict[str, str]) -> None:
...


from collections.abc import Set
from typing_extensions import Awaitable


def f(x: typing.AbstractSet[str]) -> None:
...


def f(x: Set) -> None:
...


def f(x: Awaitable) -> None:
...
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,83 @@ UP006_0.py:65:10: UP006 [*] Use `collections.defaultdict` instead of `typing.Def
65 |-def f(x: typing.DefaultDict[str, str]) -> None:
66 |+def f(x: defaultdict[str, str]) -> None:
66 67 | ...
67 68 |
68 69 |

UP006_0.py:69:10: UP006 [*] Use `collections.abc.Set` instead of `typing.AbstractSet` for type annotation
|
69 | def f(x: typing.AbstractSet[str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
70 | ...
|
= help: Replace with `collections.abc.Set`

ℹ Suggested fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections.abc import Set
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
66 67 | ...
67 68 |
68 69 |
69 |-def f(x: typing.AbstractSet[str]) -> None:
70 |+def f(x: Set[str]) -> None:
70 71 | ...
71 72 |
72 73 |

UP006_0.py:73:10: UP006 [*] Use `re.Pattern` instead of `typing.Pattern` for type annotation
|
73 | def f(x: typing.Pattern[str]) -> None:
| ^^^^^^^^^^^^^^ UP006
74 | ...
|
= help: Replace with `re.Pattern`

ℹ Suggested fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from re import Pattern
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
70 71 | ...
71 72 |
72 73 |
73 |-def f(x: typing.Pattern[str]) -> None:
74 |+def f(x: Pattern[str]) -> None:
74 75 | ...
75 76 |
76 77 |

UP006_0.py:77:10: UP006 [*] Use `collections.abc.Sequence` instead of `typing.Sequence` for type annotation
|
77 | def f(x: typing.Sequence[str]) -> None:
| ^^^^^^^^^^^^^^^ UP006
78 | ...
|
= help: Replace with `collections.abc.Sequence`

ℹ Suggested fix
20 20 |
21 21 |
22 22 | from typing import List as IList
23 |+from collections.abc import Sequence
23 24 |
24 25 |
25 26 | def f(x: IList[str]) -> None:
--------------------------------------------------------------------------------
74 75 | ...
75 76 |
76 77 |
77 |-def f(x: typing.Sequence[str]) -> None:
78 |+def f(x: Sequence[str]) -> None:
78 79 | ...


Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,33 @@ UP006_1.py:9:10: UP006 [*] Use `collections.defaultdict` instead of `typing.Defa
9 |-def f(x: typing.DefaultDict[str, str]) -> None:
9 |+def f(x: defaultdict[str, str]) -> None:
10 10 | ...
11 11 |
12 12 |

UP006_1.py:17:10: UP006 [*] Use `collections.abc.Set` instead of `typing.AbstractSet` for type annotation
|
17 | def f(x: typing.AbstractSet[str]) -> None:
| ^^^^^^^^^^^^^^^^^^ UP006
18 | ...
|
= help: Replace with `collections.abc.Set`

ℹ Suggested fix
14 14 | from typing_extensions import Awaitable
15 15 |
16 16 |
17 |-def f(x: typing.AbstractSet[str]) -> None:
17 |+def f(x: Set[str]) -> None:
18 18 | ...
19 19 |
20 20 |

UP006_1.py:25:10: UP006 Use `collections.abc.Awaitable` instead of `Awaitable` for type annotation
|
25 | def f(x: Awaitable) -> None:
| ^^^^^^^^^ UP006
26 | ...
|
= help: Replace with `collections.abc.Awaitable`


3 changes: 2 additions & 1 deletion crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ impl<'a> SemanticModel<'a> {
// `from os.path import join` -> `join`
// `from os.path import join as join2` -> `join2`
BindingKind::FromImport(FromImport { qualified_name }) => {
if let Some((target_module, target_member)) = qualified_name.split_once('.')
if let Some((target_module, target_member)) =
qualified_name.rsplit_once('.')
wookie184 marked this conversation as resolved.
Show resolved Hide resolved
{
if target_module == module && target_member == member {
if let Some(source) = binding.source {
Expand Down
65 changes: 53 additions & 12 deletions crates/ruff_python_stdlib/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,59 @@ type ModuleMember = (&'static str, &'static str);
///
/// [PEP 585]: https://peps.python.org/pep-0585/
pub fn as_pep_585_generic(module: &str, member: &str) -> Option<ModuleMember> {
match (module, member) {
("typing", "Dict") => Some(("", "dict")),
("typing", "FrozenSet") => Some(("", "frozenset")),
("typing", "List") => Some(("", "list")),
("typing", "Set") => Some(("", "set")),
("typing", "Tuple") => Some(("", "tuple")),
("typing", "Type") => Some(("", "type")),
("typing_extensions", "Type") => Some(("", "type")),
("typing", "Deque") => Some(("collections", "deque")),
("typing_extensions", "Deque") => Some(("collections", "deque")),
("typing", "DefaultDict") => Some(("collections", "defaultdict")),
("typing_extensions", "DefaultDict") => Some(("collections", "defaultdict")),
// typing_extensions mirrors all the relevant types defined in typing.
if module != "typing" && module != "typing_extensions" {
return None;
};

match member {
// Builtins
"Tuple" => Some(("", "tuple")),
"List" => Some(("", "list")),
"Dict" => Some(("", "dict")),
"Set" => Some(("", "set")),
"FrozenSet" => Some(("", "frozenset")),
"Type" => Some(("", "type")),

// collections
"Deque" => Some(("collections", "deque")),
"DefaultDict" => Some(("collections", "defaultdict")),
"OrderedDict" => Some(("collections", "OrderedDict")),
"Counter" => Some(("collections", "Counter")),
"ChainMap" => Some(("collections", "ChainMap")),

// collections.abc
"Awaitable" => Some(("collections.abc", "Awaitable")),
"Coroutine" => Some(("collections.abc", "Coroutine")),
"AsyncIterable" => Some(("collections.abc", "AsyncIterable")),
"AsyncGenerator" => Some(("collections.abc", "AsyncGenerator")),
"Iterable" => Some(("collections.abc", "Iterable")),
"Iterator" => Some(("collections.abc", "Iterator")),
"Generator" => Some(("collections.abc", "Generator")),
"Reversible" => Some(("collections.abc", "Reversible")),
"Container" => Some(("collections.abc", "Container")),
"Collection" => Some(("collections.abc", "Collection")),
"Callable" => Some(("collections.abc", "Callable")),
"AbstractSet" => Some(("collections.abc", "Set")),
"MutableSet" => Some(("collections.abc", "MutableSet")),
"Mapping" => Some(("collections.abc", "Mapping")),
"MutableMapping" => Some(("collections.abc", "MutableMapping")),
"Sequence" => Some(("collections.abc", "Sequence")),
"MutableSequence" => Some(("collections.abc", "MutableSequence")),
"ByteString" => Some(("collections.abc", "ByteString")),
"MappingView" => Some(("collections.abc", "MappingView")),
"KeysView" => Some(("collections.abc", "KeysView")),
"ItemsView" => Some(("collections.abc", "ItemsView")),
"ValuesView" => Some(("collections.abc", "ValuesView")),

// contextlib
"ContextManager" => Some(("contextlib", "AbstractContextManager")),
"AsyncContextManager" => Some(("contextlib", "AbstractAsyncContextManager")),

// re
"Pattern" => Some(("re", "Pattern")),
"Match" => Some(("re", "Match")),

_ => None,
}
}
Expand Down