Skip to content

Commit 6420e27

Browse files
JiwaniZakirclaude
andcommitted
Add regression test for value-in-container narrowing widening (#21512)
Adds a test case to check-narrowing.test to cover the regression where `x in container` narrowing would widen x to the container's item type even when that item type is not a proper subtype of x's declared type. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent dbea92a commit 6420e27

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

test-data/unit/check-narrowing.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,3 +4281,17 @@ def func(y: H) -> H:
42814281
else:
42824282
return y
42834283
[builtins fixtures/primitives.pyi]
4284+
4285+
[case testValueInContainerNoWideningToNonSubtype]
4286+
# Regression test for https://github.com/python/mypy/issues/21512
4287+
# Narrowing x in `if x in container` must not widen x to the container item
4288+
# type when that item type is not a subtype of x's declared type.
4289+
class Size(tuple[int, ...]):
4290+
def numel(self) -> int: ...
4291+
4292+
sizes: list[Size]
4293+
value = (1, 2, 3)
4294+
if value in sizes:
4295+
reveal_type(value) # N: Revealed type is "tuple[builtins.int, builtins.int, builtins.int]"
4296+
value.numel() # E: "tuple[int, int, int]" has no attribute "numel"
4297+
[builtins fixtures/primitives.pyi]

0 commit comments

Comments
 (0)