Skip to content

Commit

Permalink
Update reverse list
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 1, 2023
1 parent d91df20 commit 3a46aeb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ def f(x: typing.Sequence[str]) -> None:

from typing import Collection


def f(x: typing.Collection[str]) -> None:
...
...
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ UP006_0.py:69:10: UP006 [*] Use `collections.abc.Set` instead of `typing.Abstrac
|
= help: Replace with `collections.abc.Set`

Suggested fix
Unsafe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
Expand All @@ -317,7 +317,7 @@ UP006_0.py:73:10: UP006 [*] Use `re.Pattern` instead of `typing.Pattern` for typ
|
= help: Replace with `re.Pattern`

Suggested fix
Unsafe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
Expand All @@ -343,7 +343,7 @@ UP006_0.py:77:10: UP006 [*] Use `collections.abc.Sequence` instead of `typing.Se
|
= help: Replace with `collections.abc.Sequence`

Suggested fix
Unsafe fix
20 20 |
21 21 |
22 22 | from typing import List as IList
Expand All @@ -361,13 +361,11 @@ UP006_0.py:77:10: UP006 [*] Use `collections.abc.Sequence` instead of `typing.Se
79 80 |
80 81 |

UP006_0.py:83:10: UP006 Use `collections.abc.Collection` instead of `typing.Collection` for type annotation
UP006_0.py:84:10: UP006 Use `collections.abc.Collection` instead of `typing.Collection` for type annotation
|
81 | from typing import Collection
82 |
83 | def f(x: typing.Collection[str]) -> None:
84 | def f(x: typing.Collection[str]) -> None:
| ^^^^^^^^^^^^^^^^^ UP006
84 | ...
85 | ...
|
= help: Replace with `collections.abc.Collection`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ UP006_1.py:17:10: UP006 [*] Use `collections.abc.Set` instead of `typing.Abstrac
|
= help: Replace with `collections.abc.Set`

Suggested fix
Unsafe fix
14 14 | from typing_extensions import Awaitable
15 15 |
16 16 |
Expand Down
5 changes: 1 addition & 4 deletions crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ impl std::fmt::Display for ModuleMember {
/// a variant exists.
pub fn to_pep585_generic(expr: &Expr, semantic: &SemanticModel) -> Option<ModuleMember> {
semantic.resolve_call_path(expr).and_then(|call_path| {
let [module, member] = call_path.as_slice() else {
return None;
};
as_pep_585_generic(module, member).map(|(module, member)| {
as_pep_585_generic(call_path.as_slice()).map(|(module, member)| {
if module.is_empty() {
ModuleMember::BuiltIn(member)
} else {
Expand Down
132 changes: 86 additions & 46 deletions crates/ruff_python_stdlib/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,59 +375,68 @@ type ModuleMember = (&'static str, &'static str);
/// library (e.g., `list` for `typing.List`), if such a generic was introduced by [PEP 585].
///
/// [PEP 585]: https://peps.python.org/pep-0585/
pub fn as_pep_585_generic(module: &str, member: &str) -> Option<ModuleMember> {
// typing_extensions mirrors all the relevant types defined in typing.
if module != "typing" && module != "typing_extensions" {
return None;
};

match member {
pub fn as_pep_585_generic(call_path: &[&str]) -> Option<ModuleMember> {
match call_path {
// Builtins
"Tuple" => Some(("", "tuple")),
"List" => Some(("", "list")),
"Dict" => Some(("", "dict")),
"Set" => Some(("", "set")),
"FrozenSet" => Some(("", "frozenset")),
"Type" => Some(("", "type")),
["typing" | "typing_extensions", "Tuple"] => Some(("", "tuple")),
["typing" | "typing_extensions", "List"] => Some(("", "list")),
["typing" | "typing_extensions", "Dict"] => Some(("", "dict")),
["typing" | "typing_extensions", "Set"] => Some(("", "set")),
["typing" | "typing_extensions", "FrozenSet"] => Some(("", "frozenset")),
["typing" | "typing_extensions", "Type"] => Some(("", "type")),

// collections
"Deque" => Some(("collections", "deque")),
"DefaultDict" => Some(("collections", "defaultdict")),
"OrderedDict" => Some(("collections", "OrderedDict")),
"Counter" => Some(("collections", "Counter")),
"ChainMap" => Some(("collections", "ChainMap")),
["typing" | "typing_extensions", "Deque"] => Some(("collections", "deque")),
["typing" | "typing_extensions", "DefaultDict"] => Some(("collections", "defaultdict")),
["typing" | "typing_extensions", "OrderedDict"] => Some(("collections", "OrderedDict")),
["typing" | "typing_extensions", "Counter"] => Some(("collections", "Counter")),
["typing" | "typing_extensions", "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")),
["typing" | "typing_extensions", "Awaitable"] => Some(("collections.abc", "Awaitable")),
["typing" | "typing_extensions", "Coroutine"] => Some(("collections.abc", "Coroutine")),
["typing" | "typing_extensions", "AsyncIterable"] => {
Some(("collections.abc", "AsyncIterable"))
}
["typing" | "typing_extensions", "AsyncGenerator"] => {
Some(("collections.abc", "AsyncGenerator"))
}
["typing" | "typing_extensions", "Iterable"] => Some(("collections.abc", "Iterable")),
["typing" | "typing_extensions", "Iterator"] => Some(("collections.abc", "Iterator")),
["typing" | "typing_extensions", "Generator"] => Some(("collections.abc", "Generator")),
["typing" | "typing_extensions", "Reversible"] => Some(("collections.abc", "Reversible")),
["typing" | "typing_extensions", "Container"] => Some(("collections.abc", "Container")),
["typing" | "typing_extensions", "Collection"] => Some(("collections.abc", "Collection")),
["typing" | "typing_extensions", "Callable"] => Some(("collections.abc", "Callable")),
["typing" | "typing_extensions", "AbstractSet"] => Some(("collections.abc", "Set")),
["typing" | "typing_extensions", "MutableSet"] => Some(("collections.abc", "MutableSet")),
["typing" | "typing_extensions", "Mapping"] => Some(("collections.abc", "Mapping")),
["typing" | "typing_extensions", "MutableMapping"] => {
Some(("collections.abc", "MutableMapping"))
}
["typing" | "typing_extensions", "Sequence"] => Some(("collections.abc", "Sequence")),
["typing" | "typing_extensions", "MutableSequence"] => {
Some(("collections.abc", "MutableSequence"))
}
["typing" | "typing_extensions", "ByteString"] => Some(("collections.abc", "ByteString")),
["typing" | "typing_extensions", "MappingView"] => Some(("collections.abc", "MappingView")),
["typing" | "typing_extensions", "KeysView"] => Some(("collections.abc", "KeysView")),
["typing" | "typing_extensions", "ItemsView"] => Some(("collections.abc", "ItemsView")),
["typing" | "typing_extensions", "ValuesView"] => Some(("collections.abc", "ValuesView")),

// contextlib
"ContextManager" => Some(("contextlib", "AbstractContextManager")),
"AsyncContextManager" => Some(("contextlib", "AbstractAsyncContextManager")),
["typing" | "typing_extensions", "ContextManager"] => {
Some(("contextlib", "AbstractContextManager"))
}
["typing" | "typing_extensions", "AsyncContextManager"] => {
Some(("contextlib", "AbstractAsyncContextManager"))
}

// re
"Pattern" => Some(("re", "Pattern")),
"Match" => Some(("re", "Match")),
["typing" | "typing_extensions", "Pattern"] => Some(("re", "Pattern")),
["typing" | "typing_extensions", "Match"] => Some(("re", "Match")),
["typing" | "typing_extensions", "re", "Pattern"] => Some(("re", "Pattern")),
["typing" | "typing_extensions", "re", "Match"] => Some(("re", "Match")),

_ => None,
}
Expand All @@ -442,8 +451,39 @@ pub fn has_pep_585_generic(module: &str, member: &str) -> bool {
// the last element in each pattern, and de-duplicating the values.
matches!(
(module, member),
("", "dict" | "frozenset" | "list" | "set" | "tuple" | "type")
| ("collections", "deque" | "defaultdict")
("", "tuple" | "list" | "dict" | "set" | "frozenset" | "type")
| (
"collections",
"deque" | "defaultdict" | "OrderedDict" | "Counter" | "ChainMap"
)
| (
"collections.abc",
"Awaitable"
| "Coroutine"
| "Iterable"
| "Iterator"
| "Generator"
| "Reversible"
| "Container"
| "Collection"
| "Callable"
| "Set"
| "MutableSet"
| "Mapping"
| "MutableMapping"
| "Sequence"
| "MutableSequence"
| "ByteString"
| "MappingView"
| "KeysView"
| "ItemsView"
| "ValuesView"
)
| (
"contextlib",
"AbstractContextManager" | "AbstractAsyncContextManager"
)
| ("re", "Pattern" | "Match")
)
}

Expand Down

0 comments on commit 3a46aeb

Please sign in to comment.