Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/black/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ def lib2to3_parse(
faulty_line = lines[lineno - 1]
except IndexError:
faulty_line = "<line number missing in source>"
errors[grammar.version] = InvalidInput(
f"Cannot parse{tv_str}: {lineno}:{column}: {faulty_line}"
)
error_msg = (
f"Cannot parse{tv_str}: {lineno}:{column}\n"
f" {faulty_line}\n"
f" {' ' * (column - 1)}^\n"
"SyntaxError: invalid syntax"
)

errors[grammar.version] = InvalidInput(error_msg)

except TokenError as te:
# In edge cases these are raised; and typically don't have a "faulty_line".
lineno, column = te.args[1]
errors[grammar.version] = InvalidInput(
f"Cannot parse{tv_str}: {lineno}:{column}: {te.args[0]}"
)
error_msg = (
f"Cannot parse{tv_str}: {lineno}:{column}\n"
f" {te.args[0]}\n"
f" {' ' * (column - 1)}^\n"
"SyntaxError: invalid syntax"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

te.args[0] isn't the source code, it's the error message. The faulty line would have to be extracted from the source the same way it was for ParseError. And again, could the last line be changed to TokenError: {te.args[0]}? Thanks!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I didn't realize this earlier, but it isn't fixed still, it needs to extract the error line

)

errors[grammar.version] = InvalidInput(error_msg)

else:
# Choose the latest version when raising the actual parsing error.
Expand Down
Loading