Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad formatting of error messages about EOF in multi-line statements #2343

Merged
merged 10 commits into from Dec 4, 2021
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -44,6 +44,7 @@
- Fix regression where `R` prefixes would be lowercased for docstrings (#2285)
- Fix handling of named escapes (`\N{...}`) when `--experimental-string-processing` is
used (#2319)
- Fix bad formatting of error messages about EOF in multi-line statements (#2343)

### Integrations

Expand Down
7 changes: 7 additions & 0 deletions src/black/parsing.py
Expand Up @@ -11,6 +11,7 @@
from blib2to3.pgen2 import driver
from blib2to3.pgen2.grammar import Grammar
from blib2to3.pgen2.parse import ParseError
from blib2to3.pgen2.tokenize import TokenError

from black.mode import TargetVersion, Feature, supports_feature
from black.nodes import syms
Expand Down Expand Up @@ -92,6 +93,12 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -
except IndexError:
faulty_line = "<line number missing in source>"
exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}")

except TokenError as te:
lineno, column = te.args[1]
lines = src_txt.splitlines()
exc = InvalidInput(f"Cannot parse: {lineno}:{column}: Unexpected EOF")
tanvimoharir marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included it as a separate block since that seemed more readable to me instead of adding multiple if (isintance,TokenError) in the above block where we're handling ParseError

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I agree the duplication isn't a problem here.

else:
raise exc from None

Expand Down