Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
bc71456abea39a771f492900f2c76e76441b00ab174d913ad1c1ab384927e7868ea3a8f94b7e005db029e11f40563b00e4ac7b4a8File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -429,8 +429,17 @@ 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. ContributorAuthor
| ||
| 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? ContributorAuthor
| ||
| if self is Any: | ||
| raise TypeError("typing.Any cannot be used with isinstance()") | ||
| return super().__instancecheck__(obj) | ||
| def __repr__(self): | ||
JelleZijlstra marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| return "typing.Any" | ||
| class Any(metaclass=_AnyMeta): | ||
| """Special type indicating an unconstrained type. | ||
| - Any is compatible with every type. | ||
| @@ -439,9 +448,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 super().__new__(cls, *args, **kwargs) | ||
| @_SpecialForm | ||
| def NoReturn(self, parameters): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow subclassing of :class:`typing.Any`. Patch by Shantanu Jain. |
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?