Repository: psf/black
Pull request: Fix bad formatting of error messages about EOF in multi-line statements
Closes #2317 

Problem statement:
Bad formatting of error messages about EOF in multi-line statements
**Describe the bug**

"EOF in multi-line statement" error is shown to the user as the raw tuple it was returned as, rather than in a nicely formatted message.

**To Reproduce**

For example:
1. Take this (invalid) file:
```py
print(

```
2. Run _Black_ on it with no arguments
3. See a message with the EOF error being shown as a raw tuple representation:
```
error: cannot format test_black.py: ('EOF in multi-line statement', (2, 0))
Oh no! 💥 💔 💥
1 file failed to reformat.
```

**Expected behavior**

A bit more nicely formatted error message.

**Environment (please complete the following information):**

- Version: <!-- e.g. [main] -->
- OS and Python version: <!-- e.g. [Linux/Python 3.7.4rc1] -->

**Does this bug also happen on main?**

Yes.

**Additional context**

For comparison, here's how I'm assuming the error should look like:
```
error: cannot format test_black.py: Cannot parse: 1:7: print([)
Oh no! 💥 💔 💥
1 file failed to reformat.
```
which is what you get in output when you try to format:
```py
print([)
```

Hints from issue discussion:
Many thanks for reporting!
It appears to be about `TokenError` (and potentially also `IndentationError`) exception leaking out from `lib2to3_parse()` call. Should the `lib2to3_parse()` function just be updated to handle these 2 errors or is there some other solution that would be preferred here?
Eh, it seems to be just easier to add those two exceptions to the except definition than come up withe something more complicated.
Hi,
I would like to work on this. (I'm new to this).
Thanks
Hi, thanks for considering contributing! And welcome 😄 I don't think anyone's currently working on it so feel free to attempt a fix 👍 Jack's messages above should get you in the right direction.

Commit message: Changing message for TokenError

Patch to review:
diff --git a/CHANGES.md b/CHANGES.md
index 6714a9d9eb2..15eac739e66 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -44,6 +44,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 0b8d984cedd..015d237996f 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,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 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
 


Code review comments:
diff_hunk: @@ -92,6 +93,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 TokenError as te:
+            lineno, column = te.args[1]
+            lines = src_txt.splitlines()
+            exc = InvalidInput(f"Cannot parse: {lineno}:{column}: Unexpected EOF")
path: src/black/parsing.py
text: @user1:
The `lines = src_txt.splitlines()` line is unnecessary now.

@author:
Removed