8000 [suggest] Support refining existing type annotations by msullivan · Pull Request #7838 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[suggest] Support refining existing type annotations #7838

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 10 commits into from
Nov 2, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename merge_types to refine_type
  • Loading branch information
msullivan committed Nov 2, 2019
commit 8eae7e024c72caf7b4e79e47ba8f75aa98a29841
26 changes: 13 additions & 13 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def get_guesses(self, is_method: bool, base: CallableType, defaults: List[Option
"""
options = self.get_args(is_method, base, defaults, callsites, uses)
options = [self.add_adjustments(tps) for tps in options]
return [merge_callables(base, base.copy_modified(arg_types=list(x)))
return [refine_callable(base, base.copy_modified(arg_types=list(x)))
for x in itertools.product(*options)]

def get_callsites(self, func: FuncDef) -> Tuple[List[Callsite], List[str]]:
Expand Down Expand Up @@ -439,7 +439,7 @@ def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature:
else:
ret_types = [NoneType()]

guesses = [merge_callables(best, best.copy_modified(ret_type=t)) for t in ret_types]
guesses = [refine_callable(best, best.copy_modified(ret_type=t)) for t in ret_types]
guesses = self.filter_options(guesses, is_method)
best, errors = self.find_best(node, guesses)

Expand Down Expand Up @@ -851,8 +851,8 @@ def count_errors(msgs: List[str]) -> int:
T = TypeVar('T')


def merge_types(ti: Type, si: Type) -> Type:
"""Merge two callable types in a left-biased Any dropping way.
def refine_type(ti: Type, si: Type) -> Type:
"""Refine `ti` by replacing Anys in it with information taken from `si`

XXX: More
"""
Expand All @@ -863,18 +863,18 @@ def merge_types(ti: Type, si: Type) -> Type:
return s

if isinstance(t, Instance) and isinstance(s, Instance) and t.type == s.type:
return t.copy_modified(args=[merge_types(ta, sa) for ta, sa in zip(t.args, s.args)])
return t.copy_modified(args=[refine_type(ta, sa) for ta, sa in zip(t.args, s.args)])

if (
isinstance(t, TupleType)
and isinstance(s, TupleType)
and t.partial_fallback == s.partial_fallback
and len(t.items) == len(s.items)
):
return t.copy_modified(items=[merge_types(ta, sa) for ta, sa in zip(t.items, s.items)])
return t.copy_modified(items=[refine_type(ta, sa) for ta, sa in zip(t.items, s.items)])

if isinstance(t, CallableType) and isinstance(s, CallableType):
return merge_callables(t, s)
return refine_callable(t, s)

if isinstance(t, UnionType) and any(isinstance(x, AnyType) for x in t.items):
# TODO: Should we try to go deeper??
Expand All @@ -888,23 +888,23 @@ def merge_types(ti: Type, si: Type) -> Type:
return t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add couple special cases (not important however) for type and tuple. These two are represented by instances, but can be refined by Type[C] and Tuple[X, Y, ...].



def merge_callables(t: CallableType, s: CallableType) -> CallableType:
"""Merge two callable types in a left-biased Any dropping way.
def refine_callable(t: CallableType, s: CallableType) -> CallableType:
"""Refine a callable based on another.

See comments for merge_types.
See comments for refine_type.
"""
if t.fallback != s.fallback:
return t

if t.is_ellipsis_args and not is_tricky_callable(s):
return s.copy_modified(ret_type=merge_types(t.ret_type, s.ret_type))
return s.copy_modified(ret_type=refine_type(t.ret_type, s.ret_type))

if is_tricky_callable(t) or t.arg_kinds != s.arg_kinds:
return t

return t.copy_modified(
arg_types=[merge_types(ta, sa) for ta, sa in zip(t.arg_types, s.arg_types)],
ret_type=merge_types(t.ret_type, s.ret_type),
arg_types=[refine_type(ta, sa) for ta, sa in zip(t.arg_types, s.arg_types)],
ret_type=refine_type(t.ret_type, s.ret_type),
)


Expand Down
0