Skip to content

Commit 6a83e71

Browse files
authored
[suggest] Fix refine turning Optional[Any] into None (#8100)
1 parent ac3e5ce commit 6a83e71

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

mypy/suggestions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,12 @@ def refine_union(t: UnionType, s: ProperType) -> Type:
970970
one). If an element of the union is succesfully refined, we drop it
971971
from the union in favor of the refined versions.
972972
"""
973+
# Don't try to do any union refining if the types are already the
974+
# same. This prevents things like refining Optional[Any] against
975+
# itself and producing None.
976+
if t == s:
977+
return t
978+
973979
rhs_items = s.items if isinstance(s, UnionType) else [s]
974980

975981
new_items = []

test-data/unit/fine-grained-suggest.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,7 @@ foo.py:4: error: unexpected EOF while parsing
10301030
# suggest: foo.optional3
10311031
# suggest: foo.optional4
10321032
# suggest: foo.optional5
1033+
# suggest: foo.optional_any
10331034
# suggest: foo.dict1
10341035
# suggest: foo.tuple1
10351036
[file foo.py]
@@ -1112,6 +1113,9 @@ def optional5(x: Optional[Any]):
11121113
optional5(10)
11131114
optional5(None)
11141115

1116+
def optional_any(x: Optional[Any] = None):
1117+
pass
1118+
11151119
def dict1(d: Dict[int, Any]):
11161120
pass
11171121

@@ -1138,6 +1142,7 @@ tuple1(t)
11381142
(Optional[foo.List[int]]) -> int
11391143
(Union[foo.Set[int], foo.List[int]]) -> None
11401144
(Optional[int]) -> None
1145+
(Optional[Any]) -> None
11411146
(foo.Dict[int, int]) -> None
11421147
(Tuple[int, int]) -> None
11431148
==

0 commit comments

Comments
 (0)