Skip to content

Commit

Permalink
Support pep585 rewriting when using alias
Browse files Browse the repository at this point in the history
State.as_import probably has uses elsewhere, but this is sufficient to
start with.

We need to update some of the pep604 feature tests to ensure we actually
do the import.

Signed-off-by: Stephen Finucane <stephen@that.guru>
  • Loading branch information
stephenfin committed Oct 24, 2024
1 parent e4f87de commit 3bc5f61
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyupgrade/_plugins/typing_pep585.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def visit_Attribute(
if (
_should_rewrite(state) and
isinstance(node.value, ast.Name) and
node.value.id == 'typing' and
state.as_imports.get(node.value.id) == 'typing' and
node.attr in PEP585_BUILTINS
):
func = functools.partial(
Expand Down
9 changes: 9 additions & 0 deletions tests/features/typing_pep585_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def f(x: list[str]) -> None: ...
id='import of typing + typing.List',
),
pytest.param(
'import typing as ty\n'
'x: ty.List[int]\n',
'import typing as ty\n'
'x: list[int]\n',
id='aliased import of typing + typing.List',
),
pytest.param(
'from typing import List\n'
'SomeAlias = List[int]\n',
Expand Down
16 changes: 16 additions & 0 deletions tests/features/typing_pep604_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,46 +105,58 @@ def f(x: int | str) -> None: ...
id='Union rewrite',
),
pytest.param(
'import typing\n'
'x: typing.Union[int]\n',
'import typing\n'
'x: int\n',
id='Union of only one value',
),
pytest.param(
'import typing\n'
'x: typing.Union[Foo[str, int], str]\n',
'import typing\n'
'x: Foo[str, int] | str\n',
id='Union containing a value with brackets',
),
pytest.param(
'import typing\n'
'x: typing.Union[typing.List[str], str]\n',
'import typing\n'
'x: list[str] | str\n',
id='Union containing pep585 rewritten type',
),
pytest.param(
'import typing\n'
'x: typing.Union[int, str,]\n',
'import typing\n'
'x: int | str\n',
id='Union trailing comma',
),
pytest.param(
'import typing\n'
'x: typing.Union[(int, str)]\n',
'import typing\n'
'x: int | str\n',
id='Union, parenthesized tuple',
),
pytest.param(
'import typing\n'
'x: typing.Union[\n'
' int,\n'
' str\n'
']\n',
'import typing\n'
'x: (\n'
' int |\n'
' str\n'
Expand All @@ -153,11 +165,13 @@ def f(x: int | str) -> None: ...
id='Union multiple lines',
),
pytest.param(
'import typing\n'
'x: typing.Union[\n'
' int,\n'
' str,\n'
']\n',
'import typing\n'
'x: (\n'
' int |\n'
' str\n'
Expand All @@ -175,10 +189,12 @@ def f(x: int | str) -> None: ...
id='Optional rewrite',
),
pytest.param(
'import typing\n'
'x: typing.Optional[\n'
' ComplicatedLongType[int]\n'
']\n',
'import typing\n'
'x: None | (\n'
' ComplicatedLongType[int]\n'
')\n',
Expand Down

0 comments on commit 3bc5f61

Please sign in to comment.