Skip to content
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

from __future__ import annotations ignored #6523

Closed
clo-vis opened this issue May 6, 2022 · 3 comments · Fixed by #6536
Closed

from __future__ import annotations ignored #6523

clo-vis opened this issue May 6, 2022 · 3 comments · Fixed by #6536
Assignees
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check False Positive 🦟 A message is emitted but nothing is wrong with the code typing
Milestone

Comments

@clo-vis
Copy link

clo-vis commented May 6, 2022

Bug description

from __future__ import annotations
from csv import DictWriter
from typing import IO


def f_is_for_fun(file: IO[str]) -> DictWriter[str]:
    return DictWriter(file, ["id", "name"])

Reference #6519

Command used

pylint playground.py

Pylint output

************* Module playground
…
playground.py:6:35: E1136: Value 'DictWriter' is unsubscriptable (unsubscriptable-object)

Expected behavior

No error, as the python interpreter doesn't complain, nor does mypy, and #3320 is closed.

Pylint version

pylint 2.13.8
astroid 2.11.4
Python 3.9.12 (tags/v3.9.12:b28265d, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)]
@clo-vis clo-vis added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 6, 2022
@Pierre-Sassoulas Pierre-Sassoulas added typing C: unsubscriptable-object Issues related to 'unsubscriptable-object' check labels May 6, 2022
@cdce8p
Copy link
Member

cdce8p commented May 6, 2022

Sorry for dismissing your first issue that early. At the moment, pylint checks if the classname is in a predefined set of allowed subscriptable names since we don't check the pyi files. It only contains the classes explicitly mentioned in PEP 585. Thus DictWriter but also others like Popen #4369 or Queue do emit a warning.
https://github.com/PyCQA/pylint/blob/7e1b9ae5f974d03e8a584631f85002127cb9405e/pylint/checkers/utils.py#L1402

Since we can't know if a class is subscriptable in typeshed, it might be time to remove the PEP 585 name check.

@cdce8p cdce8p self-assigned this May 6, 2022
@cdce8p cdce8p added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 6, 2022
@hmc-cs-mdrissi
Copy link
Contributor

More broadly generic type may not be from typeshed but from other .pyi files which may be defined in arbitrary locations (well depends on type checker's search paths for stubs). As an example mypy supports MYPY_PATH allowing defining other places for type stubs separate from python path. pyright similarly has it's own setting for possible places .pyi files can be placed. I use custom type stubs for 3rd party libraries that lack type hints.

@cdce8p
Copy link
Member

cdce8p commented May 7, 2022

@hmc-cs-mdrissi You're right of course. Pylint isn't a type checker and thus doesn't use .pyi files at all.
Probably the best way forward for us is thus to ignore all unsubscriptable-object errors in string annotations and with from __future__ import annotations. Those don't result in runtime errors and a type checker would catch them anyway.

I've opened #6536 to make that change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: unsubscriptable-object Issues related to 'unsubscriptable-object' check False Positive 🦟 A message is emitted but nothing is wrong with the code typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants