-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
False positive syntax-error
for badly placed type annotations
#3757
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
Comments
Here is a more minimal example code to reproduce the error with pylint 2.6.0 and astroid 2.4.2: def func(
# type: () -> int
):
return 2 If it is a bug, it is probably in astroid, as we end up here in pylint: https://github.com/PyCQA/pylint/blob/8112aae184f73bb780c7918821f0cac7fef7c74a/pylint/lint/pylinter.py#L1033-L1040 However, I think that the type comment is misplaced. It should not appear within the signature itself (i.e. inside the parentheses), but rather in the function body, like this: def func():
# type: () -> int
return 2 In that case, pylint does not raise a syntax error. I am not familiar with type comments, so I cannot say for sure. But all examples I could find (in 5 minutes) use that latter syntax. So does the cheat sheet in mypy documentation, which is probably a good sign that it is indeed the right way to write type comments. In which case the bug should be reported to pyinputplus itself. ;) |
I can still reproduce this on master. Raising a syntax error is a bit extreme so we should fix that, but @dbaty is correct that the type comment is in the wrong place and it should be below the function signature. This looks like an astroid bug (possibly even a Python one) rather than a pylint one. Here's the traceback I get when running this code through astroid:
|
Another issue in astroid (or maybe the same issue) causes #3556. |
I have reported an issue at The error message says Better to tell:
|
After reading AWhetter comment I found that astroid has some code that deals with misplaced type annotations causing syntax errors in the compiler but that Python3.8 uses a different message ( I have added a test in It would be great to log the line from the exception as a
|
sintax-error
for badly placed type annotations
sintax-error
for badly placed type annotationssyntax-error
for badly placed type annotations
I can reproduce this. Pycharm also think it's a syntax error. Classyfying as minor because proper typing is available since a long time and I don't think it's a big issue currently. |
Before fixing this we should investigate the effect of calling It is indeed a duplicate of #3556. The decision needed here is: do we want to retry parsing without type comments whenever |
Steps to reproduce
Current behavior
I posted a similar issue in the repo of the python extension for vsCode (Made by microsoft), but they said it was an issue with PyLint. The current behaviour is this:
When I run my code, the "problem" output looks like this:
The output looks like this:
Expected behavior
Expected behaviour is a code being able to run, with no errors shown.
pylint --version output
2.5.2
What I tried
I tried reload VSCode, reinstall it and I tried reinstalling the module. I checked the path and python versions, and everything was good. I'm not 100% sure if this is a bug, but I can't get it to work properly, so I suppose it is a bug.
The text was updated successfully, but these errors were encountered: