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

Function register_function

mypy/plugins/singledispatch.py:146–180  ·  view source on GitHub ↗

Register a function

(
    ctx: PluginContext,
    singledispatch_obj: Instance,
    func: Type,
    options: Options,
    register_arg: Type | None = None,
)

Source from the content-addressed store, hash-verified

144
145
146def register_function(
147 ctx: PluginContext,
148 singledispatch_obj: Instance,
149 func: Type,
150 options: Options,
151 register_arg: Type | None = None,
152) -> None:
153 """Register a function"""
154
155 func = get_proper_type(func)
156 if not isinstance(func, CallableType):
157 return
158 metadata = get_singledispatch_info(singledispatch_obj)
159 if metadata is None:
160 # if we never added the fallback to the type variables, we already reported an error, so
161 # just don't do anything here
162 return
163 dispatch_type = get_dispatch_type(func, register_arg)
164 if dispatch_type is None:
165 # TODO: report an error here that singledispatch requires at least one argument
166 # (might want to do the error reporting in get_dispatch_type)
167 return
168 fallback = metadata.fallback
169
170 fallback_dispatch_type = fallback.arg_types[0]
171 if not is_subtype(dispatch_type, fallback_dispatch_type):
172 fail(
173 ctx,
174 "Dispatch type {} must be subtype of fallback function first argument {}".format(
175 format_type(dispatch_type, options), format_type(fallback_dispatch_type, options)
176 ),
177 func.definition,
178 )
179 return
180 return
181
182
183def get_dispatch_type(func: CallableType, register_arg: Type | None) -> Type | None:

Calls 8

get_proper_typeFunction · 0.90
is_subtypeFunction · 0.90
format_typeFunction · 0.90
isinstanceFunction · 0.85
get_singledispatch_infoFunction · 0.85
get_dispatch_typeFunction · 0.85
failFunction · 0.70
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…