MCPcopy Index your code
hub / github.com/python/mypy / singledispatch_register_callback

Function singledispatch_register_callback

mypy/plugins/singledispatch.py:115–143  ·  view source on GitHub ↗

Called for functools._SingleDispatchCallable.register

(ctx: MethodContext)

Source from the content-addressed store, hash-verified

113
114
115def singledispatch_register_callback(ctx: MethodContext) -> Type:
116 """Called for functools._SingleDispatchCallable.register"""
117 assert isinstance(ctx.type, Instance)
118 # TODO: check that there's only one argument
119 first_arg_type = get_proper_type(get_first_arg(ctx.arg_types))
120 if isinstance(first_arg_type, (CallableType, Overloaded)) and first_arg_type.is_type_obj():
121 # HACK: We received a class as an argument to register. We need to be able
122 # to access the function that register is being applied to, and the typeshed definition
123 # of register has it return a generic Callable, so we create a new
124 # SingleDispatchRegisterCallable class, define a __call__ method, and then add a
125 # plugin hook for that.
126
127 # is_subtype doesn't work when the right type is Overloaded, so we need the
128 # actual type
129 register_type = first_arg_type.items[0].ret_type
130 type_args = RegisterCallableInfo(register_type, ctx.type)
131 register_callable = make_fake_register_class_instance(ctx.api, type_args)
132 return register_callable
133 elif isinstance(first_arg_type, CallableType):
134 # TODO: do more checking for registered functions
135 register_function(ctx, ctx.type, first_arg_type, ctx.api.options)
136 # The typeshed stubs for register say that the function returned is Callable[..., T], even
137 # though the function returned is the same as the one passed in. We return the type of the
138 # function so that mypy can properly type check cases where the registered function is used
139 # directly (instead of through singledispatch)
140 return first_arg_type
141
142 # fallback in case we don't recognize the arguments
143 return ctx.default_return_type
144
145
146def register_function(

Callers

nothing calls this directly

Calls 7

get_proper_typeFunction · 0.90
isinstanceFunction · 0.85
register_functionFunction · 0.85
get_first_argFunction · 0.70
is_type_objMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…