8000 bpo-46998: Allow subclassing Any at runtime by hauntsaninja · Pull Request #31841 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Prev Previous commit
Next Next commit
make error consistent
  • Loading branch information
hauntsaninja committed Mar 13, 2022
commit abea39a381934b0d50a3be8048edf6367a3ff512
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def __getitem__(self, parameters):

class _AnyMeta(type):
Copy link
Member

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.

Copy link
Contributor Author
@hauntsaninja hauntsaninja Mar 13, 2022

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

def __instancecheck__(self, obj):
Copy link
Member

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.

Copy link
Contributor Author
@hauntsaninja hauntsaninja Mar 13, 2022

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.

raise TypeError("Any cannot be used with isinstance()")
raise TypeError("typing.Any cannot be used with isinstance()")

def __repr__(self):
return "typing.Any"
Expand Down
0