From 05e50d9cb63b80b88caac18cda27d68d02fbd77a Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Sun, 20 Jun 2021 20:25:05 +0530 Subject: [PATCH 1/7] Adding TokenError to lib2to3 function --- src/black/parsing.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/black/parsing.py b/src/black/parsing.py index 8e9feea9120..8e9764e3095 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -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 @@ -92,6 +93,16 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - except IndexError: faulty_line = "" exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}") + + except TokenError as te: + lineno, column = te.args[1] + lines = src_txt.splitlines() + try: + faulty_line = lines[lineno - 1] + except IndexError: + faulty_line = "" + exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}") + else: raise exc from None From 179f1ecab49a55902f45abe8624af30870b006df Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Fri, 25 Jun 2021 15:03:25 +0530 Subject: [PATCH 2/7] Removing redundent exception handling for TokenError --- CHANGES.md | 1 + src/black/parsing.py | 16 +++++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ea5196e07a0..e1203251e60 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,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 diff --git a/src/black/parsing.py b/src/black/parsing.py index 8e9764e3095..2c8a5067cd4 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -85,17 +85,11 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - result = drv.parse_string(src_txt, True) break - except ParseError as pe: - lineno, column = pe.context[1] - lines = src_txt.splitlines() - try: - faulty_line = lines[lineno - 1] - except IndexError: - faulty_line = "" - exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {faulty_line}") - - except TokenError as te: - lineno, column = te.args[1] + except (TokenError, ParseError) as err: + if isinstance(err, ParseError): + lineno, column = err.context[1] + else: + lineno, column = err.args[1] lines = src_txt.splitlines() try: faulty_line = lines[lineno - 1] From fc40194c38aa540c020b3ae31e29167558333d5b Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Wed, 21 Jul 2021 21:15:13 +0530 Subject: [PATCH 3/7] Changing message for TokenError --- src/black/parsing.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 22888481d28..015d237996f 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -85,11 +85,8 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - result = drv.parse_string(src_txt, True) break - except (TokenError, ParseError) as err: - if isinstance(err, ParseError): - lineno, column = err.context[1] - else: - lineno, column = err.args[1] + except ParseError as pe: + lineno, column = pe.context[1] lines = src_txt.splitlines() try: faulty_line = lines[lineno - 1] @@ -97,6 +94,11 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - faulty_line = "" 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") + else: raise exc from None From b5a3a7b687309ea1d6df4be0dccd308477823097 Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Mon, 30 Aug 2021 22:08:36 +0530 Subject: [PATCH 4/7] Removing splitlines --- src/black/parsing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 015d237996f..62e2e845e74 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -96,7 +96,6 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - except TokenError as te: lineno, column = te.args[1] - lines = src_txt.splitlines() exc = InvalidInput(f"Cannot parse: {lineno}:{column}: Unexpected EOF") else: From 813c8ddded61e668efda2dd32a41909f3ea0ed83 Mon Sep 17 00:00:00 2001 From: tanvimoharir Date: Wed, 8 Sep 2021 14:09:35 +0530 Subject: [PATCH 5/7] Adding tests --- tests/test_black.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_black.py b/tests/test_black.py index 998ecfcbdeb..21cd96a3412 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2198,6 +2198,26 @@ def test_code_option_parent_config(self) -> None: call_args[0].lower() == str(pyproject_path).lower() ), "Incorrect config loaded." + def test_code_with_unexpected_eof_error(self) -> None: + """ + Test that Unexpected EOF error is raised with invalid code + """ + code = "print(" + args = ["--check", "--code", code] + error_msg = "error: cannot format : Cannot parse: 2:0: Unexpected EOF\n" + result = CliRunner().invoke(black.main, args) + self.compare_results(result, error_msg, 123) + + def test_invalid_input_parsing_error(self) -> None: + """ + Test with invalid code which throws parsing error + """ + code = "print([)" + args = ["--check", "--code", code] + error_msg = f"error: cannot format : Cannot parse: 1:7: {code}\n" + result = CliRunner().invoke(black.main, args) + self.compare_results(result, error_msg, 123) + with open(black.__file__, "r", encoding="utf-8") as _bf: black_source_lines = _bf.readlines() From 8ded0118b1a2e33456d9d825505731d32387c27d Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 4 Dec 2021 14:59:11 -0500 Subject: [PATCH 6/7] Cleanup tests and improve error message --- src/black/parsing.py | 3 ++- tests/test_black.py | 21 +++++---------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/black/parsing.py b/src/black/parsing.py index 62e2e845e74..7e4098b4572 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -95,8 +95,9 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) - exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {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] - exc = InvalidInput(f"Cannot parse: {lineno}:{column}: Unexpected EOF") + exc = InvalidInput(f"Cannot parse: {lineno}:{column}: {te.args[0]}") else: raise exc from None diff --git a/tests/test_black.py b/tests/test_black.py index 21cd96a3412..f1f7ffcbae0 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -2198,25 +2198,14 @@ def test_code_option_parent_config(self) -> None: call_args[0].lower() == str(pyproject_path).lower() ), "Incorrect config loaded." - def test_code_with_unexpected_eof_error(self) -> None: + def test_for_handled_unexpected_eof_error(self) -> None: """ - Test that Unexpected EOF error is raised with invalid code + Test that an unexpected EOF SyntaxError is nicely presented. """ - code = "print(" - args = ["--check", "--code", code] - error_msg = "error: cannot format : Cannot parse: 2:0: Unexpected EOF\n" - result = CliRunner().invoke(black.main, args) - self.compare_results(result, error_msg, 123) + with pytest.raises(black.parsing.InvalidInput) as exc_info: + black.lib2to3_parse("print(", {}) - def test_invalid_input_parsing_error(self) -> None: - """ - Test with invalid code which throws parsing error - """ - code = "print([)" - args = ["--check", "--code", code] - error_msg = f"error: cannot format : Cannot parse: 1:7: {code}\n" - result = CliRunner().invoke(black.main, args) - self.compare_results(result, error_msg, 123) + exc_info.match("Cannot parse: 2:0: EOF in multi-line statement") with open(black.__file__, "r", encoding="utf-8") as _bf: From 339a1738e22dbd7d36a04aaa0620a31700d6a9d4 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Sat, 4 Dec 2021 15:00:22 -0500 Subject: [PATCH 7/7] Move changelog to the right spot --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 2f63cbc942f..5d89c71f580 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,7 @@ - Add `flake8-simplify` and `flake8-comprehensions` plugins (#2653) - Fix determination of f-string expression spans (#2654) - Fix parser error location on invalid syntax in a `match` statement (#2649) +- Fix bad formatting of error messages about EOF in multi-line statements (#2343) - Functions and classes in blocks now have more consistent surrounding spacing (#2472) ## 21.11b1 @@ -146,7 +147,6 @@ - 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