22
33from __future__ import annotations
44
5- from typing import TYPE_CHECKING , Iterable , List , Sequence
5+ from typing import TYPE_CHECKING , Iterable , List , Sequence , Union
66from typing_extensions import Final
77
88import mypy .subtypes
@@ -713,26 +713,37 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
713713 from_concat = bool (prefix .arg_types ) or suffix .from_concatenate
714714 suffix = suffix .copy_modified (from_concatenate = from_concat )
715715
716-
717716 prefix = mapped_arg .prefix
718717 length = len (prefix .arg_types )
719718 if isinstance (suffix , Parameters ) or isinstance (suffix , CallableType ):
720719 # no such thing as variance for ParamSpecs
721720 # TODO: is there a case I am missing?
722- res .append (Constraint (mapped_arg , SUPERTYPE_OF , suffix .copy_modified (
723- arg_types = suffix .arg_types [length :],
724- arg_kinds = suffix .arg_kinds [length :],
725- arg_names = suffix .arg_names [length :],
726- )))
721+ res .append (
722+ Constraint (
723+ mapped_arg ,
724+ SUPERTYPE_OF ,
725+ suffix .copy_modified (
726+ arg_types = suffix .arg_types [length :],
727+ arg_kinds = suffix .arg_kinds [length :],
728+ arg_names = suffix .arg_names [length :],
729+ ),
730+ )
731+ )
727732 elif isinstance (suffix , ParamSpecType ):
728733 suffix_prefix = suffix .prefix
729- res .append (Constraint (mapped_arg , SUPERTYPE_OF , suffix .copy_modified (
730- prefix = suffix_prefix .copy_modified (
731- arg_types = suffix_prefix .arg_types [length :],
732- arg_kinds = suffix_prefix .arg_kinds [length :],
733- arg_names = suffix_prefix .arg_names [length :]
734+ res .append (
735+ Constraint (
736+ mapped_arg ,
737+ SUPERTYPE_OF ,
738+ suffix .copy_modified (
739+ prefix = suffix_prefix .copy_modified (
740+ arg_types = suffix_prefix .arg_types [length :],
741+ arg_kinds = suffix_prefix .arg_kinds [length :],
742+ arg_names = suffix_prefix .arg_names [length :],
743+ )
744+ ),
734745 )
735- )))
746+ )
736747 else :
737748 # This case should have been handled above.
738749 assert not isinstance (tvar , TypeVarTupleType )
@@ -947,12 +958,15 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
947958 prefix_len = len (prefix .arg_types )
948959 cactual_ps = cactual .param_spec ()
949960
961+ cactual_prefix : Union [Parameters , CallableType ]
950962 if cactual_ps :
951963 cactual_prefix = cactual_ps .prefix
952964 else :
953965 cactual_prefix = cactual
954966
955- max_prefix_len = len ([k for k in cactual_prefix .arg_kinds if k in (ARG_POS , ARG_OPT )])
967+ max_prefix_len = len (
968+ [k for k in cactual_prefix .arg_kinds if k in (ARG_POS , ARG_OPT )]
969+ )
956970 prefix_len = min (prefix_len , max_prefix_len )
957971
958972 # we could check the prefixes match here, but that should be caught elsewhere.
@@ -970,13 +984,22 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
970984 )
971985 )
972986 else :
973- res .append (Constraint (param_spec , SUBTYPE_OF , cactual_ps .copy_modified (
974- prefix = cactual_prefix .copy_modified (
975- arg_types = cactual_prefix .arg_types [prefix_len :],
976- arg_kinds = cactual_prefix .arg_kinds [prefix_len :],
977- arg_names = cactual_prefix .arg_names [prefix_len :]
987+ # guaranteed due to if conditions
988+ assert isinstance (cactual_prefix , Parameters )
989+
990+ res .append (
991+ Constraint (
992+ param_spec ,
993+ SUBTYPE_OF ,
994+ cactual_ps .copy_modified (
995+ prefix = cactual_prefix .copy_modified (
996+ arg_types = cactual_prefix .arg_types [prefix_len :],
997+ arg_kinds = cactual_prefix .arg_kinds [prefix_len :],
998+ arg_names = cactual_prefix .arg_names [prefix_len :],
999+ )
1000+ ),
9781001 )
979- )))
1002+ )
9801003
9811004 # compare prefixes
9821005 cactual_prefix = cactual .copy_modified (
0 commit comments