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

parse error message in case of assignment to literal #510

Merged
merged 5 commits into from
Dec 9, 2020
Merged
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
4 changes: 3 additions & 1 deletion nbqa/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def _get_pattern(
(
rf"(?<=^error: cannot format {re.escape(str(notebook))}: Cannot parse: )\d+",
standard_substitution,
)
),
(r"(?<=line )\d+(?=\)\nOh no! )", standard_substitution),
(r"line cell_(?=\d+:\d+\)\nOh no! )", "cell_"),
]

if command == "doctest":
Expand Down
35 changes: 35 additions & 0 deletions tests/invalid_data/assignment_to_literal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1 = [1,2,3]"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0-final"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
38 changes: 38 additions & 0 deletions tests/tools/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

SPARKLES = "\N{sparkles}"
SHORTCAKE = "\N{shortcake}"
COLLISION = "\N{collision symbol}"
BROKEN_HEART = "\N{broken heart}"


def test_black_works(tmp_notebook_for_testing: Path, capsys: "CaptureFixture") -> None:
Expand Down Expand Up @@ -343,3 +345,39 @@ def test_black_works_with_leading_comment(capsys: "CaptureFixture") -> None:
expected_err = ""
assert expected_out == out
assert expected_err == err


def test_black_works_with_literal_assignment(capsys: "CaptureFixture") -> None:
"""
Check black works with notebooks with invalid syntax (e.g. assignment to literal).

Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
path = os.path.abspath(
os.path.join("tests", "invalid_data", "assignment_to_literal.ipynb")
)

with pytest.raises(SystemExit):
main(["black", path])

out, err = capsys.readouterr()
expected_out = ""
expected_err = (
(
f"error: cannot format {path}: "
"cannot use --safe with this file; failed to parse source file. AST error message: "
"can't assign to literal (<unknown>, cell_1:1)\nOh no! "
f"{COLLISION} {BROKEN_HEART} {COLLISION}\n1 file failed to reformat.\n"
)
.encode("ascii", "backslashreplace")
.decode()
)
# This is required because linux supports emojis
# so both should have \\ for comparison
err = err.encode("ascii", "backslashreplace").decode()

assert expected_out == out
assert expected_err == err