@@ -198,6 +198,8 @@ def visit_type_var(self, left: TypeVarType) -> bool:
198
198
right = self .right
199
199
if isinstance (right , TypeVarType ) and left .id == right .id :
200
200
return True
201
+ if left .values and is_subtype (UnionType .make_simplified_union (left .values ), right ):
202
+ return True
201
203
return is_subtype (left .upper_bound , self .right )
202
204
203
205
def visit_callable_type (self , left : CallableType ) -> bool :
@@ -578,7 +580,8 @@ def is_callable_compatible(left: CallableType, right: CallableType,
578
580
ignore_return : bool = False ,
579
581
ignore_pos_arg_names : bool = False ,
580
582
check_args_covariantly : bool = False ,
581
- allow_partial_overlap : bool = False ) -> bool :
583
+ allow_partial_overlap : bool = False ,
584
+ unify_generics : bool = True ) -> bool :
582
585
"""Is the left compatible with the right, using the provided compatibility check?
583
586
584
587
is_compat:
@@ -666,6 +669,11 @@ def g(x: int) -> int: ...
666
669
667
670
If the 'some_check' function is also symmetric, the two calls would be equivalent
668
671
whether or not we check the args covariantly.
672
+
673
+ unify_generics:
674
+ If this parameter is set to False, we do not attempt to unify away TypeVars before
675
+ checking the callable. This option should be set to False only if you want to externally
676
+ handle generics in some custom way.
669
677
"""
670
678
if is_compat_return is None :
671
679
is_compat_return = is_compat
@@ -678,34 +686,35 @@ def g(x: int) -> int: ...
678
686
if right .is_type_obj () and not left .is_type_obj ():
679
687
return False
680
688
681
- # A callable L is a subtype of a generic callable R if L is a
682
- # subtype of every type obtained from R by substituting types for
683
- # the variables of R. We can check this by simply leaving the
684
- # generic variables of R as type variables, effectively varying
685
- # over all possible values.
686
-
687
- # It's okay even if these variables share ids with generic
688
- # type variables of L, because generating and solving
689
- # constraints for the variables of L to make L a subtype of R
690
- # (below) treats type variables on the two sides as independent.
691
- if left .variables :
692
- # Apply generic type variables away in left via type inference.
693
- unified = unify_generic_callable (left , right , ignore_return = ignore_return )
694
- if unified is None :
695
- return False
696
- else :
697
- left = unified
689
+ if unify_generics :
690
+ # A callable L is a subtype of a generic callable R if L is a
691
+ # subtype of every type obtained from R by substituting types for
692
+ # the variables of R. We can check this by simply leaving the
693
+ # generic variables of R as type variables, effectively varying
694
+ # over all possible values.
695
+
696
+ # It's okay even if these variables share ids with generic
697
+ # type variables of L, because generating and solving
698
+ # constraints for the variables of L to make L a subtype of R
699
+ # (below) treats type variables on the two sides as independent.
700
+ if left .variables :
701
+ # Apply generic type variables away in left via type inference.
702
+ unified = unify_generic_callable (left , right , ignore_return = ignore_return )
703
+ if unified is None :
704
+ return False
705
+ else :
706
+ left = unified
698
707
699
- # If we allow partial overlaps, we don't need to leave R generic:
700
- # if we can find even just a single typevar assignment which
701
- # would make these callables compatible, we should return True.
708
+ # If we allow partial overlaps, we don't need to leave R generic:
709
+ # if we can find even just a single typevar assignment which
710
+ # would make these callables compatible, we should return True.
702
711
703
- # So, we repeat the above checks in the opposite direction. This also
704
- # lets us preserve the 'symmetry' property of allow_partial_overlap.
705
- if allow_partial_overlap and right .variables :
706
- unified = unify_generic_callable (right , left , ignore_return = ignore_return )
707
- if unified is not None :
708
- right = unified
712
+ # So, we repeat the above checks in the opposite direction. This also
713
+ # lets us preserve the 'symmetry' property of allow_partial_overlap.
714
+ if allow_partial_overlap and right .variables :
715
+ unified = unify_generic_callable (right , left , ignore_return = ignore_return )
716
+ if unified is not None :
717
+ right = unified
709
718
710
719
# Check return types.
711
720
if not ignore_return and not is_compat_return (left .ret_type , right .ret_type ):
@@ -901,7 +910,9 @@ def new_is_compat(left: Type, right: Type) -> bool:
901
910
902
911
903
912
def unify_generic_callable (type : CallableType , target : CallableType ,
904
- ignore_return : bool ) -> Optional [CallableType ]:
913
+ ignore_return : bool ,
914
+ return_constraint_direction : int = mypy .constraints .SUBTYPE_OF ,
915
+ ) -> Optional [CallableType ]:
905
916
"""Try to unify a generic callable type with another callable type.
906
917
907
918
Return unified CallableType if successful; otherwise, return None.
@@ -914,7 +925,7 @@ def unify_generic_callable(type: CallableType, target: CallableType,
914
925
constraints .extend (c )
915
926
if not ignore_return :
916
927
c = mypy .constraints .infer_constraints (
917
- type .ret_type , target .ret_type , mypy . constraints . SUBTYPE_OF )
928
+ type .ret_type , target .ret_type , return_constraint_direction )
918
929
constraints .extend (c )
919
930
type_var_ids = [tvar .id for tvar in type .variables ]
920
931
inferred_vars = mypy .solve .solve_constraints (type_var_ids , constraints )
@@ -1036,7 +1047,8 @@ def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
1036
1047
def visit_type_var (self , left : TypeVarType ) -> bool :
1037
1048
if isinstance (self .right , TypeVarType ) and left .id == self .right .id :
1038
1049
return True
1039
- # TODO: Value restrictions
1050
+ if left .values and is_subtype (UnionType .make_simplified_union (left .values ), self .right ):
1051
+ return True
1040
1052
return is_proper_subtype (left .upper_bound , self .right )
1041
1053
1042
1054
def visit_callable_type (self , left : CallableType ) -> bool :
0 commit comments