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

Type narrowing behaves oddly in presence of type.__eq__ #4419

Closed
mniip opened this issue Jan 7, 2023 · 2 comments
Closed

Type narrowing behaves oddly in presence of type.__eq__ #4419

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

Comments

@mniip
Copy link

mniip commented Jan 7, 2023

Describe the bug
In some situations an x: Union[X, Y, ...] that has been previously narrowed to Y incorrectly unnarrows back to x: Union[X, Y]. This happens seemingly when we branch on something == X.

To Reproduce
Run pyright on the code from the Code section.

  a.py:12:18 - error: Cannot access member "p" for type "X"
    Member "p" is unknown (reportGeneralTypeIssues)
  a.py:11:25 - information: Type of "x" is "Y"

Expected behavior
No error.

Screenshots or Code

from typing import Union

class X:
    pass
class Y:
    p: type

def f(xs: list[Union[X, Y]]) -> None:
    for x in xs:
        if not isinstance(x, X):
            reveal_type(x)
            if x.p == X:
                pass

VS Code extension or command-line
Commandline pyright 1.1.287

Additional context
Works on pyright 1.1.285

@erictraut erictraut added the bug Something isn't working label Jan 8, 2023
erictraut pushed a commit that referenced this issue Jan 8, 2023
… certain cases when found within a loop construct. This addresses #4419.
@erictraut
Copy link
Collaborator

erictraut commented Jan 8, 2023

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

This was a long-time bug that was previously masked by the way the type narrowing logic was called. A change that I made in 1.1.286 caused it to come to light.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jan 8, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.288, which I just published. It will also be included in a future 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