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