Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,15 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
)
return AnyType(TypeOfAny.from_error)

bound_method = analyze_decorator_or_funcbase_access(
defn=dunder_get,
itype=descriptor_type,
name="__get__",
mx=mx.copy_modified(self_type=descriptor_type),
)

typ = map_instance_to_supertype(descriptor_type, dunder_get.info)
dunder_get_type = expand_type_by_instance(bound_method, typ)
# Use the unbound __get__ type and pass (self, instance, owner) together so that
# type inference can jointly solve all type variables. The bound-method approach
# (bind_self first, then call with 2 args) fails when the __get__ self annotation
# contains type variables that appear in the instance parameter too, because
# bind_self may not be able to unify them correctly against complex callable types.
dunder_get_func = dunder_get.func if isinstance(dunder_get, Decorator) else dunder_get
typ = function_type(dunder_get_func, mx.chk.named_type("builtins.function"))
typ_for_instance = map_instance_to_supertype(descriptor_type, dunder_get_func.info)
dunder_get_type: Type = expand_type_by_instance(typ, typ_for_instance)

if isinstance(instance_type, FunctionLike) and instance_type.is_type_obj():
owner_type = instance_type.items[0].ret_type
Expand All @@ -734,21 +734,23 @@ def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type:
callable_name,
dunder_get_type,
[
TempNode(orig_descriptor_type, context=mx.context),
TempNode(instance_type, context=mx.context),
TempNode(TypeType.make_normalized(owner_type), context=mx.context),
],
[ARG_POS, ARG_POS],
[ARG_POS, ARG_POS, ARG_POS],
mx.context,
object_type=descriptor_type,
)

_, inferred_dunder_get_type = mx.chk.expr_checker.check_call(
dunder_get_type,
[
TempNode(orig_descriptor_type, context=mx.context),
TempNode(instance_type, context=mx.context),
TempNode(TypeType.make_normalized(owner_type), context=mx.context),
],
[ARG_POS, ARG_POS],
[ARG_POS, ARG_POS, ARG_POS],
mx.context,
object_type=descriptor_type,
callable_name=callable_name,
Expand Down
Loading