MCPcopy
hub / github.com/python/mypy / bind_self

Function bind_self

mypy/typeops.py:407–504  ·  view source on GitHub ↗

Return a copy of `method`, with the type of its first parameter (usually self or cls) bound to original_type. If the type of `self` is a generic type (T, or Type[T] for classmethods), instantiate every occurrence of type with original_type in the rest of the signature and in the ret

(
    method: F,
    original_type: Type | None = None,
    is_classmethod: bool = False,
    ignore_instances: bool = False,
)

Source from the content-addressed store, hash-verified

405
406
407def bind_self(
408 method: F,
409 original_type: Type | None = None,
410 is_classmethod: bool = False,
411 ignore_instances: bool = False,
412) -> F:
413 """Return a copy of `method`, with the type of its first parameter (usually
414 self or cls) bound to original_type.
415
416 If the type of `self` is a generic type (T, or Type[T] for classmethods),
417 instantiate every occurrence of type with original_type in the rest of the
418 signature and in the return type.
419
420 original_type is the type of E in the expression E.copy(). It is None in
421 compatibility checks. In this case we treat it as the erasure of the
422 declared type of self.
423
424 This way we can express "the type of self". For example:
425
426 T = TypeVar('T', bound='A')
427 class A:
428 def copy(self: T) -> T: ...
429
430 class B(A): pass
431
432 b = B().copy() # type: B
433
434 """
435 if isinstance(method, Overloaded):
436 items = [
437 bind_self(c, original_type, is_classmethod, ignore_instances) for c in method.items
438 ]
439 return cast(F, Overloaded(items))
440 assert isinstance(method, CallableType)
441 func: CallableType = method
442 if not func.arg_types:
443 # Invalid method, return something.
444 return method
445 if func.arg_kinds[0] in (ARG_STAR, ARG_STAR2):
446 # The signature is of the form 'def foo(*args, ...)'.
447 # In this case we shouldn't drop the first arg,
448 # since func will be absorbed by the *args.
449 # TODO: infer bounds on the type of *args?
450
451 # In the case of **kwargs we should probably emit an error, but
452 # for now we simply skip it, to avoid crashes down the line.
453 return method
454 self_param_type = get_proper_type(func.arg_types[0])
455
456 variables: Sequence[TypeVarLikeType]
457 # Having a def __call__(self: Callable[...], ...) can cause infinite recursion. Although
458 # this special-casing looks not very principled, there is nothing meaningful we can infer
459 # from such definition, since it is inherently indefinitely recursive.
460 allow_callable = func.name is None or not func.name.startswith("__call__ of")
461 if func.variables and supported_self_type(
462 self_param_type, allow_callable=allow_callable, allow_instances=not ignore_instances
463 ):
464 from mypy.infer import infer_type_arguments

Callers 10

expand_and_bind_callableFunction · 0.90
add_class_tvarsFunction · 0.90
find_node_typeFunction · 0.90
visit_func_defMethod · 0.90

Calls 14

OverloadedClass · 0.90
get_proper_typeFunction · 0.90
infer_type_argumentsFunction · 0.90
TypeTypeClass · 0.90
UninhabitedTypeClass · 0.90
expand_typeFunction · 0.90
isinstanceFunction · 0.85
supported_self_typeFunction · 0.85
erase_to_boundFunction · 0.85
get_all_type_varsFunction · 0.85
anyFunction · 0.85
zipFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…