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

Enhanced type narrowing logic for "x == <literal>" type guard pattern… #9318

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ function narrowTypeForLiteralComparison(
isPositiveTest: boolean,
isIsOperator: boolean
): Type {
return mapSubtypes(referenceType, (subtype) => {
return evaluator.mapSubtypesExpandTypeVars(referenceType, /* options */ undefined, (subtype) => {
subtype = evaluator.makeTopLevelTypeVarsConcrete(subtype);

if (isAnyOrUnknown(subtype)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the type analyzer's type narrowing
# logic for literals.

from typing import Literal, Union
from typing import Literal, TypeVar, Union


def func1(p1: Literal["a", "b", "c"]):
Expand All @@ -28,3 +28,27 @@ def func2(p1: Literal[1, 4, 7]):
def func3(a: Union[int, None]):
if a == 1 or a == 2:
reveal_type(a, expected_text="Literal[1, 2]")


T = TypeVar("T", bound=Literal["a", "b"])


def func4(x: T) -> T:
if x == "a":
reveal_type(x, expected_text="Literal['a']")
return x
else:
reveal_type(x, expected_text="Literal['b']")
return x


S = TypeVar("S", Literal["a"], Literal["b"])


def func5(x: S) -> S:
if x == "a":
reveal_type(x, expected_text="Literal['a']")
return x
else:
reveal_type(x, expected_text="Literal['b']")
return x
Loading