MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / get_annotations

Function get_annotations

tools/python-3.11.9-amd64/Lib/inspect.py:167–279  ·  view source on GitHub ↗

Compute the annotations dict for an object. obj may be a callable, class, or module. Passing in an object of any other type raises TypeError. Returns a dict. get_annotations() returns a new dict every time it's called; calling it twice on the same object will return two

(obj, *, globals=None, locals=None, eval_str=False)

Source from the content-addressed store, hash-verified

165
166
167def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
168 """Compute the annotations dict for an object.
169
170 obj may be a callable, class, or module.
171 Passing in an object of any other type raises TypeError.
172
173 Returns a dict. get_annotations() returns a new dict every time
174 it's called; calling it twice on the same object will return two
175 different but equivalent dicts.
176
177 This function handles several details for you:
178
179 * If eval_str is true, values of type str will
180 be un-stringized using eval(). This is intended
181 for use with stringized annotations
182 ("from __future__ import annotations").
183 * If obj doesn't have an annotations dict, returns an
184 empty dict. (Functions and methods always have an
185 annotations dict; classes, modules, and other types of
186 callables may not.)
187 * Ignores inherited annotations on classes. If a class
188 doesn't have its own annotations dict, returns an empty dict.
189 * All accesses to object members and dict values are done
190 using getattr() and dict.get() for safety.
191 * Always, always, always returns a freshly-created dict.
192
193 eval_str controls whether or not values of type str are replaced
194 with the result of calling eval() on those values:
195
196 * If eval_str is true, eval() is called on values of type str.
197 * If eval_str is false (the default), values of type str are unchanged.
198
199 globals and locals are passed in to eval(); see the documentation
200 for eval() for more information. If either globals or locals is
201 None, this function may replace that value with a context-specific
202 default, contingent on type(obj):
203
204 * If obj is a module, globals defaults to obj.__dict__.
205 * If obj is a class, globals defaults to
206 sys.modules[obj.__module__].__dict__ and locals
207 defaults to the obj class namespace.
208 * If obj is a callable, globals defaults to obj.__globals__,
209 although if obj is a wrapped function (using
210 functools.update_wrapper()) it is first unwrapped.
211 """
212 if isinstance(obj, type):
213 # class
214 obj_dict = getattr(obj, '__dict__', None)
215 if obj_dict and hasattr(obj_dict, 'get'):
216 ann = obj_dict.get('__annotations__', None)
217 if isinstance(ann, types.GetSetDescriptorType):
218 ann = None
219 else:
220 ann = None
221
222 obj_globals = None
223 module_name = getattr(obj, '__module__', None)
224 if module_name:

Callers 1

_signature_from_functionFunction · 0.85

Calls 4

varsFunction · 0.85
callableFunction · 0.50
getMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected