MCPcopy
hub / github.com/python-attrs/attrs / _AnnotationExtractor

Class _AnnotationExtractor

src/attr/_compat.py:40–77  ·  view source on GitHub ↗

Extract type annotations from a callable, returning None whenever there is none.

Source from the content-addressed store, hash-verified

38
39
40class _AnnotationExtractor:
41 """
42 Extract type annotations from a callable, returning None whenever there
43 is none.
44 """
45
46 __slots__ = ["sig"]
47
48 def __init__(self, callable):
49 try:
50 self.sig = inspect.signature(callable)
51 except (ValueError, TypeError): # inspect failed
52 self.sig = None
53
54 def get_first_param_type(self):
55 """
56 Return the type annotation of the first argument if it's not empty.
57 """
58 if not self.sig:
59 return None
60
61 params = list(self.sig.parameters.values())
62 if params and params[0].annotation is not inspect.Parameter.empty:
63 return params[0].annotation
64
65 return None
66
67 def get_return_type(self):
68 """
69 Return the return type if it's not empty.
70 """
71 if (
72 self.sig
73 and self.sig.return_annotation is not inspect.Signature.empty
74 ):
75 return self.sig.return_annotation
76
77 return None
78
79
80# Thread-local global to track attrs instances which are already being repr'd.

Callers 4

__init__Method · 0.85
pipeFunction · 0.85
optionalFunction · 0.85

Calls

no outgoing calls

Tested by 1