Skip to content

Commit

Permalink
[suggest] Fix refine turning Optional[Any] into None (#8100)
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored Dec 7, 2019
1 parent ac3e5ce commit 6a83e71
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,12 @@ def refine_union(t: UnionType, s: ProperType) -> Type:
one). If an element of the union is succesfully refined, we drop it
from the union in favor of the refined versions.
"""
# Don't try to do any union refining if the types are already the
# same. This prevents things like refining Optional[Any] against
# itself and producing None.
if t == s:
return t

rhs_items = s.items if isinstance(s, UnionType) else [s]

new_items = []
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/fine-grained-suggest.test
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ foo.py:4: error: unexpected EOF while parsing
# suggest: foo.optional3
# suggest: foo.optional4
# suggest: foo.optional5
# suggest: foo.optional_any
# suggest: foo.dict1
# suggest: foo.tuple1
[file foo.py]
Expand Down Expand Up @@ -1112,6 +1113,9 @@ def optional5(x: Optional[Any]):
optional5(10)
optional5(None)

def optional_any(x: Optional[Any] = None):
pass

def dict1(d: Dict[int, Any]):
pass

Expand All @@ -1138,6 +1142,7 @@ tuple1(t)
(Optional[foo.List[int]]) -> int
(Union[foo.Set[int], foo.List[int]]) -> None
(Optional[int]) -> None
(Optional[Any]) -> None
(foo.Dict[int, int]) -> None
(Tuple[int, int]) -> None
==
Expand Down

0 comments on commit 6a83e71

Please sign in to comment.