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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylint doesn't complain about undefined name in string type annotation #7394

Closed
lggruspe opened this issue Sep 1, 2022 · 3 comments
Closed
Labels
Control flow Requires control flow understanding False Negative 馃 No message is emitted but something is wrong with the code typing Won't fix/not planned

Comments

@lggruspe
Copy link
Contributor

lggruspe commented Sep 1, 2022

Bug description

example.py:

# pylint: disable=missing-docstring

example1: undefined
# 馃槃 pylint complains

example2: "undefined"
# 馃槩 pylint doesn't

Configuration

No response

Command used

pylint example.py

Pylint output

************* Module example
example.py:3:10: E0602: Undefined variable 'undefined' (undefined-variable)

-----------------------------------
Your code has been rated at 0.00/10

Expected behavior

I think pylint should complain in both cases.

Pylint version

pylint 2.15.0
astroid 2.12.5
Python 3.10.6 (main, Aug  2 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)]

OS / Environment

No response

Additional dependencies

No response

@lggruspe lggruspe added the Needs triage 馃摜 Just created, needs acknowledgment, triage, and proper labelling label Sep 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Control flow Requires control flow understanding typing False Negative 馃 No message is emitted but something is wrong with the code and removed Needs triage 馃摜 Just created, needs acknowledgment, triage, and proper labelling labels Sep 1, 2022
@Pierre-Sassoulas
Copy link
Member

The typical use case for this is "undefined" being defined in a if TYPE_CHECKING: guard ie.:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import defined_conditionally

example1: defined_conditionally
# 馃槃 pylint complains

example2: "defined_conditionally"
# 馃槩 pylint doesn't

But pylint does not understand control flow well right now so we avoided a false positive at the cost of a false negative here. If we handle control flow better in astroid in the future then we could do this without the false positive. (I would advice checking string type annotation with mypy or pyright that are type checker).

@Pierre-Sassoulas Pierre-Sassoulas added the Blocked 馃毀 Blocked by a particular issue label Sep 1, 2022
@cdce8p
Copy link
Member

cdce8p commented Oct 8, 2022

Quoted annotations are just strings at runtime and won't break anything. This will run just fine.

example2: "undefined"

In contrast here undefined is an unresolved reference and Python would raise a NameError.

example1: undefined

As @Pierre-Sassoulas already suggested, to validate annotations, type checkers (like mypy or pyright) are much better and both will issue warnings for it.

I suggest we close this issue.

@Pierre-Sassoulas Pierre-Sassoulas closed this as not planned Won't fix, can't repro, duplicate, stale Oct 8, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Won't fix/not planned and removed Blocked 馃毀 Blocked by a particular issue labels Oct 8, 2022
@Pierre-Sassoulas
Copy link
Member

This this could be fixed if #4795. Closing for the reason Marc listed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Control flow Requires control flow understanding False Negative 馃 No message is emitted but something is wrong with the code typing Won't fix/not planned
Projects
None yet
Development

No branches or pull requests

3 participants