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
[1.20 regression] Inconsistent narroving for unions involving Any #21364
Yeah, this will be related to my change for custom equality
I guess there are two issues here: 1) inconsistency between union case and non union case, 2) violation of gradual typing principle
For the inconsistency, these should both either be Any or Any | B | int
from typing import Any
class B: ...
def f1(x: B | int, y: Any):
if x == y:
reveal_type(x) # B | int
reveal_type(y) # Any
def f2(x: B | int, y: Any | None):
if x == y:
reveal_type(x) # B | int
reveal_type(y) # Any | B | int
In general I really do like how willing mypy is to turn Any into stricter types, but I think in this case preserving gradual guarantee and just sticking with Any will work better (certainly on your example). I will still check primer for the Any | B | int option to see if I learn something interesting...
Activity
hauntsaninja commentedon Apr 29, 2026
Yeah, this will be related to my change for custom equality
I guess there are two issues here: 1) inconsistency between union case and non union case, 2) violation of gradual typing principle
For the inconsistency, these should both either be
AnyorAny | B | intIn general I really do like how willing mypy is to turn
Anyinto stricter types, but I think in this case preserving gradual guarantee and just sticking withAnywill work better (certainly on your example). I will still check primer for theAny | B | intoption to see if I learn something interesting...hauntsaninja commentedon Apr 29, 2026
Okay I see, this will fix the issue mentioned in this comment #21199 (comment)
hauntsaninja commentedon Apr 29, 2026
Okay yes, primer for the non-union change confirms preserving gradual guarantee here is important in practice as well
Preserve gradual guarantee when narrowing Any union via equality (#21368
Preserve gradual guarantee when narrowing Any union via equality (#21368