New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspect.signature bind doesn't include defaults or empty tuple/dicts #65883
Comments
I'm not sure if this is really a bug, but it is unexpected behavior. When you call "bind" on a Python 3.3 signature object, if you omit an optional argument, the default is not provided in the arguments dict. Similarly, if there is a "var positional" or "var keyword" parameter but there are no extra arguments, it will not be included. To emulate the effect on the namespace of an actual function, I would expect these to be included, as an empty tuple/dict in the case of variable arguments. I realize the current behavior may be useful in some cases, but if so, then another method could be added: bind_full, which would include all parameters of the signature. |
If this is decided against, a partial solution would be to set the "default" attribute of VAR_POSITIONAL and VAR_KEYWORD args to an empty tuple/dict, respectively. Then you could get a parameter's value no matter what with boundargs.get(param.name, param.default). |
That's the intended and documented behaviour, see https://docs.python.org/3/library/inspect.html#inspect.BoundArguments.arguments. You can easily implement the functionality you need by iterating through Signature.parameters and copying defaults to the BoundArguments.arguments mapping. There is no need to complicate the API with a dedicated method for that (if anything, in 3.5 you can subclass Signature and use from_callable to have any functionality you want). Closing this one as 'not a bug'. |
Copying defaults still doesn't give you var positional/keyword arguments, which means, you have to explicitly check the parameter type, and then add them in. I still think it would more useful to have an "official" way of getting all function parameters from arguments. |
Ryan, |
It's not really a particular use case. I was making a function decorator for automatic type checking using annotations (ironically I discovered later there is an almost identical example in the PEP for signatures). But I can't think of any use case when it would be undesirable to include the extra parameters, unless it slows down the code, hence the possibility of a separate method. It would not complicate the API to add behavior that would simplify most applications. And I just realized this is also the behavior of inspect.getcallargs, for which the docs recommend to switch to Signature.bind. |
One use case is that you are actually loosing information what arguments Signature.bind() was called with, when defaults are included. In some cases this information is important.
Since it's relatively easy to add mix defaults in, I'd prefer to be conservative here, and wait for a stronger community interest before adding a new method to API. But thanks for the bug report. If you find any other use cases for a separate method, please feel free to update this issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: