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 :
@@ -3524,37 +3525,45 @@ def is_unsafe_overlapping_signatures(signature: Type, other: Type) -> bool:
3524
3525
"""
3525
3526
if isinstance (signature , CallableType ):
3526
3527
if isinstance (other , CallableType ):
3527
- # TODO varargs
3528
- # TODO keyword args
3529
- # TODO erasure
3530
3528
# TODO allow to vary covariantly
3529
+
3531
3530
# Check if the argument counts are overlapping.
3532
3531
min_args = max (signature .min_args , other .min_args )
3533
- max_args = min (len ( signature .arg_types ), len ( other .arg_types ))
3532
+ max_args = min (signature .max_positional_args ( ), other .max_positional_args ( ))
3534
3533
if min_args > max_args :
3535
3534
# Argument counts are not overlapping.
3536
3535
return False
3537
- # Signatures are overlapping iff if they are overlapping for the
3538
- # smallest common argument count.
3539
- for i in range (min_args ):
3540
- t1 = signature .arg_types [i ]
3541
- t2 = other .arg_types [i ]
3542
- if not is_overlapping_types (t1 , t2 ):
3543
- return False
3536
+
3537
+ # If one of the corresponding argument do NOT overlap,
3538
+ # then the signatures are not overlapping.
3539
+ if not is_callable_compatible (signature , other ,
3540
+ is_compat = is_overlapping_types ,
3541
+ ignore_return = True ,
3542
+ check_args_covariantly = True ):
3543
+ # TODO: this check (unlike the others) will erase types due to
3544
+ # how is_overlapping_type is implemented. This should be
3545
+ # fixed to make this check consistent with the others.
3546
+ return False
3547
+
3544
3548
# All arguments types for the smallest common argument count are
3545
3549
# overlapping => the signature is overlapping. The overlapping is
3546
3550
# safe if the return types are identical.
3547
3551
if is_same_type (signature .ret_type , other .ret_type ):
3548
3552
return False
3553
+
3549
3554
# If the first signature has more general argument types, the
3550
3555
# latter will never be called
3551
3556
if is_more_general_arg_prefix (signature , other ):
3552
3557
return False
3558
+
3553
3559
# Special case: all args are subtypes, and returns are subtypes
3554
- if ( all ( is_proper_subtype ( s , o )
3555
- for ( s , o ) in zip ( signature . arg_types , other . arg_types )) and
3556
- is_proper_subtype ( signature . ret_type , other . ret_type ) ):
3560
+ if is_callable_compatible ( signature , other ,
3561
+ is_compat = is_proper_subtype ,
3562
+ check_args_covariantly = True ):
3557
3563
return False
3564
+
3565
+ # If the first signature is NOT more precise then the second,
3566
+ # then the overlap is unsafe.
3558
3567
return not is_more_precise_signature (signature , other )
3559
3568
return True
3560
3569
@@ -3563,12 +3572,11 @@ def is_more_general_arg_prefix(t: FunctionLike, s: FunctionLike) -> bool:
3563
3572
"""Does t have wider arguments than s?"""
3564
3573
# TODO should an overload with additional items be allowed to be more
3565
3574
# general than one with fewer items (or just one item)?
3566
- # TODO check argument kinds and otherwise make more general
3567
3575
if isinstance (t , CallableType ):
3568
3576
if isinstance (s , CallableType ):
3569
- t , s = unify_generic_callables (t , s )
3570
- return all ( is_proper_subtype ( args , argt )
3571
- for argt , args in zip ( t . arg_types , s . arg_types ) )
3577
+ return is_callable_compatible (t , s ,
3578
+ is_compat = is_proper_subtype ,
3579
+ ignore_return = True )
3572
3580
elif isinstance (t , FunctionLike ):
3573
3581
if isinstance (s , FunctionLike ):
3574
3582
if len (t .items ()) == len (s .items ()):
@@ -3577,29 +3585,6 @@ def is_more_general_arg_prefix(t: FunctionLike, s: FunctionLike) -> bool:
3577
3585
return False
3578
3586
3579
3587
3580
- def unify_generic_callables (t : CallableType ,
3581
- s : CallableType ) -> Tuple [CallableType ,
3582
- CallableType ]:
3583
- """Make type variables in generic callables the same if possible.
3584
-
3585
- Return updated callables. If we can't unify the type variables,
3586
- return the unmodified arguments.
3587
- """
3588
- # TODO: Use this elsewhere when comparing generic callables.
3589
- if t .is_generic () and s .is_generic ():
3590
- t_substitutions = {}
3591
- s_substitutions = {}
3592
- for tv1 , tv2 in zip (t .variables , s .variables ):
3593
- # Are these something we can unify?
3594
- if tv1 .id != tv2 .id and is_equivalent_type_var_def (tv1 , tv2 ):
3595
- newdef = TypeVarDef .new_unification_variable (tv2 )
3596
- t_substitutions [tv1 .id ] = TypeVarType (newdef )
3597
- s_substitutions [tv2 .id ] = TypeVarType (newdef )
3598
- return (cast (CallableType , expand_type (t , t_substitutions )),
3599
- cast (CallableType , expand_type (s , s_substitutions )))
3600
- return t , s
3601
-
3602
-
3603
3588
def is_equivalent_type_var_def (tv1 : TypeVarDef , tv2 : TypeVarDef ) -> bool :
3604
3589
"""Are type variable definitions equivalent?
3605
3590
@@ -3615,26 +3600,22 @@ def is_equivalent_type_var_def(tv1: TypeVarDef, tv2: TypeVarDef) -> bool:
3615
3600
3616
3601
3617
3602
def is_same_arg_prefix (t : CallableType , s : CallableType ) -> bool :
3618
- # TODO check argument kinds
3619
- return all (is_same_type (argt , args )
3620
- for argt , args in zip (t .arg_types , s .arg_types ))
3603
+ return is_callable_compatible (t , s ,
3604
+ is_compat = is_same_type ,
3605
+ ignore_return = True ,
3606
+ check_args_covariantly = True ,
3607
+ ignore_pos_arg_names = True )
3621
3608
3622
3609
3623
3610
def is_more_precise_signature (t : CallableType , s : CallableType ) -> bool :
3624
3611
"""Is t more precise than s?
3625
3612
3626
3613
A signature t is more precise than s if all argument types and the return
3627
3614
type of t are more precise than the corresponding types in s.
3628
-
3629
- Assume that the argument kinds and names are compatible, and that the
3630
- argument counts are overlapping.
3631
3615
"""
3632
- # TODO generic function types
3633
- # Only consider the common prefix of argument types.
3634
- for argt , args in zip (t .arg_types , s .arg_types ):
3635
- if not is_more_precise (argt , args ):
3636
- return False
3637
- return is_more_precise (t .ret_type , s .ret_type )
3616
+ return is_callable_compatible (t , s ,
3617
+ is_compat = is_more_precise ,
3618
+ check_args_covariantly = True )
3638
3619
3639
3620
3640
3621
def infer_operator_assignment_method (typ : Type , operator : str ) -> Tuple [bool , str ]:
0 commit comments