Register a function
(
ctx: PluginContext,
singledispatch_obj: Instance,
func: Type,
options: Options,
register_arg: Type | None = None,
)
| 144 | |
| 145 | |
| 146 | def 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 | |
| 183 | def get_dispatch_type(func: CallableType, register_arg: Type | None) -> Type | None: |
no test coverage detected
searching dependent graphs…