mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
This commit is contained in:
parent
5390d00cc6
commit
6c3787cb70
5 changed files with 36 additions and 3 deletions
|
@ -133,11 +133,14 @@ class ABCMeta(type):
|
|||
return cls
|
||||
|
||||
def register(cls, subclass):
|
||||
"""Register a virtual subclass of an ABC."""
|
||||
"""Register a virtual subclass of an ABC.
|
||||
|
||||
Returns the subclass, to allow usage as a class decorator.
|
||||
"""
|
||||
if not isinstance(subclass, type):
|
||||
raise TypeError("Can only register classes")
|
||||
if issubclass(subclass, cls):
|
||||
return # Already a subclass
|
||||
return subclass # Already a subclass
|
||||
# Subtle: test for cycles *after* testing for "already a subclass";
|
||||
# this means we allow X.register(X) and interpret it as a no-op.
|
||||
if issubclass(cls, subclass):
|
||||
|
@ -145,6 +148,7 @@ class ABCMeta(type):
|
|||
raise RuntimeError("Refusing to create an inheritance cycle")
|
||||
cls._abc_registry.add(subclass)
|
||||
ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
|
||||
return subclass
|
||||
|
||||
def _dump_registry(cls, file=None):
|
||||
"""Debug helper to print the ABC registry."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue