You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type narrowing on object attribute with match statement doesn't apply #14911
Expected behavior
By doing a match with Test(a = int()), python will check test is of type Test AND test.a is of type int.
But mypy "only assert" that test if of type Test. It keep the type int | str for test.a.
Would it be possible to support narrowing across cases like this as part of this issue, or would it be a separate issue/not possible? I searched the issue tracker and this issue seemed to be the closest to what I'm looking for.
Would it be possible to support narrowing across cases like this as part of this issue, or would it be a separate issue/not possible? I searched the issue tracker and this issue seemed to be the closest to what I'm looking for.
@tjkuson With #19736 it would almost work. Just assert_never would still throw an error as I haven't implemented the "else" narrowing. Similarly to how it isn't implemented for just the bound variable yet.
Activity
erictraut commentedon Mar 16, 2023
For reference: microsoft/pyright#4775
cdce8p commentedon Aug 27, 2025
This should be fixed with #19736.
tjkuson commentedon Sep 24, 2025
Would it be possible to support narrowing across cases like this as part of this issue, or would it be a separate issue/not possible? I searched the issue tracker and this issue seemed to be the closest to what I'm looking for.
This works if I go back to using
isinstancewith ordinary if statements, but that would mean being unable to use pattern matching features.cdce8p commentedon Sep 24, 2025
@tjkuson With #19736 it would almost work. Just
assert_neverwould still throw an error as I haven't implemented the "else" narrowing. Similarly to how it isn't implemented for just the bound variable yet.... case Test(a=a): reveal_type(a) # int | str