Skip to content

Commit 8ade002

Browse files
committed
type-safety
1 parent cbd71ee commit 8ade002

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

mypy/plugins/attrs.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -929,17 +929,19 @@ def add_method(
929929
add_method(self.ctx, method_name, args, ret_type, self_type, tvd)
930930

931931

932-
def _get_attrs_init_type(typ: Instance) -> CallableType | None:
932+
def _get_attrs_init_type(typ: Type) -> tuple[Instance, CallableType] | tuple[None, None]:
933933
"""
934934
If `typ` refers to an attrs class, gets the type of its initializer method.
935935
"""
936+
if not isinstance(typ, Instance):
937+
return None, None
936938
magic_attr = typ.type.get(MAGIC_ATTR_NAME)
937939
if magic_attr is None or not magic_attr.plugin_generated:
938-
return None
940+
return None, None
939941
init_method = typ.type.get_method("__init__") or typ.type.get_method(ATTRS_INIT_NAME)
940942
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
943945

944946

945947
def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> CallableType:
@@ -965,13 +967,11 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
965967
inst_type = get_proper_type(inst_type)
966968
if isinstance(inst_type, AnyType):
967969
return ctx.default_signature # evolve(Any, ....) -> Any
968-
# We stringify it first, so that TypeVars maintain their name.
969970
inst_type_str = format_type_bare(inst_type)
970971
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:
975975
ctx.api.fail(
976976
f'Argument 1 to "evolve" has a variable type "{inst_type_str}" not bound to an attrs class'
977977
if isinstance(inst_type, TypeVarType)

0 commit comments

Comments
 (0)