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

undefined-variable on correct return type hints #3461

Closed
craynic opened this issue Mar 27, 2020 · 10 comments
Closed

undefined-variable on correct return type hints #3461

craynic opened this issue Mar 27, 2020 · 10 comments
Labels
Bug 🪲 Minor 💅 Polishing pylint is always nice

Comments

@craynic
Copy link

craynic commented Mar 27, 2020

Steps to reproduce

code:

from enum import Enum


class Right:
    class Result1(Enum):
        OK = 0
    def work(self) -> Result1:
        return self.Result1.OK


class Wrong:
    class Result2(Enum):
        OK = 0
    def work(self) -> self.Result2:
        return self.Result2.OK

Got an error in running:

Traceback (most recent call last):
  File "tmp.py", line 11, in <module>
    class Wrong:
  File "tmp.py", line 14, in Wrong
    def work(self) -> self.Result2:
NameError: name 'self' is not defined

Current behavior

tmp.py:7:22: E0602: Undefined variable 'Result1' (undefined-variable)

Expected behavior

tmp.py:14:22: E0602: Undefined variable 'self' (undefined-variable)

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.8.2 (default, Feb 26 2020, 22:21:03)

@ghost
Copy link

ghost commented Mar 27, 2020

The "right" code isn't really correct. You'd need to forward the reference: def work(self) -> "Result1":.

@craynic
Copy link
Author

craynic commented Mar 30, 2020

The "right" code isn't really correct. You'd need to forward the reference: def work(self) -> "Result1":.

I could run it with python with no problem. So that's correct.

If that's really not good enough, the message level should be W, not E.

@craynic
Copy link
Author

craynic commented Apr 3, 2020

any update?

@ghost
Copy link

ghost commented Apr 3, 2020

I could run it with python with no problem. So that's correct.

You're right, my bad, mixed something up. It appears that pylint cannot handle type hints very well. def work(self) -> "Result1": seems to satisfy pylint but so does def work(self) -> "FooBar": which it shouldn't, of course.

@PCManticore
Copy link
Contributor

Thanks for the report. This is definitely a bug, and it's unrelated to type hints. It's caused by pylint not properly scoping annotations in the enclosing scope.

For instance, if the code would be written like this, the error would not be emitted:

from enum import Enum

class Result1(Enum):
   OK = 0

class Right:
    def work(self) -> Result1:
        return Result1.OK

@PCManticore PCManticore added Bug 🪲 Minor 💅 Polishing pylint is always nice labels Apr 3, 2020
@craynic
Copy link
Author

craynic commented May 21, 2020

It seems to have been fixed in 2.5. Is it right?
If yes, hope a link to the PR or commit could be given.

@svenpanne
Copy link

No, this has not been fixed yet. I've just tried to convert some parts of our code base with https://github.com/ilevkivskyi/com2ann and stumbled over quite a few places which had to be fixed by hand (adding quotes) because pylint doesn't get the scoping right.

It is quite common that signatures of methods in a class Foo involve, well, Foo, so this pylint bug is really annoying in a large code base...

@svenpanne
Copy link

One more point: pylint should somehow treat typing.TYPE_CHECKING like mypy does, otherwise the scoping is screwed up for another reason.

@anjsimmo
Copy link
Contributor

It seems to have been fixed in 2.5. Is it right?
If yes, hope a link to the PR or commit could be given.

@craynic Yes, the false positive for Result1 that you reported was fixed by PR #3497 and released in 2.5.0. I've submitted PR #3713 which should also fix the false negative for self.Result2 once merged.

(If converting a large code base, Pylint may encounter other type hint and scoping related issues--some of which have bug reports--but I think the specific case in this issue is now addressed)

Pierre-Sassoulas pushed a commit that referenced this issue Jul 12, 2020
)

Fix scoping for function annotations, decorators and base classes

Closes #1082, #3434, #3461

Reduce number of branches in variables checker

Co-authored-by: Andrew Simmons <[email protected]>
@Pierre-Sassoulas
Copy link
Member

Fixed in #3713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Minor 💅 Polishing pylint is always nice
Projects
None yet
Development

No branches or pull requests

5 participants