-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 on type-hints #1461
Comments
In general, this problem isn't that Black can't format the file, it's that it can't do its sanity checks which verify if Black didn't make dangerous changes to the file. That's why the error message says The problem in this case is that the third party AST3 parser from (I did modify the Black source code to skip the build-in (black-rHKUX7ap) R:\Programming\black>black test.py
Python3.7: misplaced type annotation (<unknown>, line 15)
Python3.6: misplaced type annotation (<unknown>, line 15)
error: cannot format test.py: cannot use --safe with this file; failed to parse source file. AST error message: invalid syntax (<unknown>, line 8)
Oh no! 💥 💔 💥
1 file failed to reformat The second issue of Black erroring on the wrong line when trying to do its sanity checks is already tracked here: #1263. The problem is that when Black tries to parse the source code the Python3.7 grammar fails to parse with What's interesting about this is that it only fails when running Black on Python37 or Python36 because the built-in Environment:
Unrelated, but the unittests are failing because they need extra dependencies to test |
Hi ichard26. Thanks for having a look into this! The Good to have the wrong-line issue a confirmed as a duplicate of #1263. It was very much unrelated then to the original problem. For the type-hint parser issue: I guess this still remains an "issue" with black's syntax-tree checker then, and it should try to parse the file without type-hints a second time, where applicable, maybe? |
Regarding the source code parser issue, since the built-in parser provided by the standard library can parse your bug-causing code without error, it might be a bug with the The only problem with Black trying to parse the file without type-hints is that we give special treatment to them because they shouldn't be moved or else Black will annoy
We really don't want Black to suddenly start formatting files that fail pre-commit hooks because Black moved a few type-comments to the wrong place for example. And while the formatting part of Black uses a completely different parser from what I know, the parser used for sanity checks needs to have type-hints so Black can check if it wrongly moved or deleted type-hints. Although, I don't know why we only need the third-party parser when running on Python36 and Python37. 🤷 |
The
Black uses (yeah I know I took forever to respond with the right answer, but that's because I was more naive and dumber when I first replied so had no idea what was the actual issue. now I'm more experienced around the Black project :D ) |
Hey there! Thanks for revisiting this bug again. IMO this might just be a case of a wont-fix even, maybe. At this point we left behind python2.7 for good (for the most part, I guess) and interoperability between different python versions is not so important anymore. That then only leaves the question if this is relevant for projects before python3.5, however, for those projects I would not see why they cannot upgrade to a newer python version. For me/us it is certainly not important anymore. I will leave it to the black-admins to determine whether this is worth fixing. Personally, I would expect this problem to just not be relevant anymore. |
Yeah I agree and even if it was decided this should be fixed, it would be a task for the |
I think issue lies with the underlying # Example YAML schema:
# SomeObject:
# type: "something"
class SomeClass:
pass I was able to make the error go away by double commenting the line so it become For completeness The error (Black version 21.5b1, Python version 3.6)
All the more reason to update to |
Closing since there's nothing we can reasonably do due to this being an issue with the Thanks for the bug report tho, a bit unfortunate we can't do anything about :( |
Describe the bug
Black fails to parse a valid python3.6 file due to a type-hint comment. The produced error is:
There seem to be actually two problems observable here.
The issue seems to be caused by line 15
# type: (Arg) -> None
AND having empty lines at the beginning of the file. Letting the file start withclass SomeClass:
immediately works.The error is reported at line 8. The line 8 vs 15 confusion can be solved by removing the python3.6-style type annotation from line 8. Then the error is correctly reported on line 15.
To Reproduce
Take this python file:
Run black on it:
black --target-version py36 test.py
Expected behavior
No errors, maybe a reformat. But it should be working - the file can be parsed by python afterall.
Environment:
Does this bug also happen on master?
Yes, I just downloaded master to confirm. However the online formatter with the same file works. Maybe it does something to the initial lines, which this bug seems sensitive to?
Note that the test_suite did not run all the way through for me, not sure whether this is expected:
The text was updated successfully, but these errors were encountered: