Skip to content

Commit

Permalink
implement workaround for \n{{ roundtripping bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattscarpenter committed Oct 7, 2024
1 parent 38d7cbc commit ef13623
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,14 @@ def p() -> AST:
tokens = tokenize.generate_tokens(io.StringIO(source).readline)
else:
tokens = tokenize.tokenize(io.BytesIO(source).readline)
# We do a workaround for a roundtripping error https://github.com/python/cpython/issues/125008
# 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).
# Luckily, it was caught before the first official (non-rc) release of python 3.13.
is_defective_version = (3, 12, 3) <= sys.version_info[:3] <= (3, 12, 7)
# 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.
source = tokenize.untokenize(
(
t,
(
re.sub(r"#\s*mypy:\s*ignore(?![-_])", "# type: ignore", s)
if t == tokenize.COMMENT
else s
),
)
for t, s, *_ in tokens
(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)
for t, s, *_ in tokens
)
return p()

Expand Down

0 comments on commit ef13623

Please sign in to comment.