@@ -929,17 +929,19 @@ def add_method(
929
929
add_method (self .ctx , method_name , args , ret_type , self_type , tvd )
930
930
931
931
932
- def _get_attrs_init_type (typ : Instance ) -> CallableType | None :
932
+ def _get_attrs_init_type (typ : Type ) -> tuple [ Instance , CallableType ] | tuple [ None , None ] :
933
933
"""
934
934
If `typ` refers to an attrs class, gets the type of its initializer method.
935
935
"""
936
+ if not isinstance (typ , Instance ):
937
+ return None , None
936
938
magic_attr = typ .type .get (MAGIC_ATTR_NAME )
937
939
if magic_attr is None or not magic_attr .plugin_generated :
938
- return None
940
+ return None , None
939
941
init_method = typ .type .get_method ("__init__" ) or typ .type .get_method (ATTRS_INIT_NAME )
940
942
if not isinstance (init_method , FuncDef ) or not isinstance (init_method .type , CallableType ):
941
- return None
942
- return init_method .type
943
+ return None , None
944
+ return typ , init_method .type
943
945
944
946
945
947
def evolve_function_sig_callback (ctx : mypy .plugin .FunctionSigContext ) -> CallableType :
@@ -965,13 +967,11 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
965
967
inst_type = get_proper_type (inst_type )
966
968
if isinstance (inst_type , AnyType ):
967
969
return ctx .default_signature # evolve(Any, ....) -> Any
968
- # We stringify it first, so that TypeVars maintain their name.
969
970
inst_type_str = format_type_bare (inst_type )
970
971
attrs_type = inst_type .upper_bound if isinstance (inst_type , TypeVarType ) else inst_type
971
- attrs_init_type = None
972
- if isinstance (attrs_type , Instance ):
973
- attrs_init_type = _get_attrs_init_type (attrs_type )
974
- if attrs_init_type is None :
972
+
973
+ attrs_type , attrs_init_type = _get_attrs_init_type (attrs_type )
974
+ if attrs_type is None or attrs_init_type is None :
975
975
ctx .api .fail (
976
976
f'Argument 1 to "evolve" has a variable type "{ inst_type_str } " not bound to an attrs class'
977
977
if isinstance (inst_type , TypeVarType )
0 commit comments