20
20
Context , Decorator , PrintStmt , BreakStmt , PassStmt , ContinueStmt ,
21
21
ComparisonExpr , StarExpr , EllipsisExpr , RefExpr , PromoteExpr ,
22
22
Import , ImportFrom , ImportAll , ImportBase ,
23
- ARG_POS , ARG_STAR , LITERAL_TYPE , MDEF , GDEF ,
23
+ ARG_POS , ARG_STAR , ARG_NAMED , ARG_NAMED_OPT , LITERAL_TYPE , MDEF , GDEF ,
24
24
CONTRAVARIANT , COVARIANT , INVARIANT ,
25
25
)
26
26
from mypy import nodes
39
39
from mypy import messages
40
40
from mypy .subtypes import (
41
41
is_subtype , is_equivalent , is_proper_subtype , is_more_precise ,
42
- restrict_subtype_away , is_subtype_ignoring_tvars , is_callable_subtype ,
42
+ restrict_subtype_away , is_subtype_ignoring_tvars , is_callable_compatible ,
43
43
unify_generic_callable , find_member
44
44
)
45
45
from mypy .maptype import map_instance_to_supertype
@@ -437,7 +437,8 @@ def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
437
437
438
438
assert isinstance (impl_type , CallableType )
439
439
assert isinstance (sig1 , CallableType )
440
- if not is_callable_subtype (impl_type , sig1 , ignore_return = True ):
440
+ if not is_callable_compatible (impl_type , sig1 ,
441
+ is_compat = is_subtype , ignore_return = True ):
441
442
self .msg .overloaded_signatures_arg_specific (i + 1 , defn .impl )
442
443
impl_type_subst = impl_type
443
444
if impl_type .variables :
@@ -3533,37 +3534,45 @@ def is_unsafe_overlapping_signatures(signature: Type, other: Type) -> bool:
3533
3534
"""
3534
3535
if isinstance (signature , CallableType ):
3535
3536
if isinstance (other , CallableType ):
3536
- # TODO varargs
3537
- # TODO keyword args
3538
- # TODO erasure
3539
3537
# TODO allow to vary covariantly
3538
+
3540
3539
# Check if the argument counts are overlapping.
3541
3540
min_args = max (signature .min_args , other .min_args )
3542
- max_args = min (len ( signature .arg_types ), len ( other .arg_types ))
3541
+ max_args = min (signature .max_positional_args ( ), other .max_positional_args ( ))
3543
3542
if min_args > max_args :
3544
3543
# Argument counts are not overlapping.
3545
3544
return False
3546
- # Signatures are overlapping iff if they are overlapping for the
3547
- # smallest common argument count.
3548
- for i in range (min_args ):
3549
- t1 = signature .arg_types [i ]
3550
- t2 = other .arg_types [i ]
3551
- if not is_overlapping_types (t1 , t2 ):
3552
- return False
3545
+
3546
+ # If one of the corresponding argument do NOT overlap,
3547
+ # then the signatures are not overlapping.
3548
+ if not is_callable_compatible (signature , other ,
3549
+ is_compat = is_overlapping_types ,
3550
+ ignore_return = True ,
3551
+ check_args_covariantly = True ):
3552
+ # TODO: this check (unlike the others) will erase types due to
3553
+ # how is_overlapping_type is implemented. This should be
3554
+ # fixed to make this check consistent with the others.
3555
+ return False
3556
+
3553
3557
# All arguments types for the smallest common argument count are
3554
3558
# overlapping => the signature is overlapping. The overlapping is
3555
3559
# safe if the return types are identical.
3556
3560
if is_same_type (signature .ret_type , other .ret_type ):
3557
3561
return False
3562
+
3558
3563
# If the first signature has more general argument types, the
3559
3564
# latter will never be called
3560
3565
if is_more_general_arg_prefix (signature , other ):
3561
3566
return False
3567
+
3562
3568
# Special case: all args are subtypes, and returns are subtypes
3563
- if ( all ( is_proper_subtype ( s , o )
3564
- for ( s , o ) in zip ( signature . arg_types , other . arg_types )) and
3565
- is_proper_subtype ( signature . ret_type , other . ret_type ) ):
3569
+ if is_callable_compatible ( signature , other ,
3570
+ is_compat = is_proper_subtype ,
3571
+ check_args_covariantly = True ):
3566
3572
return False
3573
+
3574
+ # If the first signature is NOT more precise then the second,
3575
+ # then the overlap is unsafe.
3567
3576
return not is_more_precise_signature (signature , other )
3568
3577
return True
3569
3578
@@ -3572,12 +3581,11 @@ def is_more_general_arg_prefix(t: FunctionLike, s: FunctionLike) -> bool:
3572
3581
"""Does t have wider arguments than s?"""
3573
3582
# TODO should an overload with additional items be allowed to be more
3574
3583
# general than one with fewer items (or just one item)?
3575
- # TODO check argument kinds and otherwise make more general
3576
3584
if isinstance (t , CallableType ):
3577
3585
if isinstance (s , CallableType ):
3578
- t , s = unify_generic_callables (t , s )
3579
- return all ( is_proper_subtype ( args , argt )
3580
- for argt , args in zip ( t . arg_types , s . arg_types ) )
3586
+ return is_callable_compatible (t , s ,
3587
+ is_compat = is_proper_subtype ,
3588
+ ignore_return = True )
3581
3589
elif isinstance (t , FunctionLike ):
3582
3590
if isinstance (s , FunctionLike ):
3583
3591
if len (t .items ()) == len (s .items ()):
@@ -3586,29 +3594,6 @@ def is_more_general_arg_prefix(t: FunctionLike, s: FunctionLike) -> bool:
3586
3594
return False
3587
3595
3588
3596
3589
- def unify_generic_callables (t : CallableType ,
3590
- s : CallableType ) -> Tuple [CallableType ,
3591
- CallableType ]:
3592
- """Make type variables in generic callables the same if possible.
3593
-
3594
- Return updated callables. If we can't unify the type variables,
3595
- return the unmodified arguments.
3596
- """
3597
- # TODO: Use this elsewhere when comparing generic callables.
3598
- if t .is_generic () and s .is_generic ():
3599
- t_substitutions = {}
3600
- s_substitutions = {}
3601
- for tv1 , tv2 in zip (t .variables , s .variables ):
3602
- # Are these something we can unify?
3603
- if tv1 .id != tv2 .id and is_equivalent_type_var_def (tv1 , tv2 ):
3604
- newdef = TypeVarDef .new_unification_variable (tv2 )
3605
- t_substitutions [tv1 .id ] = TypeVarType (newdef )
3606
- s_substitutions [tv2 .id ] = TypeVarType (newdef )
3607
- return (cast (CallableType , expand_type (t , t_substitutions )),
3608
- cast (CallableType , expand_type (s , s_substitutions )))
3609
- return t , s
3610
-
3611
-
3612
3597
def is_equivalent_type_var_def (tv1 : TypeVarDef , tv2 : TypeVarDef ) -> bool :
3613
3598
"""Are type variable definitions equivalent?
3614
3599
@@ -3624,26 +3609,22 @@ def is_equivalent_type_var_def(tv1: TypeVarDef, tv2: TypeVarDef) -> bool:
3624
3609
3625
3610
3626
3611
def is_same_arg_prefix (t : CallableType , s : CallableType ) -> bool :
3627
- # TODO check argument kinds
3628
- return all (is_same_type (argt , args )
3629
- for argt , args in zip (t .arg_types , s .arg_types ))
3612
+ return is_callable_compatible (t , s ,
3613
+ is_compat = is_same_type ,
3614
+ ignore_return = True ,
3615
+ check_args_covariantly = True ,
3616
+ ignore_pos_arg_names = True )
3630
3617
3631
3618
3632
3619
def is_more_precise_signature (t : CallableType , s : CallableType ) -> bool :
3633
3620
"""Is t more precise than s?
3634
3621
3635
3622
A signature t is more precise than s if all argument types and the return
3636
3623
type of t are more precise than the corresponding types in s.
3637
-
3638
- Assume that the argument kinds and names are compatible, and that the
3639
- argument counts are overlapping.
3640
3624
"""
3641
- # TODO generic function types
3642
- # Only consider the common prefix of argument types.
3643
- for argt , args in zip (t .arg_types , s .arg_types ):
3644
- if not is_more_precise (argt , args ):
3645
- return False
3646
- return is_more_precise (t .ret_type , s .ret_type )
3625
+ return is_callable_compatible (t , s ,
3626
+ is_compat = is_more_precise ,
3627
+ check_args_covariantly = True )
3647
3628
3648
3629
3649
3630
def infer_operator_assignment_method (typ : Type , operator : str ) -> Tuple [bool , str ]:
0 commit comments