-
-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
bpo-46998: Allow subclassing Any at runtime #31841
Conversation
This PR also permits The error in |
Misc/NEWS.d/next/Library/2022-03-13-08-52-58.bpo-46998.cHh-9O.rst
Outdated
Show resolved
Hide resolved
@@ -2802,8 +2802,6 @@ def f(arg): | |||
f.register(list[int] | str, lambda arg: "types.UnionTypes(types.GenericAlias)") | |||
with self.assertRaisesRegex(TypeError, "Invalid first argument to "): | |||
f.register(typing.List[float] | bytes, lambda arg: "typing.Union[typing.GenericAlias]") | |||
with self.assertRaisesRegex(TypeError, "Invalid first argument to "): | |||
f.register(typing.Any, lambda arg: "typing.Any") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be worth forbidding explicitly because the behavior could be quite unintuitive. Happy to leave that decision to the functools maintainer though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ambv, do you have any thoughts on the bits of this PR that touch singledispatch
?
@_SpecialForm | ||
def Any(self, parameters): | ||
class _AnyMeta(type): | ||
def __instancecheck__(self, obj): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we even have this? isinstance(X, Any)
is now a meaningful operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with removing it (as this PR currently does for issubclass
). Doing so would also allow us to get rid of the metaclass, which will help remove restrictions on what classes can inherit from Any
.
My reasoning for keeping it is that isinstance
is very commonly used, potentially by typing / Python novices, and isinstance(..., Any)
doesn't correspond well to the notion of Any
at type check time. Sophisticated users have workarounds available to them for the equivalent isinstance check.
@@ -428,8 +428,15 @@ def __getitem__(self, parameters): | |||
return self._getitem(self, *parameters) | |||
|
|||
|
|||
@_SpecialForm | |||
def Any(self, parameters): | |||
class _AnyMeta(type): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metaclass is unfortunate because it restricts what classes can double-inherit from Any (due to metaclass conflicts). Seems unavoidable though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely unavoidable, if we were willing to give up on instancecheck (and repr), I'd say we could just remove the metaclass entirely
Planning to merge in a few days unless there's more discussion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at it again, I realize we should document this. Probably fine to just add a note like .. versionchanged: 3.11: Any can now be used as a base class
.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be poked with soft cushions! |
Thanks for making the requested changes! @JelleZijlstra: please review the changes made to this pull request. |
Doc/library/typing.rst
Outdated
@@ -578,6 +578,9 @@ These can be used as types in annotations and do not support ``[]``. | |||
* Every type is compatible with :data:`Any`. | |||
* :data:`Any` is compatible with every type. | |||
|
|||
.. versionchanged:: 3.11 | |||
:data:`Any` can now be used as a base class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some explanation about the use cases?
This doesn’t say why someone could want that, nor does the ticket, one has to hunt for the last message in the mailing list thread!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a sentence here. Let me know if I should make it longer; I tried to keep it short because this is a lot more niche than other things covered in typing.rst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding one short example might be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this. On the one hand an example would be useful; on the other hand it risks putting too much emphasis on a pretty obscure use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's omit the example then (devguide says "err on the side of being succinct" for docs). I'm hoping to do some work on typing docs sometime soon, which might be a better home for such details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alrighty!
https://build.opensuse.org/request/show/1031001 by user mcepl + dimstar_suse - Clean specfile from old cruft. - Requires Python 3.7+ - Fix testsuite: Must test as module; don't need multibuild. - Update Summary and Description - Update to version 4.4.0 * Add `typing_extensions.Any` a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike `default` parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding `typing_extensions.override`. Patch by Jelle Zijlstra. * Add the `infer_variance` parameter to `TypeVar`, as specified in PEP 695. Patch by Jelle Zijlstra.
https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. (From OE-Core rev: 0b8f212744f8de7e6b33ab02d042009eba462241) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. (From OE-Core rev: 0b8f212744f8de7e6b33ab02d042009eba462241) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. (From OE-Core rev: 34c8f1eac66ae770d64eaa854b1f263848210773) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 (From OE-Core rev: 6dc6c11b638c1d45f4dc9c7813f93a09229c33b3) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 (From OE-Core rev: 0cb15457edaa6cee3ac152a18d8b6ba4204dd958) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 (From OE-Core rev: 0cb15457edaa6cee3ac152a18d8b6ba4204dd958) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 (From OE-Core rev: 15ca091ae01ae298c013e8cf82ee6af382259ed8) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]> Signed-off-by: Richard Purdie <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]>
…thon-3.10.8 - Updated from version 4.1.1 to 4.4.0 - Update of rootfile - Changelog # Release 4.4.0 (October 6, 2022) - Add `typing_extensions.Any` a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). - Add initial support for TypeVarLike `default` parameter, PEP 696. Patch by Marc Mueller (@cdce8p). - Runtime support for PEP 698, adding `typing_extensions.override`. Patch by Jelle Zijlstra. - Add the `infer_variance` parameter to `TypeVar`, as specified in PEP 695. Patch by Jelle Zijlstra. # Release 4.3.0 (July 1, 2022) - Add `typing_extensions.NamedTuple`, allowing for generic `NamedTuple`s on Python <3.11 (backport from python/cpython#92027, by Serhiy Storchaka). Patch by Alex Waygood (@AlexWaygood). - Adjust `typing_extensions.TypedDict` to allow for generic `TypedDict`s on Python <3.11 (backport from python/cpython#27663, by Samodya Abey). Patch by Alex Waygood (@AlexWaygood). # Release 4.2.0 (April 17, 2022) - Re-export `typing.Unpack` and `typing.TypeVarTuple` on Python 3.11. - Add `ParamSpecArgs` and `ParamSpecKwargs` to `__all__`. - Improve "accepts only single type" error messages. - Improve the distributed package. Patch by Marc Mueller (@cdce8p). - Update `typing_extensions.dataclass_transform` to rename the `field_descriptors` parameter to `field_specifiers` and accept arbitrary keyword arguments. - Add `typing_extensions.get_overloads` and `typing_extensions.clear_overloads`, and add registry support to `typing_extensions.overload`. Backport from python/cpython#89263. - Add `typing_extensions.assert_type`. Backport from bpo-46480. - Drop support for Python 3.6. Original patch by Adam Turner (@AA-Turner). Tested-by: Adolf Belka <[email protected]> Signed-off-by: Adolf Belka <[email protected]>
* Add SUMMARY, DESCRIPTION, BUGTRACKER and SECTION * Update HOMEPAGE to agree with PyPi https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-440-october-6-2022 Release 4.4.0 (October 6, 2022) * Add typing_extensions.Any a backport of python 3.11's Any class which is subclassable at runtime. (backport from python/cpython#31841, by Shantanu and Jelle Zijlstra). Patch by James Hilton-Balfe (@Gobot1234). * Add initial support for TypeVarLike default parameter, PEP 696. Patch by Marc Mueller (@cdce8p). * Runtime support for PEP 698, adding typing_extensions.override. Patch by Jelle Zijlstra. * Add the infer_variance parameter to TypeVar, as specified in PEP 695. Patch by Jelle Zijlstra. License-Update: update copyright years; align with CPython LICENSE See python/typing_extensions#63 (From OE-Core rev: 15ca091ae01ae298c013e8cf82ee6af382259ed8) Signed-off-by: Tim Orling <[email protected]> Signed-off-by: Alexandre Belloni <[email protected]> Signed-off-by: Richard Purdie <[email protected]>
https://bugs.python.org/issue46998