-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
syntax-error incorectly detected inside a type-comment with python 3.8 #3556
Comments
I can reproduce this. Apparently, pylint expect the type to be after the declaration not in the middle of it. Ie: ports = relationship(
"Port", back_populates="device", collection_class=set
) # type: Set[Port] But it should not be a syntax error. |
I tried to add a test case, but I think fixing the version of various package is causing a problem. The problem does not exist with pylint 2.5.0 if you do not force other libs to be at a specific version. Step to fix:
Result :
Try not settings the version of the dependencies of your dependencies, your requirements should be just "pylint". |
@Pierre-Sassoulas I don't understand. I have pylint 2.5 and I get the error. But you say:
I also don't fix the versions in the dependencies. I am using
The output you see in my original post is the output of I still get the "Syntax Error" and I have pylint 2.5.0. So I don't understand why this was closed? |
I'm sorry, I made the mistake of assuming I could make a quick guess to help you to fix your environment. Pylint 2.5.0 can process the file you gave correctly if you have the right package installed alongside it. I can even reproduce the bug with your faulty environment. I'm not going to debug your environement though. But maybe it's a problem with python 3.8. Could you test what I suggested in my answer above and tell me if the problem still exists?
|
Good call about the Python version. It seems to be an issue with Python 3.8. I created two fresh environments with 3.5 and 3.8. Here's the console output: For Python 3.5
And for Python 3.8
|
I believe this is in issue with type comments parsing on Python 3.8:
We do have some code to reparse the file if the syntax errors were caused by misplaced type annotations, but in this case the error does not indicate anything related to the type comments at all: https://github.com/PyCQA/astroid/blob/master/astroid/builder.py#L446 |
See also #3757 which reports the same syntax error from Astroid on the following function definition: def func(
# type: () -> int
):
return 2 Here also, the type comment is probably misplaced. Both errors are probably related, if not the same. |
Hello! I'm on
and also experience some syntax errors on typing comments like:
each highlighting the first argument listed in the typing comment within the parenthesis with a popup:
How is this still an issue!? Why does this break on comments at ALL!? Is there a way to prevent this? I'm actually using Pylance for these kinds of inspections and it works superb. Pylance is just not interested in a lot of coding style issues that pylint nicely covers... |
Hey could you provide the example as text instead of screenshot@ewerybody ? (Edit : not ina new issue, this one is still opened, sorry) |
OK. Amended with code snippets👍 but kept the shots for context.
What do you mean? You want me to open a new issue with this? Btw: |
OK I just noticed that "fixing" it by adding |
OK. I just digged into this as well.
So. this is an astroid issue! What should we ask for there?
In the function @PCManticore was linking it seems that astroid IS actually dealing with broken parsing MISPLACED_TYPE_ANNOTATION_ERROR = "misplaced type annotation" The one that I'm having is # If the type annotations are misplaced for some reason, we do not want
# to fail the entire parsing of the file, so we need to retry the parsing without
# type comment support. After all: astroid would probably needed to be fixed to accept these kinds of type comments! |
This is very similar to #3757. However, while that issues is about ignoring incorrect type comments this one is about recognising a different set of incorrect type comments and handling those accordingly. They should probably be fixed at the same time. |
I've tested this against newer versions of Python (3.10.4) and Pylint (2.15.5) and it is still reproducible. Here's the new requirements.txt file:
And the diff against the previous file: --- requirements.txt 2022-10-29 11:18:27.580688144 +0200
+++ requirements-2022-10-29.txt 2022-10-29 11:17:57.039770141 +0200
@@ -1,19 +1,23 @@
-astroid==2.4.0
+astroid==2.12.12
attrs==19.3.0
autopep8==1.5.2
+dill==0.3.6
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
mypy==0.770
mypy-extensions==0.4.3
+platformdirs==2.5.2
psutil==5.7.0
psycopg2-binary==2.8.5
pycodestyle==2.5.0
-pylint==2.5.0
+pylint==2.15.5
six==1.14.0
SQLAlchemy==1.3.16
sqlalchemy-stubs==0.3
toml==0.10.0
-typed-ast==1.4.1
+tomli==2.0.1
+tomlkit==0.11.6
+typed-ast==1.5.4
typing-extensions==3.7.4.2
wrapt==1.12.1 Interestingly, For Python 3 projects it is anyway possible to write proper annotations for type-hinting instead of comments. Which does not suffer from the same problem: class Foo:
var = {10} # type: Set[int] <- Syntax error
var: Set[int] = {10} # ok |
This is still an issue with python2.7 style type hints. Here's a trivial repro:
Software versions used when reproducing this issue:
This was a false positive flagged in some code that copy-pasted output from a comment (I changed the type). |
I honestly think that the best path forward for addressing this is to either make this issue a warning or remove pre-python3.6 compatible type hint support entirely, given that <python3.6 is no longer supported. |
I don't have an issue with dropping 3.6 considering that it's out of support. |
Code with python 2 type hint is still valid in python 3.11. The support is being dropped on some tools like autoflake, I know because I have some big code base that are typed this way and autoflake started to remove the typing import used in the py2 type hint. But I don't have any trouble with pylint so far, so I suppose this issue is a corner case (or I would have fixed it myself). Ultimately we might have to drop the issue if no one is interested by working on it (even if theoretically it's still useful). |
Pylint also doesn't like from collections import abc # type: Any
|
Steps to reproduce
"requirements.txt" file for the code-example below:
Create a file with the following content:
Run pylint on that file.
Current behavior
pylint currently raises a syntax-error on line 14:
Expected behavior
No syntax error should be raised
pylint --version output
The text was updated successfully, but these errors were encountered: