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

Method fdef_to_sig

mypyc/irbuild/mapper.py:182–229  ·  view source on GitHub ↗
(self, fdef: FuncDef, strict_dunders_typing: bool)

Source from the content-addressed store, hash-verified

180 return self.type_to_rtype(typ)
181
182 def fdef_to_sig(self, fdef: FuncDef, strict_dunders_typing: bool) -> FuncSignature:
183 if isinstance(fdef.type, CallableType):
184 arg_types = [
185 self.get_arg_rtype(typ, kind)
186 for typ, kind in zip(fdef.type.arg_types, fdef.type.arg_kinds)
187 ]
188 arg_pos_onlys = [name is None for name in fdef.type.arg_names]
189 ret = self.type_to_rtype(fdef.type.ret_type)
190 else:
191 # Handle unannotated functions
192 arg_types = [object_rprimitive for _ in fdef.arguments]
193 arg_pos_onlys = [arg.pos_only for arg in fdef.arguments]
194 # We at least know the return type for __init__ methods will be None.
195 is_init_method = fdef.name == "__init__" and bool(fdef.info)
196 if is_init_method:
197 ret = none_rprimitive
198 else:
199 ret = object_rprimitive
200
201 # mypyc FuncSignatures (unlike mypy types) want to have a name
202 # present even when the argument is position only, since it is
203 # the sole way that FuncDecl arguments are tracked. This is
204 # generally fine except in some cases (like for computing
205 # init_sig) we need to produce FuncSignatures from a
206 # deserialized FuncDef that lacks arguments. We won't ever
207 # need to use those inside of a FuncIR, so we just make up
208 # some crap.
209 if hasattr(fdef, "arguments"):
210 arg_names = [arg.variable.name for arg in fdef.arguments]
211 else:
212 arg_names = [name or "" for name in fdef.arg_names]
213
214 args = [
215 RuntimeArg(arg_name, arg_type, arg_kind, arg_pos_only)
216 for arg_name, arg_kind, arg_type, arg_pos_only in zip(
217 arg_names, fdef.arg_kinds, arg_types, arg_pos_onlys
218 )
219 ]
220
221 if not strict_dunders_typing:
222 # We force certain dunder methods to return objects to support letting them
223 # return NotImplemented. It also avoids some pointless boxing and unboxing,
224 # since tp_richcompare needs an object anyways.
225 # However, it also prevents some optimizations.
226 if fdef.name in ("__eq__", "__ne__", "__lt__", "__gt__", "__le__", "__ge__"):
227 ret = object_rprimitive
228
229 return FuncSignature(args, ret)
230
231 def is_native_module(self, module: str) -> bool:
232 """Is the given module one compiled by mypyc?"""

Callers 7

transform_func_defFunction · 0.80
transform_decoratorFunction · 0.80
handle_ext_methodFunction · 0.80
handle_non_ext_methodFunction · 0.80
prepare_func_defFunction · 0.80
prepare_fast_pathFunction · 0.80
prepare_init_methodFunction · 0.80

Calls 8

get_arg_rtypeMethod · 0.95
type_to_rtypeMethod · 0.95
RuntimeArgClass · 0.90
FuncSignatureClass · 0.90
isinstanceFunction · 0.85
zipFunction · 0.85
boolClass · 0.85
hasattrFunction · 0.85

Tested by

no test coverage detected