DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.02k stars 19 forks source link

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

Open KotlinIsland opened 4 months ago

KotlinIsland commented 4 months ago
def f(a: int | None, b: int):
    if a == b:
        reveal_type(a)  # int | None
    if b == a:
        reveal_type(a)  # int | None
DetachHead commented 4 months ago

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: https://github.com/microsoft/pyright/issues/8065

DetachHead commented 3 months ago

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 commented 3 months ago

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