MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / generate_function

Function generate_function

tools/boilerplate.py:119–199  ·  view source on GitHub ↗

Create a wrapper function *pyplot_name* calling *call_name*. Parameters ---------- name : str The function to be created. called_fullname : str The method to be wrapped in the format ``"Class.method"``. template : str The template to be used. The tem

(name, called_fullname, template, **kwargs)

Source from the content-addressed store, hash-verified

117
118
119def generate_function(name, called_fullname, template, **kwargs):
120 """
121 Create a wrapper function *pyplot_name* calling *call_name*.
122
123 Parameters
124 ----------
125 name : str
126 The function to be created.
127 called_fullname : str
128 The method to be wrapped in the format ``"Class.method"``.
129 template : str
130 The template to be used. The template must contain {}-style format
131 placeholders. The following placeholders are filled in:
132
133 - name: The function name.
134 - signature: The function signature (including parentheses).
135 - called_name: The name of the called function.
136 - call: Parameters passed to *called_name* (including parentheses).
137
138 **kwargs
139 Additional parameters are passed to ``template.format()``.
140 """
141 # Get signature of wrapped function.
142 class_name, called_name = called_fullname.split('.')
143 class_ = {'Axes': Axes, 'Figure': Figure}[class_name]
144
145 meth = getattr(class_, called_name)
146 decorator = _api.deprecation.DECORATORS.get(meth)
147 # Generate the wrapper with the non-kwonly signature, as it will get
148 # redecorated with make_keyword_only by _copy_docstring_and_deprecators.
149 if decorator and decorator.func is _api.make_keyword_only:
150 meth = meth.__wrapped__
151
152 annotated_trees = get_ast_mro_trees(class_)
153 signature = get_matching_signature(meth, annotated_trees)
154
155 # Replace self argument.
156 params = list(signature.parameters.values())[1:]
157 has_return_value = str(signature.return_annotation) != 'None'
158 signature = str(signature.replace(parameters=[
159 param.replace(default=value_formatter(param.default))
160 if param.default is not param.empty else param
161 for param in params]))
162 # How to call the wrapped function.
163 call = '(' + ', '.join((
164 # Pass "intended-as-positional" parameters positionally to avoid
165 # forcing third-party subclasses to reproduce the parameter names.
166 '{0}'
167 if param.kind in [
168 Parameter.POSITIONAL_OR_KEYWORD]
169 and param.default is Parameter.empty else
170 # Only pass the data kwarg if it is actually set, to avoid forcing
171 # third-party subclasses to support it.
172 '**({{"data": data}} if data is not None else {{}})'
173 if param.name == "data" else
174 '{0}={0}'
175 if param.kind in [
176 Parameter.POSITIONAL_OR_KEYWORD,

Callers 1

boilerplate_genFunction · 0.85

Calls 6

get_ast_mro_treesFunction · 0.85
get_matching_signatureFunction · 0.85
value_formatterClass · 0.85
valuesMethod · 0.80
joinMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…