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
False positive with F401 (imported but unused) for type comments #1619
Comments
Is this in reference to the type comment? Like, that |
Yeah exactly! mypy will complain that it isn't defined if the import is missing, i.e.
|
Yeah we don't really support type comments right now, since they were made obsolete in Python 3.6 AFAIK. I know some projects still use them for compatibility... I'll think on it. |
@charliermarsh maybe 1 area that this is still useful in Python 3.6+: type hinting the result of a context manager. Example: from types_aiobotocore_s3 import S3ServiceResource
async with self.boto_session.resource("s3") as s3: # type: S3ServiceResource
obj = await s3.Object(self.bucket_name, key)
return (await obj.get())["Body"] The Looks like leaving it off or on is ok by Mypy, but type hinting at least in Pycharm is better with the type comment. |
Maybe the issue title can be change to Affects me as well. |
I’d say the comment looking nicer than a type annotation is an editor/highlighter problem, but the general point of this being useful is correct. |
Similar to #1619 (comment), I still use type comments in some very rare cases where things like from models import Wheel
for wheel in car.wheels.all(): # type: Wheel
... I realize this is non-standard, but PyCharm will interpret this and provide intellisense/auto-complete for the loop variable, which is practically useful in these niche cases. |
All type comments can be replaced (c.f.: https://peps.python.org/pep-0526/#where-annotations-aren-t-allowed) from types_aiobotocore_s3 import S3ServiceResource
async with self.boto_session.resource("s3") as s3: # type: S3ServiceResource
obj = await s3.Object(self.bucket_name, key)
return (await obj.get())["Body"]
# Becomes
s3: S3ServiceResource
async with self.boto_session.resource("s3") as s3:
obj = await s3.Object(self.bucket_name, key)
return (await obj.get())["Body"] from models import Wheel
for wheel in car.wheels.all(): # type: Wheel
...
# Becomes
wheel: Wheel
for wheel in car.wheels.all():
... |
@JonathanPlasse Good point bringing that up. I've generally been a little resistant to this particular fix because it complicates the actual code just to add the hint:
But you're right that this is the officially prescribed way of dealing with it, but hopefully the above points help explain why people may be hesitant to do it just for a hint. |
Also, it's worth mentioning that if you unpack values in this manner: n: str
p: Tensor
r: Tensor
f1: Tensor
for n, p, r, f1 in zip(names, precision, recall, f1_micro):
... it seems unnecessarily complicated for such a simple functionality compared to a simpler approach like: for n, p, r, f1 in zip(names, precision, recall, f1_micro): # type: str, Tensor, Tensor, Tensor
... |
I stumbled onto this issue in the configparser project. I worked around it by converting the type comments to native type hints (jaraco/configparser@4990ba1). |
They are not obsolete, they are part of PEP 484 specification and are supported by all major type checkers (pyright, mypy, pyre, etc) There are a ton of projects that leverage them, i.e. numpy. |
To be clear, "obsolete" is different from "unused". I'm not claiming that type comments aren't used in existing projects or that type checkers do not support them. It would be nice to support them! But it's a matter of prioritization, and my understanding is that PEP 526 had the explicit goal of introducing "a more readable syntax to replace type comments." Perhaps another way of framing my lack of urgency to prioritize this: is there any reason to use type comments when writing Python code today? (This is not a rhetorical question, I am genuinely asking.) |
@charliermarsh personally I use them exclusively, (see my previous comment #1619 (comment)), purely because I think they make code much more readable. But thats just my 2c 🤷🏼♂️ |
As explained in the link below, for the rare cases of needing to type hint loop variables or |
The company where I work has multi-million line codebase and we just migrated to using It's not possible to migrate the whole codebase in a one go to the py3 comments unfortunately (there are technical reasons why we cannot do that). |
(Thank you, these comments are all welcome and helpful.) |
FWIW flake8 has the same problem (tested on flake8 6.1.0) |
pyflakes (what Flake8 runs under the hood) used to support this (I used the feature for years before moving to Ruff). Perhaps there was a regression. |
It looks like it was removed about a year ago: PyCQA/pyflakes#684 |
minimal example (of some project code):
This isn't a big deal since
# type: List[str]
can be replaced with# type: list[str]
and mypy warns aboutList
being undefined with the auto fixer removes the importThe text was updated successfully, but these errors were encountered: