-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Make abstract/dunder methods positional-only. #135312
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
I did a scan of the
|
This is probably fine for the dunder methods in 3.15; it technically breaks compatibility but any affected code (if any exists) is extremely questionable. The non-dunder methods should probably also use positional-only args if we add them today, but making them positional-only now seems a bit riskier. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
It would be nice if certain abstract methods / dunder methods would be defined with positional-only arguments, so that when one subclasses them an appropriate name can be chosen, or they can be defined positional-only in the subclass.1
Code sample in pyright playground1
In some cases, the stubs already fake that the argument is positional-only, such as for
Container.__contains__
:def __contains__(self, x: object, /) -> bool: ...
intypeshed/stdlib/typing.pyi
def __contains__(self, x):
incpython/Lib/_collections_abc.py
Personally, I think only changing the stubs would be a pragmatic choice, but I was told by the maintainers that they believe this change should be done in Cpython first.
Backward Compatibility
Changing non-abstract methods can potentially break user code. For example, this is currently legal code at runtime
Changing the signature of abstract methods can introduce type-checker errors Code sample in pyright playground1
With this in mind, I believe changing abstrac dunder methods to use positional only arguments should be relatively safe, as calling them directly with keyword is very uncommon, and at worst it will introduce type errors.
Personally, I'd also like so see methods like
Container.__subclasshook__
use positional-only arguments, but I also understand if this is not feasible due to bc-conerns.Has this already been discussed elsewhere?
python/typeshed#14071
Footnotes
Note that
mypy
special cases certain dunder methods, and yields different results thanpyright
on these examples ↩ ↩2 ↩3The text was updated successfully, but these errors were encountered: