-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PEP 590: allow passing a dict to PyObject_Vectorcall #1038
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
Conversation
The argument against is that we want to position |
That's just a matter of documentation: we could say that a tuple is recommended.
There is an obvious advantage over An important note is also that it's not just a matter of performance but also of convenience: by allowing various kinds of calls, we make it as easy as possible for the users of the API. There are various existing use cases for using a vector of args but a dict of kwargs (that's the reason why |
Also, this would not be hard to add afterwards, if we find compelling use cases. |
I agree that optionally allowing a dict would be nice. Some callers might simply already have a dict lying around (if only for legacy reasons to support older Python versions), in which case forcing them to either create a pos-args tuple and use |
I don't get this argument. In any case, it seems more like a documentation issue: we could document that a tuple is recommended.
We don't need to find use cases, there are already use cases in the CPython source code. To name two random use cases, it's used for implementing |
That convinces me that |
I think this is a bad idea, as it suggests to the user that passing a dictionary to The first reason is that The second reason is that it is not clear what
To answer @scoder's comment about caller having a dictionary "lying around". If they do so for legacy call reasons, they should also have tuple "lying around" and can just use the Would an API function to help convert from the vector/dict form proposed by this PR to the |
As I already said, that's just a documentation issue... |
Also read what I posted earlier: it's not only a matter of performance, but also of convenience. With this in mind, it shouldn't be a problem to accept a |
But that's an argument in favor of this PR. When you don't allow passing a dict in |
If the caller knows the callee, it should use the right protocol. So, let's assume it doesn't know the callee (and doesn't want to introspect it). Now,
|
I should add that I've been using this |
I'm unconvinced that this "have a vector + dict lying around" is a real scenario, and it is definitely not a common one. The purpose of PEP 590 is to add one new calling convention. Allowing PEP 590 is not about convenience or ease of use. It is about performance. Vagueness in the API is likely to introduce confusion and inefficiency. I think that accepting two fundamentally different kinds of things for a single function is poor API design. The change in the second argument implicitly changes the type of first argument. If the second argument is a tuple, then the first argument is a vector of all the argument values. However if the second argument were to be a dict, then the first argument becomes a vector of just the positional arguments. Which seems needlessly confusing and error prone. If the ability to pass vector+dict is that valuable, which I doubt, then use |
I'm fine creating a second function for the vector + dict calling convention, if that's the compromise. But I would put those two functions on the same level (i.e. make both public, calling them |
Could you please explain
|
Counter-proposal at #1057 with the same functionality as this PR but split over two different API functions. |
Allow passing a dict as
keywords
argument toPyObject_Vectorcall
. Since checking for atuple
ordict
is very fast, this costs almost nothing in terms of performance.The fact that
_PyObject_FastCallDict
(which does vectorcall with a dict) exists and is used in various places in CPython shows that such functionality is useful.CC @encukou @markshannon