Skip to content

[suggest] Fix refine turning Optional[Any] into None #8100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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