Skip to content

Commit

Permalink
Make join and meet symmetric with strict_optional (#18227)
Browse files Browse the repository at this point in the history
Fixes #18199 

Updated `join.py` to return `AnyType(TypeOfAny.special_form)` for both
`UnboundType` and `AnyType`.

Updated `meet.py` to return `UninhabitedType()` when visiting a
`UnboundType` as a `NoneType` when `state.strict_optional` is true.
  • Loading branch information
MechanicalConstruct authored Dec 2, 2024
1 parent e666217 commit 1a95964
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def visit_none_type(self, t: NoneType) -> ProperType:
if state.strict_optional:
if isinstance(self.s, (NoneType, UninhabitedType)):
return t
elif isinstance(self.s, UnboundType):
elif isinstance(self.s, (UnboundType, AnyType)):
return AnyType(TypeOfAny.special_form)
else:
return mypy.typeops.make_simplified_union([self.s, t])
Expand Down
2 changes: 1 addition & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def __init__(self, s: ProperType) -> None:
def visit_unbound_type(self, t: UnboundType) -> ProperType:
if isinstance(self.s, NoneType):
if state.strict_optional:
return AnyType(TypeOfAny.special_form)
return UninhabitedType()
else:
return self.s
elif isinstance(self.s, UninhabitedType):
Expand Down
6 changes: 2 additions & 4 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,7 @@ def test_any_type(self) -> None:
self.fx.anyt,
self.fx.a,
self.fx.o,
# TODO: fix this is not currently symmetric
# NoneType(),
NoneType(),
UnboundType("x"),
self.fx.t,
self.tuple(),
Expand Down Expand Up @@ -1177,8 +1176,7 @@ def test_none(self) -> None:
self.assert_meet(self.fx.o, NoneType(), NoneType())
for t in [
self.fx.a,
# TODO: fix this is not currently symmetric
# UnboundType("x"),
UnboundType("x"),
self.fx.t,
self.tuple(),
self.callable(self.fx.a, self.fx.b),
Expand Down

0 comments on commit 1a95964

Please sign in to comment.