10000 Fixed a bug that resulted in incorrect type narrowing when using an `… · nejch/scip-python@991d8e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 991d8e7

Browse files
committed
Fixed a bug that resulted in incorrect type narrowing when using an in expression and the LHS operand is of type type. This situation requires some special casing.
1 parent 4c47b0b commit 991d8e7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

packages/pyright-internal/src/analyzer/typeGuards.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,12 @@ function narrowTypeForContains(evaluator: TypeEvaluator, referenceType: Type, co
14601460
return referenceSubtype;
14611461
}
14621462

1463+
// Handle "type" specially.
1464+
if (isClassInstance(concreteReferenceType) && ClassType.isBuiltIn(concreteReferenceType, 'type')) {
1465+
canNarrow = false;
1466+
return referenceSubtype;
1467+
}
1468+
14631469
if (evaluator.assignType(elementType, concreteReferenceType)) {
14641470
return referenceSubtype;
14651471
}

packages/pyright-internal/src/tests/samples/typeNarrowingIn1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,10 @@ def func5(x: str | None, y: int | None, z: dict[str, str]):
9595
reveal_type(y, expected_text="int | None")
9696
else:
9797
reveal_type(y, expected_text="Never")
98+
99+
100+
def func6(x: type):
101+
if x in (str, int, float, bool):
102+
reveal_type(x, expected_text="type")
103+
else:
104+
reveal_type(x, expected_text="type")

0 commit comments

Comments
 (0)
0