Skip to content

Commit 3bc5f61

Browse files
committed
Support pep585 rewriting when using alias
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 <[email protected]>
1 parent e4f87de commit 3bc5f61

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

pyupgrade/_plugins/typing_pep585.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def visit_Attribute(
3636
if (
3737
_should_rewrite(state) and
3838
isinstance(node.value, ast.Name) and
39-
node.value.id == 'typing' and
39+
state.as_imports.get(node.value.id) == 'typing' and
4040
node.attr in PEP585_BUILTINS
4141
):
4242
func = functools.partial(

tests/features/typing_pep585_test.py

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def f(x: list[str]) -> None: ...
8484
8585
id='import of typing + typing.List',
8686
),
87+
pytest.param(
88+
'import typing as ty\n'
89+
'x: ty.List[int]\n',
90+
91+
'import typing as ty\n'
92+
'x: list[int]\n',
93+
94+
id='aliased import of typing + typing.List',
95+
),
8796
pytest.param(
8897
'from typing import List\n'
8998
'SomeAlias = List[int]\n',

tests/features/typing_pep604_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -105,46 +105,58 @@ def f(x: int | str) -> None: ...
105105
id='Union rewrite',
106106
),
107107
pytest.param(
108+
'import typing\n'
108109
'x: typing.Union[int]\n',
109110
111+
'import typing\n'
110112
'x: int\n',
111113
112114
id='Union of only one value',
113115
),
114116
pytest.param(
117+
'import typing\n'
115118
'x: typing.Union[Foo[str, int], str]\n',
116119
120+
'import typing\n'
117121
'x: Foo[str, int] | str\n',
118122
119123
id='Union containing a value with brackets',
120124
),
121125
pytest.param(
126+
'import typing\n'
122127
'x: typing.Union[typing.List[str], str]\n',
123128
129+
'import typing\n'
124130
'x: list[str] | str\n',
125131
126132
id='Union containing pep585 rewritten type',
127133
),
128134
pytest.param(
135+
'import typing\n'
129136
'x: typing.Union[int, str,]\n',
130137
138+
'import typing\n'
131139
'x: int | str\n',
132140
133141
id='Union trailing comma',
134142
),
135143
pytest.param(
144+
'import typing\n'
136145
'x: typing.Union[(int, str)]\n',
137146
147+
'import typing\n'
138148
'x: int | str\n',
139149
140150
id='Union, parenthesized tuple',
141151
),
142152
pytest.param(
153+
'import typing\n'
143154
'x: typing.Union[\n'
144155
' int,\n'
145156
' str\n'
146157
']\n',
147158
159+
'import typing\n'
148160
'x: (\n'
149161
' int |\n'
150162
' str\n'
@@ -153,11 +165,13 @@ def f(x: int | str) -> None: ...
153165
id='Union multiple lines',
154166
),
155167
pytest.param(
168+
'import typing\n'
156169
'x: typing.Union[\n'
157170
' int,\n'
158171
' str,\n'
159172
']\n',
160173
174+
'import typing\n'
161175
'x: (\n'
162176
' int |\n'
163177
' str\n'
@@ -175,10 +189,12 @@ def f(x: int | str) -> None: ...
175189
id='Optional rewrite',
176190
),
177191
pytest.param(
192+
'import typing\n'
178193
'x: typing.Optional[\n'
179194
' ComplicatedLongType[int]\n'
180195
']\n',
181196
197+
'import typing\n'
182198
'x: None | (\n'
183199
' ComplicatedLongType[int]\n'
184200
')\n',

0 commit comments

Comments
 (0)