Called for functools._SingleDispatchCallable.register
(ctx: MethodContext)
| 113 | |
| 114 | |
| 115 | def 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 | |
| 146 | def register_function( |
nothing calls this directly
no test coverage detected
searching dependent graphs…