-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Constructors of some mapping classes don't accept self
keyword argument
#66799
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
Comments
>>> import collections
>>> collections.Counter(self=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got multiple values for argument 'self'
>>> collections.OrderedDict(self="test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got multiple values for argument 'self'
>>> collections.UserDict(self="test")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() got multiple values for argument 'self' Python surely should have positional-only parameters. |
Here is a patch. |
I'll spend some time taking this one under consideration. Keyword arguments for dicts and dict-like classes are already somewhat limited (only non-keyword identifiers) that why Guido resisted adding them in the first place. And, in the context of OrderedDicts, they aren't really useful at all (because the order gets scrambled). I'm reluctant to make the number of changes required in order to support this corner case. The ship for Python 2.7 sailed a long time ago and it is risky to make changes like this. After the patch, the code is less readable, harder to get right, harder to maintain, and not as performant. On the other hand, adding "self" would be a nice-to-have, so it is worth thinking about. |
After some thought, I think we have to fix this. I'll go through the patch in careful detail this weekend. Ethan, if you would like to help, it would be great to have a third pair of eyes looking at the patch to make sure it correct it every detail. |
I thought that more verbose but straightforward code would be more acceptable. |
I will take a look. |
Code looks good. Only downside is the change in help and inspect.signature output, but that is minor: Help on dict object: vs. Help on class Counter in module collections: +1 to accept. |
FWIW, I agree that it should be fixed: >>> dict(self=1)
{'self': 1} |
This looks good. Go ahead and apply the first version of the patch. |
New changeset 816c15fe5812 by Serhiy Storchaka in branch '3.4': New changeset 88ab046fdd8a by Serhiy Storchaka in branch 'default': |
New changeset cd1ead4feddf by Serhiy Storchaka in branch '3.4': New changeset 167d51a54de2 by Serhiy Storchaka in branch 'default': |
Unfortunately there is existing test for current behavior of UserDict:
This looks wrong to me and I think we should break compatibility here. |
New changeset 3dfe4f0c626b by Serhiy Storchaka in branch '2.7': |
So what to do wish UserDict? Should we break backward compatibility and add support for "self" and "dict" keywords as in other dict-like types? |
Fix |
[Serhiy]
I'm leaning in favor of leaving UserDict as-is. AFAICT, in the very long history of UserDict, this has never been a problem. So, I don't think there is an issue worth breaking the published API and possibly breaking code that currently works. (This is a case of practicality beating purity). |
So may be close this issue? See also bpo-22958. |
Here is a patch for UserDict, that keep current behavior, but allows to pass keys "self" and "dict" if positional parameter dict is specified. >>> UserDict(self=42)
{'self': 42}
>>> UserDict({}, dict=42)
{'dict': 42}
>>> UserDict(dict={'a': 42})
{'a': 42} |
UserDict_self_and_dict_keywords.patch looks good to me. Is there a reason not to use assertWarnsRegex? Also, there are already collections.UserDict() usages in the test file, so I'd remove the "from collections import UserDict" import. |
Thank you for your review Berker.
Initially the patch was written for 2.7. Fixing WeakValueDictionary in 2.7
These tests originally was written for test_collections. Updated patch addresses Berker's comment. |
Could you please look at the new patch Raymond? This is the dependency for bpo-22958. |
Ping. |
Left a couple pedantic comments. |
Updated patch addresses Martin's comments. |
Left some feedback in the code review. |
Patch looks fine to me. I understand Yury withdrew his comment. |
Ping again. |
Who are you pinging? I did just notice a minor English grammar problem (“one arguments”). But as far as I am concered you could have already committed the patch. |
I will look at this more when I get a chance (likely this week). |
I was pinging Raymond. He is maintainer of the collections module, this issue is assigned to his, and he had valid objections to previous version of the patch. Even one of this reason is enough to wait his review before committing. Thank you Raymond. |
Ping. |
UserDict_self_and_dict_keywords_3.patch looks good to me. |
New changeset 4c5407e1b0ec by Serhiy Storchaka in branch '2.7': New changeset 1869f5625392 by Serhiy Storchaka in branch '3.4': New changeset ab7e3f1f9f88 by Serhiy Storchaka in branch '3.5': New changeset 901964295066 by Serhiy Storchaka in branch 'default': |
Thank you all for your reviews. |
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: