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

narrowing should work on values that are arent literals if their type is @final #391

Open
KotlinIsland opened this issue Jun 3, 2024 · 3 comments
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@KotlinIsland
Copy link
Collaborator

KotlinIsland commented Jun 3, 2024

def f(a: int | None, b: int):
    if a == b:
        reveal_type(a)  # int | None
    if b == a:
        reveal_type(a)  # int | None
@KotlinIsland KotlinIsland changed the title assert doesn't narrow properly assert with equals doesn't narrow Jun 3, 2024
@DetachHead DetachHead changed the title assert with equals doesn't narrow narrowing only works on literals Jun 4, 2024
@DetachHead
Copy link
Owner

DetachHead commented Jun 4, 2024

the issue is actually that it only narrows literals:

def f(a: int | None, b: int):
    if a == 1:
        reveal_type(a)  # Literal[1]
    if a == b:
        reveal_type(a)  # int | None

upstream issue: microsoft/pyright#8065

@DetachHead DetachHead added upstream issue (awaiting response) rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it and removed upstream issue (awaiting response) labels Jun 4, 2024
@DetachHead DetachHead added the type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules label Jun 16, 2024
@DetachHead
Copy link
Owner

actually this would be unsafe because a subtype of int could define a custom __eq__. so this should only work if the class is @final

@DetachHead DetachHead changed the title narrowing only works on literals narrowing should work on values that are arent literals if their type is @final Jun 25, 2024
@DetachHead
Copy link
Owner

narrowing with is instead of == should also work because it doesn't use __eq__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
Development

No branches or pull requests

2 participants