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

Pylint fails to detect NameError from type hint #3771

Closed
turettn opened this issue Aug 10, 2020 · 0 comments · Fixed by #5184
Closed

Pylint fails to detect NameError from type hint #3771

turettn opened this issue Aug 10, 2020 · 0 comments · Fixed by #5184
Assignees
Labels
Milestone

Comments

@turettn
Copy link

turettn commented Aug 10, 2020

Steps to reproduce

Pylint the following code:

import math


class Point:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

    def distance_to(self, other: Point) -> float:
        x_diff = self.x - other.x
        y_diff = self.y - other.y
        return math.sqrt(x_diff ** 2 + y_diff ** 2)


def main() -> None:
    origin = Point(0, 0)
    pnt = Point(10, 15)
    print(f"Distance: {pnt.distance_to(origin)}")


if __name__ == "__main__":
    main()

Current behavior

Pylint is perfectly happy:

(venv) [nturett@dev core]$ pylint test.py
(venv) [nturett@dev core]$ 

Python is not:

(venv) [nturett@dev core]$ python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    class Point:
  File "test.py", line 9, in Point
    def distance_to(self, other: Point) -> float:
NameError: name 'Point' is not defined

Expected behavior

Since this code will apparently never run, I would expect that pylint would return an undefined-variable error, matching the interpreter's actions.

Interestingly, if I change the line to include a misspelling, pylint catches it:

    def distance_to(self, other: Piont) -> float:

yields:

test.py:9:33: E0602: Undefined variable 'Piont' (undefined-variable)

pylint --version output

(venv) [nturett@dev core]$ pylint --version
pylint 2.5.3
astroid 2.4.2
Python 3.7.8 (default, Jul 24 2020, 20:26:49) 
[GCC 7.3.1 20180712 (Red Hat 7.3.1-9)]
@DanielNoord DanielNoord added this to the 2.12.0 milestone Oct 18, 2021
@DanielNoord DanielNoord self-assigned this Oct 18, 2021
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Oct 18, 2021
cdce8p added a commit to cdce8p/pylint that referenced this issue Oct 21, 2021
Squashed commit of the following:

commit 3d7d5d3
Merge: f7333e2 27cabbb
Author: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Date:   Tue Oct 19 13:26:45 2021 +0200

    Merge branch 'main' into undefined-variable

commit f7333e2
Merge: 63e34c0 8e6cd18
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:37:39 2021 +0200

    Merge branch 'undefined-variable' of https://github.com/DanielNoord/pylint into undefined-variable

commit 63e34c0
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:36:32 2021 +0200

    Handle `annotations` import

commit 8e6cd18
Merge: c45d82e cd5838c
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:02:14 2021 +0200

    Merge branch 'main' into undefined-variable

commit c45d82e
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 20:32:06 2021 +0200

    Update name and docstring

commit 0f87c0e
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 17:45:51 2021 +0200

    Make ``used-before-assignment`` consider classes in method annotation and defaults
    This closes pylint-dev#3771
cdce8p added a commit to cdce8p/pylint that referenced this issue Oct 21, 2021
Squashed commit of the following:

commit 3d7d5d3
Merge: f7333e2 27cabbb
Author: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Date:   Tue Oct 19 13:26:45 2021 +0200

    Merge branch 'main' into undefined-variable

commit f7333e2
Merge: 63e34c0 8e6cd18
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:37:39 2021 +0200

    Merge branch 'undefined-variable' of https://github.com/DanielNoord/pylint into undefined-variable

commit 63e34c0
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:36:32 2021 +0200

    Handle `annotations` import

commit 8e6cd18
Merge: c45d82e cd5838c
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 22:02:14 2021 +0200

    Merge branch 'main' into undefined-variable

commit c45d82e
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 20:32:06 2021 +0200

    Update name and docstring

commit 0f87c0e
Author: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Date:   Mon Oct 18 17:45:51 2021 +0200

    Make ``used-before-assignment`` consider classes in method annotation and defaults
    This closes pylint-dev#3771
DanielNoord added a commit that referenced this issue Oct 23, 2021
… and defaults (#5184)

This closes #3771

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants