|
42 | 42 | restrict_subtype_away, is_subtype_ignoring_tvars, is_callable_compatible,
|
43 | 43 | unify_generic_callable, find_member
|
44 | 44 | )
|
| 45 | +from mypy.constraints import SUPERTYPE_OF |
45 | 46 | from mypy.maptype import map_instance_to_supertype
|
46 | 47 | from mypy.typevars import fill_typevars, has_no_typevars
|
47 | 48 | from mypy.semanal import set_callable_name, refers_to_fullname, calculate_mro
|
@@ -414,6 +415,23 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
|
414 | 415 | def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
|
415 | 416 | # At this point we should have set the impl already, and all remaining
|
416 | 417 | # items are decorators
|
| 418 | + |
| 419 | + # Compute some info about the implementation (if it exists) for use below |
| 420 | + impl_type = None # type: Optional[CallableType] |
| 421 | + if defn.impl: |
| 422 | + if isinstance(defn.impl, FuncDef): |
| 423 | + inner_type = defn.impl.type |
| 424 | + elif isinstance(defn.impl, Decorator): |
| 425 | + inner_type = defn.impl.var.type |
| 426 | + else: |
| 427 | + assert False, "Impl isn't the right type" |
| 428 | + |
| 429 | + # This can happen if we've got an overload with a different |
| 430 | + # decorator or if the implementation is untyped -- we gave up on the types. |
| 431 | + if inner_type is not None and not isinstance(inner_type, AnyType): |
| 432 | + assert isinstance(inner_type, CallableType) |
| 433 | + impl_type = inner_type |
| 434 | + |
417 | 435 | is_descriptor_get = defn.info is not None and defn.name() == "__get__"
|
418 | 436 | for i, item in enumerate(defn.items):
|
419 | 437 | # TODO overloads involving decorators
|
@@ -451,43 +469,35 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
|
451 | 469 | self.msg.overloaded_signatures_overlap(
|
452 | 470 | i + 1, i + j + 2, item.func)
|
453 | 471 |
|
454 |
| - if defn.impl: |
455 |
| - if isinstance(defn.impl, FuncDef): |
456 |
| - impl_type = defn.impl.type |
457 |
| - elif isinstance(defn.impl, Decorator): |
458 |
| - impl_type = defn.impl.var.type |
459 |
| - else: |
460 |
| - assert False, "Impl isn't the right type" |
| 472 | + if impl_type is not None: |
| 473 | + assert defn.impl is not None |
461 | 474 |
|
462 |
| - # This can happen if we've got an overload with a different |
463 |
| - # decorator too -- we gave up on the types. |
464 |
| - if impl_type is None or isinstance(impl_type, AnyType): |
465 |
| - return |
466 |
| - assert isinstance(impl_type, CallableType) |
| 475 | + # We perform a unification step that's very similar to what |
| 476 | + # 'is_callable_compatible' would have done if we had set |
| 477 | + # 'unify_generics' to True -- the only difference is that |
| 478 | + # we check and see if the impl_type's return value is a |
| 479 | + # *supertype* of the overload alternative, not a *subtype*. |
| 480 | + # |
| 481 | + # This is to match the direction the implementation's return |
| 482 | + # needs to be compatible in. |
| 483 | + if impl_type.variables: |
| 484 | + impl = unify_generic_callable(impl_type, sig1, |
| 485 | + ignore_return=False, |
| 486 | + return_constraint_direction=SUPERTYPE_OF) |
| 487 | + if impl is None: |
| 488 | + self.msg.overloaded_signatures_typevar_specific(i + 1, defn.impl) |
| 489 | + continue |
| 490 | + else: |
| 491 | + impl = impl_type |
467 | 492 |
|
468 | 493 | # Is the overload alternative's arguments subtypes of the implementation's?
|
469 |
| - if not is_callable_compatible(impl_type, sig1, |
| 494 | + if not is_callable_compatible(impl, sig1, |
470 | 495 | is_compat=is_subtype,
|
471 | 496 | ignore_return=True):
|
472 | 497 | self.msg.overloaded_signatures_arg_specific(i + 1, defn.impl)
|
473 | 498 |
|
474 |
| - # Repeat the same unification process 'is_callable_compatible' |
475 |
| - # internally performs so we can examine the return type separately. |
476 |
| - if impl_type.variables: |
477 |
| - # Note: we set 'ignore_return=True' because 'unify_generic_callable' |
478 |
| - # normally checks the arguments and return types with differing variance. |
479 |
| - # |
480 |
| - # This is normally what we want, but for checking the validity of overload |
481 |
| - # implementations, we actually want to use the same variance for both. |
482 |
| - # |
483 |
| - # TODO: Patch 'is_callable_compatible' and 'unify_generic_callable'? |
484 |
| - # somehow so we can customize the variance in all different sorts |
485 |
| - # of ways? This would let us infer more constraints, letting us |
486 |
| - # infer more precise types. |
487 |
| - impl_type = unify_generic_callable(impl_type, sig1, ignore_return=True) |
488 |
| - |
489 | 499 | # Is the overload alternative's return type a subtype of the implementation's?
|
490 |
| - if impl_type is not None and not is_subtype(sig1.ret_type, impl_type.ret_type): |
| 500 | + if not is_subtype(sig1.ret_type, impl.ret_type): |
491 | 501 | self.msg.overloaded_signatures_ret_specific(i + 1, defn.impl)
|
492 | 502 |
|
493 | 503 | # Here's the scoop about generators and coroutines.
|
|
0 commit comments