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

Incorrect "undefined variable" error for class variables as return type annotations #1976

Closed
nicktimko opened this issue Mar 29, 2018 · 4 comments · Fixed by #3497
Closed
Labels

Comments

@nicktimko
Copy link

nicktimko commented Mar 29, 2018

pylint seems to incorrectly identify an undefined variable error when using a class variable as a return type annotation. Argument type annotations appear to be OK.

Steps to reproduce

# lintcheck.py
class MyObject:
    class MyType:
        pass
    def my_method(self) -> MyType:
        pass

Current behavior

$ pylint lintcheck.py -E
No config file found, using default configuration
************* Module lintcheck
E:  5,27: Undefined variable 'MyType' (undefined-variable)

Expected behavior

No error.

Curiously, the below doesn't emit any errors, where MyType should have identical scope:

# lintcheck_ok.py
class MyObject2:
    class MyType:
        pass
    def my_method(self, x: MyType):
        pass
    def my_thing(self, x=MyType):
        pass

pylint --version output

$ pylint --version
No config file found, using default configuration
pylint 1.8.3, 
astroid 1.6.2
Python 3.6.4 (default, Mar 26 2018, 16:21:07) 
[GCC 5.4.0 20160609]
@PCManticore
Copy link
Contributor

Thanks, I can reproduce the issue!

anjsimmo added a commit to anjsimmo/pylint that referenced this issue Apr 20, 2020
anjsimmo added a commit to anjsimmo/pylint that referenced this issue Apr 20, 2020
anjsimmo added a commit to anjsimmo/pylint that referenced this issue Apr 20, 2020
PCManticore pushed a commit that referenced this issue Apr 22, 2020
@failable
Copy link

I can't get this work

class Dataset(object):
    T = TypeVar("T", bound="Dataset")

    @classmethod
    def from_dir(cls: Type[T], dirname: str) -> T:                    # Undefined variable 'T'
        ...

Am I doing wrong? Thanks!

@zhiltsov-max
Copy link

The same thing for

class Project:
    MyType = NewType('MyType', str)

    def foo(self, obj: MyType):                    # Undefined variable 'MyType'

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.

5 participants