Skip to content

Commit 3a1254b

Browse files
authored
Merge pull request #1043 from asottile/unstring-annotation-multiline
fix unstringing multiline string annotations
2 parents 46c3d52 + 98a3718 commit 3a1254b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pyupgrade/_plugins/typing_pep563.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Iterable
77
from collections.abc import Sequence
88

9+
from tokenize_rt import NON_CODING_TOKENS
910
from tokenize_rt import Offset
1011

1112
from pyupgrade._ast_helpers import ast_to_offset
@@ -23,7 +24,15 @@ def _supported_version(state: State) -> bool:
2324

2425

2526
def _dequote(i: int, tokens: list[Token], *, new: str) -> None:
26-
tokens[i] = tokens[i]._replace(src=new)
27+
end = i + 1
28+
for j in range(end, len(tokens)):
29+
if tokens[j].name == 'STRING':
30+
end = j + 1
31+
elif tokens[j].name not in NON_CODING_TOKENS:
32+
break
33+
else:
34+
raise AssertionError('past end?')
35+
tokens[i:end] = [tokens[i]._replace(src=new)]
2736

2837

2938
def _get_name(node: ast.expr) -> str:

tests/features/typing_pep563_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,20 @@ def test_fix_typing_pep563_noop(s):
365365
366366
id='posonly args',
367367
),
368+
pytest.param(
369+
'from __future__ import annotations\n'
370+
'x: (\n'
371+
' "int | "\n'
372+
' "str"\n'
373+
')\n',
374+
375+
'from __future__ import annotations\n'
376+
'x: (\n'
377+
' int | str\n'
378+
')\n',
379+
380+
id='multiline implicit-concat annotation',
381+
),
368382
),
369383
)
370384
def test_fix_typing_pep563(s, expected):

0 commit comments

Comments
 (0)