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

added python version to parse error message #3322

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@

### Output

- Add python version to parse error message
- Change from deprecated `asyncio.get_event_loop()` to create our event loop which
removes DeprecationWarning (#3164)
- Remove logging from internal `blib2to3` library since it regularly emits error logs
Expand Down
6 changes: 3 additions & 3 deletions docs/usage_and_configuration/the_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ plus a short summary.

```console
$ black src/
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6: mport asyncio
error: cannot format src/black_primer/cli.py: Cannot parse (python 3.10): 5:6: mport asyncio
reformatted src/black_primer/lib.py
reformatted src/blackd/__init__.py
reformatted src/black/__init__.py
Expand All @@ -151,7 +151,7 @@ Using configuration from /tmp/pyproject.toml.
src/blib2to3 ignored: matches the --extend-exclude regular expression
src/_black_version.py wasn't modified on disk since last run.
src/black/__main__.py wasn't modified on disk since last run.
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6: mport asyncio
error: cannot format src/black_primer/cli.py: Cannot parse (python 3.10): 5:6: mport asyncio
reformatted src/black_primer/lib.py
reformatted src/blackd/__init__.py
reformatted src/black/__init__.py
Expand All @@ -164,7 +164,7 @@ Error messages will still be emitted (which can silenced by `2>/dev/null`).

```console
$ black src/ -q
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6: mport asyncio
error: cannot format src/black_primer/cli.py: Cannot parse (python 3.10): 5:6: mport asyncio
```

### Versions
Expand Down
8 changes: 6 additions & 2 deletions src/black/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,19 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -
faulty_line = lines[lineno - 1]
except IndexError:
faulty_line = "<line number missing in source>"
pretty_version = ".".join(str(number) for number in grammar.version)
errors[grammar.version] = InvalidInput(
f"Cannot parse: {lineno}:{column}: {faulty_line}"
f"Cannot parse (python {pretty_version}): {lineno}:{column}:"
f" {faulty_line}"
)

except TokenError as te:
# In edge cases these are raised; and typically don't have a "faulty_line".
lineno, column = te.args[1]
pretty_version = ".".join(str(number) for number in grammar.version)
errors[grammar.version] = InvalidInput(
f"Cannot parse: {lineno}:{column}: {te.args[0]}"
f"Cannot parse (python {pretty_version}): {lineno}:{column}:"
f" {te.args[0]}"
)

else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def test_format_file_contents(self) -> None:
invalid = "return if you can"
with self.assertRaises(black.InvalidInput) as e:
black.format_file_contents(invalid, mode=mode, fast=False)
self.assertEqual(str(e.exception), "Cannot parse: 1:7: return if you can")
self.assertIn("1:7: return if you can", str(e.exception))

def test_endmarker(self) -> None:
n = black.lib2to3_parse("\n")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_patma_invalid() -> None:
with pytest.raises(black.parsing.InvalidInput) as exc_info:
assert_format(source, expected, mode, minimum_version=(3, 10))

exc_info.match("Cannot parse: 10:11")
exc_info.match("Cannot parse (python 3.10): 10:11")


@pytest.mark.parametrize("filename", all_data_cases("py_311"))
Expand Down