| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -429,8 +429,17 @@ def __getitem__(self, parameters): | |
| return self._getitem(self, *parameters) | ||
|
|
||
|
|
||
| @_SpecialForm | ||
| def Any(self, parameters): | ||
| class _AnyMeta(type): | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThe metaclass is unfortunate because it restricts what classes can double-inherit from Any (due to metaclass conflicts). Seems unavoidable though.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNot entirely unavoidable, if we were willing to give up on instancecheck (and repr), I'd say we could just remove the metaclass entirely
Sorry, something went wrong.
All reactions
|
||
| def __instancecheck__(self, obj): | ||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityShould we even have this? isinstance(X, Any) is now a meaningful operation.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI'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.
Sorry, something went wrong.
All reactions
|
||
| if self is Any: | ||
| raise TypeError("typing.Any cannot be used with isinstance()") | ||
| return super().__instancecheck__(obj) | ||
|
|
||
| def __repr__(self): | ||
|
Comment thread
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. | ||
| Expand All | @@ -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): | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow subclassing of :class:`typing.Any`. Patch by Shantanu Jain. |
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis might be worth forbidding explicitly because the behavior could be quite unintuitive. Happy to leave that decision to the functools maintainer though.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality@ambv, do you have any thoughts on the bits of this PR that touch singledispatch?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.