MCPcopy Index your code
hub / github.com/bpython/bpython / _fix_default_values

Function _fix_default_values

bpython/inspection.py:176–215  ·  view source on GitHub ↗

Functions taking default arguments that are references to other objects will cause breakage, so we swap out the object itself with the name it was referenced with in the source by parsing the source itself!

(f: Callable, argspec: ArgSpec)

Source from the content-addressed store, hash-verified

174
175
176def _fix_default_values(f: Callable, argspec: ArgSpec) -> ArgSpec:
177 """Functions taking default arguments that are references to other objects
178 will cause breakage, so we swap out the object itself with the name it was
179 referenced with in the source by parsing the source itself!"""
180
181 if argspec.defaults is None and argspec.kwonly_defaults is None:
182 # No keyword args, no need to do anything
183 return argspec
184
185 try:
186 src, _ = inspect.getsourcelines(f)
187 except (OSError, IndexError):
188 # IndexError is raised in inspect.findsource(), can happen in
189 # some situations. See issue #94.
190 return argspec
191 except TypeError:
192 # No source code is available, so replace the default values with what we have.
193 if argspec.defaults is not None:
194 argspec.defaults = [_Repr(str(value)) for value in argspec.defaults]
195 if argspec.kwonly_defaults is not None:
196 argspec.kwonly_defaults = {
197 key: _Repr(str(value))
198 for key, value in argspec.kwonly_defaults.items()
199 }
200 return argspec
201
202 kwparsed = parsekeywordpairs("".join(src))
203
204 if argspec.defaults is not None:
205 values = list(argspec.defaults)
206 keys = argspec.args[-len(values) :]
207 for i, key in enumerate(keys):
208 values[i] = _Repr(kwparsed[key])
209
210 argspec.defaults = values
211 if argspec.kwonly_defaults is not None:
212 for key in argspec.kwonly_defaults.keys():
213 argspec.kwonly_defaults[key] = _Repr(kwparsed[key])
214
215 return argspec
216
217
218_getpydocspec_re = LazyReCompile(

Callers 1

getfuncpropsFunction · 0.85

Calls 2

_ReprClass · 0.85
parsekeywordpairsFunction · 0.85

Tested by

no test coverage detected