Fixed bug in x is E type narrowing pattern where E is an enum literal and x is a supertype of E like object. This addresses #8059. (#8062)

This commit is contained in:
Eric Traut 2024-06-03 17:08:32 -07:00 committed by GitHub
parent 14a7622300
commit 46eba6ab41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -2543,7 +2543,8 @@ function narrowTypeForLiteralComparison(
}
} else if (isPositiveTest) {
if (isIsOperator || isNoneInstance(subtype)) {
return undefined;
const isSubtype = evaluator.assignType(subtype, literalType);
return isSubtype ? literalType : undefined;
}
}

View File

@ -11,8 +11,7 @@ class SomeEnum(Enum):
VALUE2 = 2
def assert_never(val: NoReturn):
...
def assert_never(val: NoReturn): ...
def func1(a: SomeEnum):
@ -69,3 +68,10 @@ def func7(a: Union[str, bool]) -> str:
elif a is True:
return "True"
return a
def func8(a: object):
if a is SomeEnum.VALUE1 or a is SomeEnum.VALUE2:
reveal_type(a, expected_text="Literal[SomeEnum.VALUE1, SomeEnum.VALUE2]")
else:
reveal_type(a, expected_text="object")