]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Don't let TokenError bubble up from lib2to3_parse (GH-2343)
authorTanvi Moharir <74228962+tanvimoharir@users.noreply.github.com>
Sat, 4 Dec 2021 20:21:26 +0000 (01:51 +0530)
committerGitHub <noreply@github.com>
Sat, 4 Dec 2021 20:21:26 +0000 (15:21 -0500)
error: cannot format <string>: ('EOF in multi-line statement', (2, 0))

 ▲ before ▼ after

error: cannot format <string>: Cannot parse: 2:0: EOF in multi-line statement

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
CHANGES.md
src/black/parsing.py
tests/test_black.py

index e5f4a1fdf82d13302978fad3f832e96a1aa93afe..5d89c71f5801075056240b8c8f8a8596000ce79c 100644 (file)
@@ -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)
 - 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
 - Functions and classes in blocks now have more consistent surrounding spacing (#2472)
 
 ## 21.11b1
index e38405637cd88c745cfd965ef7698f68ad471ce7..b673027022f44b4a2ebd5a9b4b3bd44d0b163b3b 100644 (file)
@@ -17,6 +17,7 @@ from blib2to3 import pygram
 from blib2to3.pgen2 import driver
 from blib2to3.pgen2.grammar import Grammar
 from blib2to3.pgen2.parse import ParseError
 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
 
 from black.mode import TargetVersion, Feature, supports_feature
 from black.nodes import syms
@@ -109,6 +110,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 IndexError:
                 faulty_line = "<line number missing in source>"
             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}: {te.args[0]}")
+
     else:
         raise exc from None
 
     else:
         raise exc from None
 
index 51a20307e5697466ba6510ed2023427ed43fe8c2..92598532e2c4086b794dcbaa27e83e6b52ec294f 100644 (file)
@@ -1557,6 +1557,15 @@ class BlackTestCase(BlackBaseTestCase):
                     call_args[0].lower() == str(pyproject_path).lower()
                 ), "Incorrect config loaded."
 
                     call_args[0].lower() == str(pyproject_path).lower()
                 ), "Incorrect config loaded."
 
+    def test_for_handled_unexpected_eof_error(self) -> None:
+        """
+        Test that an unexpected EOF SyntaxError is nicely presented.
+        """
+        with pytest.raises(black.parsing.InvalidInput) as exc_info:
+            black.lib2to3_parse("print(", {})
+
+        exc_info.match("Cannot parse: 2:0: EOF in multi-line statement")
+
 
 class TestCaching:
     def test_cache_broken_file(self) -> None:
 
 class TestCaching:
     def test_cache_broken_file(self) -> None: