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)
| 174 | |
| 175 | |
| 176 | def _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( |
no test coverage detected