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

Assigning cls() does not narrow optional type #2813

Closed
matejcik opened this issue Jan 5, 2022 · 2 comments
Closed

Assigning cls() does not narrow optional type #2813

matejcik opened this issue Jan 5, 2022 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@matejcik
Copy link

matejcik commented Jan 5, 2022

Using pyright 1.1.203.
The problem is a regression, wasn't there in 1.1.202.

The following code will not typecheck:

class A:
    instance: "A" | None = None

    def __init__(self) -> None:
        self.foo = True

    @classmethod
    def get_foo(cls) -> bool:
        if cls.instance is None:
            cls.instance = cls()
        reveal_type(cls.instance)  # info: Type of "cls.instance" is "A | None"
        return cls.instance.foo  # error: "foo" is not a known member of "None"

Changing the assignment to cls.instance = A() makes the error go away.

In pyright .202, the revealed type of cls.instance is Self@A | A.

@erictraut erictraut added needs investigation Requires additional investigation to determine course of action addressed in next version Issue is fixed and will appear in next published version bug Something isn't working and removed needs investigation Requires additional investigation to determine course of action labels Jan 5, 2022
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be fixed in the next release.

@erictraut
Copy link
Collaborator

This is included in pyright 1.1.204, which I just published. It will also be included in the next release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants