Skip to content

Commit ef13623

Browse files
implement workaround for \n{{ roundtripping bug
cf python/cpython#125008
1 parent 38d7cbc commit ef13623

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

mypy/fastparse.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,14 @@ def p() -> AST:
161161
tokens = tokenize.generate_tokens(io.StringIO(source).readline)
162162
else:
163163
tokens = tokenize.tokenize(io.BytesIO(source).readline)
164+
# We do a workaround for a roundtripping error https://github.com/python/cpython/issues/125008
165+
# An error that was introduced in 3.12.3 (https://github.com/python/cpython/blob/v3.12.3/Lib/tokenize.py#L205) and fixed in 3.12.8 (not out yet at time of writing but it's probably https://github.com/python/cpython/blob/v3.12.8/Lib/tokenize.py#L203 or thereabouts).
166+
# Luckily, it was caught before the first official (non-rc) release of python 3.13.
167+
is_defective_version = (3, 12, 3) <= sys.version_info[:3] <= (3, 12, 7)
168+
# Could the workaround ever come back to bite us? I confess I don't know enough about FSTRING_MIDDLE to say no for certain, even though I have tried to examine all relevant cases.
164169
source = tokenize.untokenize(
165-
(
166-
t,
167-
(
168-
re.sub(r"#\s*mypy:\s*ignore(?![-_])", "# type: ignore", s)
169-
if t == tokenize.COMMENT
170-
else s
171-
),
172-
)
173-
for t, s, *_ in tokens
170+
(t, re.sub(r"#\s*mypy:\s*ignore(?![-_])", "# type: ignore", s) if t == tokenize.COMMENT else s+'{' if is_defective_version and t == tokenize.FSTRING_MIDDLE and s.startswith('\\') and s.endswith('{') else s)
171+
for t, s, *_ in tokens
174172
)
175173
return p()
176174

0 commit comments

Comments
 (0)