-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
tuple subclasses allow arbitrary kwargs #87579
Comments
While troubleshooting a strange problem (jaraco/keyring#498) where a program worked on Python 3.7+ but failed on Python 3.6, I discovered a somewhat unintuitive behavior. On Python 3.7+, keyword arguments to tuple subclasses are allowed and ignored: >>> class Foo(tuple): pass
>>> tuple(name='xyz')
TypeError: tuple() takes no keyword arguments
>>> Foo(name='xyz')
() But on Python 3.6, the keyword parameter causes an error: $ python3.6 -c "type('Foo', (tuple,), {})(name='xyz')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'name' is an invalid keyword argument for this function I checked out the What's new in Python 3.7 and I see this notice: Functions bool(), float(), list() and tuple() no longer take keyword arguments. The first argument of int() can now be passed only as positional argument. Hmm, but in my experience, tuple on Python 3.6 doesn't take keyword arguments either: importlib_metadata main $ python3.6 -c "tuple(name='xyz')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'name' is an invalid keyword argument for this function So that change may be related, but I'm not sure where or how. The main place my expectation is violated is in the subclass. Why should a subclass of tuple allow keyword arguments when the parent class does not? I'd expect that the subclass should reject keyword arguments as well. Less importantly, the What's New doc implies that keyword arguments were accepted in Python 3.6; why aren't they? |
To be abundantly clear, the downstream issue was a coding error, but the coding error was masked on Python 3.7+ when the subclass didn't reject the invalid usage. |
I see that changelog entry traces back to bpo-29695, but I don't believe it's relevant to this issue. |
I suspect bpo-20186 is implicated. |
In particular, this commit. |
They do. >>> tuple(sequence='abc')
('a', 'b', 'c')
>>> list(sequence='abc')
['a', 'b', 'c']
>>> int(x='123')
123
>>> bool(x='123')
True
>>> float(x='123')
123.0 It was changed in bpo-29695. But accepting arbitrary keyword arguments is another issue. Object creation in Python can be customized be implementing special methods __new__ and __init__. They both are called sequentially with arguments passed to the constructor. If object is mutable, it is enough to implement __init__. If the object contains immutable data which should be initialized at creation time, it needs __new__, and __init__ is not necessary. So the list class has __init__, but the tuple and int classes have __new__. Usually class implements only one of __new__ or __init__, and inherits the other one from parent classes. Since positional and keyword arguments are passed to both __new__ and __init__, they should accept same arguments. If __new__ and __init__ are inherited from parent class, they cannot know about arguments supported in the other method. Therefore object's __new__ and __init__ accept and ignore all arguments (if the other method is overridden). Implementations of __new__ for most builtin classes which accept only positional arguments accept and ignore also arbitrary keyword arguments in subclasses. It makes easier to implement a subclass with non-trivial __init__. You just add additional keyword arguments which will be ignored in __new__. It is long tradition. Example:
bpo-20186 just used this idiom for tuple and several other classes. It is a feature which makes subclassing these classes easier. And with converting more classes to Argument Clinic it is now used in more and more classes. Now, perhaps it would be more correct to test |
Surprisingly there were almost no tests for keyword arguments in subclasses. PR 26456 makes checks for some classes (like tuple, list, frozenset) more strict: if subclass does not define __init__ or __new__, it will reject arbitrary keyword arguments. It also makes the check in set.__init__() more lenient for uniformity with frozenset and list. Subclass of set can now define a __new__() method with additional keyword parameters without overriding also __init__(). Added tests for some of builtin classes. Raymond, please take a look. It touches classes maintained tracked by you: set/frozenset, itertools, random. |
Is there any use case for this? Offhand, I can't think of any reason. The new code in set.__init__ is somewhat opaque and is likely slower, so I prefer the previous code unless there is a legitimate use case being served. |
I do not have any particular use case. It was a side effect of unification code that uses _PyArg_NoKeywords(). |
Is your concern about a use-case for the general concept or for set specifically? I appreciate that Serhiy has taken the time to evaluate the specific concern I raised and extrapolate it to the implications for most/all built-in types. It seems worthwhile to me that built-in types have consistent behaviors. Moreover, it seems preferable to provide the intuitive behavior (allowing simply overriding After exploring this some, I'm convinced there may not be a strong case for this behavior. Raymond, if this new behavior was removed, how would you propose to rewrite the test (specifically Lines 665 to 673 in 778b075
|
Oh, and I see now Serhiy has proposed a change that looks reasonable. |
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: