-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
bpo-46998: Allow subclassing Any at runtime #31841
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
Changes from 1 commit
bc71456
abea39a
771f492
900f2c7
6e76441
b00ab17
4d913ad
1c1ab38
4927e78
68ea3a8
f94b7e0
05db029
e11f405
63b00e4
ac7b4a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -428,8 +428,15 @@ def __getitem__(self, parameters): | |
| return self._getitem(self, *parameters) | ||
|
|
||
|
|
||
| @_SpecialForm | ||
| def Any(self, parameters): | ||
| class _AnyMeta(type): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| def __instancecheck__(self, obj): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we even have this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with removing it (as this PR currently does for My reasoning for keeping it is that |
||
| raise TypeError("Any cannot be used with isinstance()") | ||
|
|
||
| def __repr__(self): | ||
|
JelleZijlstra marked this conversation as resolved.
|
||
| return "typing.Any" | ||
|
|
||
|
|
||
| class Any(metaclass=_AnyMeta): | ||
| """Special type indicating an unconstrained type. | ||
|
|
||
| - Any is compatible with every type. | ||
|
|
@@ -438,9 +445,13 @@ def Any(self, parameters): | |
|
|
||
| Note that all the above statements are true from the point of view of | ||
| static type checkers. At runtime, Any should not be used with instance | ||
| or class checks. | ||
| checks. | ||
| """ | ||
| raise TypeError(f"{self} is not subscriptable") | ||
| def __new__(cls, *args, **kwargs): | ||
| if cls is Any: | ||
| raise TypeError("Any cannot be instantiated") | ||
| return object.__new__(cls, *args, **kwargs) | ||
|
hauntsaninja marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| @_SpecialForm | ||
| def NoReturn(self, parameters): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.