44
55from typing_extensions import Final
66
7- import mypy .sametypes
87import mypy .subtypes
98import mypy .typeops
109from mypy .argmap import ArgTypeExpander
1110from mypy .erasetype import erase_typevars
1211from mypy .maptype import map_instance_to_supertype
13- from mypy .nodes import CONTRAVARIANT , COVARIANT , ArgKind
12+ from mypy .nodes import ARG_OPT , ARG_POS , CONTRAVARIANT , COVARIANT , ArgKind
1413from mypy .types import (
1514 TUPLE_LIKE_INSTANCE_NAMES ,
1615 AnyType ,
@@ -141,7 +140,9 @@ def infer_constraints(template: Type, actual: Type, direction: int) -> List[Cons
141140
142141 The constraints are represented as Constraint objects.
143142 """
144- if any (get_proper_type (template ) == get_proper_type (t ) for t in TypeState ._inferring ):
143+ if any (
144+ get_proper_type (template ) == get_proper_type (t ) for t in reversed (TypeState ._inferring )
145+ ):
145146 return []
146147 if isinstance (template , TypeAliasType ) and template .is_recursive :
147148 # This case requires special care because it may cause infinite recursion.
@@ -341,7 +342,7 @@ def is_same_constraint(c1: Constraint, c2: Constraint) -> bool:
341342 return (
342343 c1 .type_var == c2 .type_var
343344 and (c1 .op == c2 .op or skip_op_check )
344- and mypy .sametypes .is_same_type (c1 .target , c2 .target )
345+ and mypy .subtypes .is_same_type (c1 .target , c2 .target )
345346 )
346347
347348
@@ -474,9 +475,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
474475 if isinstance (actual , (CallableType , Overloaded )) and template .type .is_protocol :
475476 if template .type .protocol_members == ["__call__" ]:
476477 # Special case: a generic callback protocol
477- if not any (
478- mypy .sametypes .is_same_type (template , t ) for t in template .type .inferring
479- ):
478+ if not any (template == t for t in template .type .inferring ):
480479 template .type .inferring .append (template )
481480 call = mypy .subtypes .find_member (
482481 "__call__" , template , actual , is_operator = True
@@ -635,7 +634,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
635634 # Note that we use is_protocol_implementation instead of is_subtype
636635 # because some type may be considered a subtype of a protocol
637636 # due to _promote, but still not implement the protocol.
638- not any (mypy . sametypes . is_same_type ( template , t ) for t in template .type .inferring )
637+ not any (template == t for t in reversed ( template .type .inferring ) )
639638 and mypy .subtypes .is_protocol_implementation (instance , erased )
640639 ):
641640 template .type .inferring .append (template )
@@ -651,7 +650,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
651650 and self .direction == SUBTYPE_OF
652651 and
653652 # We avoid infinite recursion for structural subtypes also here.
654- not any (mypy . sametypes . is_same_type ( instance , i ) for i in instance .type .inferring )
653+ not any (instance == i for i in reversed ( instance .type .inferring ) )
655654 and mypy .subtypes .is_protocol_implementation (erased , instance )
656655 ):
657656 instance .type .inferring .append (instance )
@@ -734,6 +733,8 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
734733 cactual_ps = cactual .param_spec ()
735734
736735 if not cactual_ps :
736+ max_prefix_len = len ([k for k in cactual .arg_kinds if k in (ARG_POS , ARG_OPT )])
737+ prefix_len = min (prefix_len , max_prefix_len )
737738 res .append (
738739 Constraint (
739740 param_spec .id ,
0 commit comments