Debian Codegraph @

rpython/rlib/signature.py

raw symbols SCIP
1 from rpython.rlib import types
2
3
4 def signature(*paramtypes, **kwargs):
5 """Decorate a function to specify its type signature.
6
7 Usage:
8 @signature(param1type, param2type, ..., returns=returntype)
9 def foo(...)
10
11 The arguments paramNtype and returntype should be instances
12 of the classes in rpython.rlib.types.
13 """
14 returntype = kwargs.pop('returns', None)
15 if returntype is None:
16 raise TypeError("signature: parameter 'returns' required")
17
18 def decorator(f):
19 f._signature_ = (paramtypes, returntype)
20 return f
21 return decorator
22
23
24 def finishsigs(cls):
25 """Decorate a class to finish any method signatures involving types.self().
26
27 This is required if any method has a signature with types.self() in it.
28 """
29 # A bit annoying to have to use this, but it avoids performing any
30 # terrible hack in the implementation. Eventually we'll offer signatures
31 # on classes, and then that decorator can do this on the side.
32 def fix(sigtype):
33 if isinstance(sigtype, types.SelfTypeMarker):
34 return types.instance(cls)
35 return sigtype
36 for attr in cls.__dict__.values():
37 if hasattr(attr, '_signature_'):
38 paramtypes, returntype = attr._signature_
39 attr._signature_ = (tuple(fix(t) for t in paramtypes), fix(returntype))
40 return cls
41
42
43 class FieldSpec(object):
44 def __init__(self, tp):
45 pass
46
47
48 class ClassSpec(object):
49 def __init__(self, fields, inherit=False):
50 pass