88from typing import Any
99
1010
11+ from . import _eval_operators
1112from . import _eval_typing
1213from . import _typing_inspect
1314from ._eval_operators import _callable_type_to_signature
@@ -79,13 +80,16 @@ def _get_bound_type_args_from_bound_args(
7980 or _typing_inspect .is_generic_alias (param .annotation )
8081 ):
8182 param_value = bound .arguments [param .name ]
82- _update_bound_typevar (param .annotation , param_value , vars )
83+ _update_bound_typevar (
84+ param .name , param .annotation , param_value , vars
85+ )
8386 # TODO: simple bindings to other variables too
8487
8588 return vars
8689
8790
8891def _update_bound_typevar (
92+ param_name : str ,
8993 tv : Any ,
9094 param_value : Any ,
9195 vars : dict [str , RtType ],
@@ -99,13 +103,19 @@ def _update_bound_typevar(
99103 f"is already bound to { vars [tv .__name__ ].__name__ } , "
100104 f"but got { param_value .__name__ } "
101105 )
102- elif bool (
103- _typing_inspect .is_generic_alias (tv )
104- and _typing_inspect .is_generic_alias (param_value )
105- and tv .__origin__ == param_value .__origin__
106- ):
107- for p_arg , c_arg in zip (tv .__args__ , param_value .__args__ , strict = True ):
108- _update_bound_typevar (p_arg , c_arg , vars )
106+ elif _typing_inspect .is_generic_alias (tv ):
107+ tv_args = tv .__args__
108+
109+ with _eval_typing ._ensure_context () as ctx :
110+ param_args = _eval_operators ._get_args (
111+ param_value , tv .__origin__ , ctx
112+ )
113+
114+ if param_args is None :
115+ raise ValueError (f"Argument type mismatch for { param_name } " )
116+
117+ for p_arg , c_arg in zip (tv_args , param_args , strict = True ):
118+ _update_bound_typevar (param_name , p_arg , c_arg , vars )
109119
110120
111121def eval_call_with_types (
0 commit comments