Skip to content

Undefined variable __class__ as return type when using annotations #2640

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

Closed
mattclay opened this issue Dec 11, 2018 · 3 comments
Closed

Undefined variable __class__ as return type when using annotations #2640

mattclay opened this issue Dec 11, 2018 · 3 comments
Labels

Comments

@mattclay
Copy link

Steps to reproduce

Using __class__ as a method return type results in an undefined variable error, unless the type is also used in the arguments. Using it only for arguments works as expected.

#!/usr/bin/env python3.7

from __future__ import annotations


class Demo:
    def __init__(self, number):
        self.number = number

    # OK
    def become(self, demo: __class__) -> None:
        self.number = demo.number

    # OK
    def combine(self, demo: __class__) -> __class__:
        return Demo(self.number + demo.number)

    # demo.py:19:27: E0602: Undefined variable '__class__' (undefined-variable)
    def increment(self) -> __class__:
        return Demo(self.number + 1)

Current behavior

demo.py:19:27: E0602: Undefined variable '__class__' (undefined-variable)

Expected behavior

No undefined variable error.

pylint --version output

__main__.py 2.2.2
astroid 2.1.0
Python 3.7.0 (default, Sep  5 2018, 11:18:59) 
[Clang 9.1.0 (clang-902.0.39.2)]
@mattclay
Copy link
Author

May be related to #1976

@PCManticore
Copy link
Contributor

Right, this makes sense and it's a bug, but it's quite an edge case with __class__.

@PCManticore
Copy link
Contributor

This no longer reproduces with the unreleased version:

pylint 2.5.0-dev1
astroid 2.4.0
Python 3.8.1 (default, Feb 13 2020, 10:17:07)
[Clang 8.1.0 (clang-802.0.42)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants