62
62
LiteralType ,
63
63
NoneType ,
64
64
Overloaded ,
65
+ ProperType ,
65
66
TupleType ,
66
67
Type ,
67
68
TypeOfAny ,
@@ -929,13 +930,10 @@ def add_method(
929
930
add_method (self .ctx , method_name , args , ret_type , self_type , tvd )
930
931
931
932
932
- def _get_attrs_init_type (typ : Type ) -> CallableType | None :
933
+ def _get_attrs_init_type (typ : Instance ) -> CallableType | None :
933
934
"""
934
935
If `typ` refers to an attrs class, gets the type of its initializer method.
935
936
"""
936
- typ = get_proper_type (typ )
937
- if not isinstance (typ , Instance ):
938
- return None
939
937
magic_attr = typ .type .get (MAGIC_ATTR_NAME )
940
938
if magic_attr is None or not magic_attr .plugin_generated :
941
939
return None
@@ -945,6 +943,14 @@ def _get_attrs_init_type(typ: Type) -> CallableType | None:
945
943
return init_method .type
946
944
947
945
946
+ def _get_attrs_cls_and_init (typ : ProperType ) -> tuple [Instance | None , CallableType | None ]:
947
+ if isinstance (typ , TypeVarType ):
948
+ typ = get_proper_type (typ .upper_bound )
949
+ if not isinstance (typ , Instance ):
950
+ return None , None
951
+ return typ , _get_attrs_init_type (typ )
952
+
953
+
948
954
def evolve_function_sig_callback (ctx : mypy .plugin .FunctionSigContext ) -> CallableType :
949
955
"""
950
956
Generates a signature for the 'attr.evolve' function that's specific to the call site
@@ -967,13 +973,15 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
967
973
968
974
inst_type = get_proper_type (inst_type )
969
975
if isinstance (inst_type , AnyType ):
970
- return ctx .default_signature
976
+ return ctx .default_signature # evolve(Any, ....) -> Any
971
977
inst_type_str = format_type_bare (inst_type )
972
978
973
- attrs_init_type = _get_attrs_init_type (inst_type )
974
- if not attrs_init_type :
979
+ attrs_type , attrs_init_type = _get_attrs_cls_and_init (inst_type )
980
+ if attrs_type is None or attrs_init_type is None :
975
981
ctx .api .fail (
976
- f'Argument 1 to "evolve" has incompatible type "{ inst_type_str } "; expected an attrs class' ,
982
+ f'Argument 1 to "evolve" has a variable type "{ inst_type_str } " not bound to an attrs class'
983
+ if isinstance (inst_type , TypeVarType )
984
+ else f'Argument 1 to "evolve" has incompatible type "{ inst_type_str } "; expected an attrs class' ,
977
985
ctx .context ,
978
986
)
979
987
return ctx .default_signature
0 commit comments