Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b67596d

Browse files
committed
Issue #16049: add abc.ABC helper class.
Patch by Bruno Dupuis.
1 parent 174bc1e commit b67596d

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

Doc/library/abc.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
--------------
1313

1414
This module provides the infrastructure for defining :term:`abstract base
15-
classes <abstract base class>` (ABCs) in Python, as outlined in :pep:`3119`; see the PEP for why this
16-
was added to Python. (See also :pep:`3141` and the :mod:`numbers` module
17-
regarding a type hierarchy for numbers based on ABCs.)
15+
classes <abstract base class>` (ABCs) in Python, as outlined in :pep:`3119`;
16+
see the PEP for why this was added to Python. (See also :pep:`3141` and the
17+
:mod:`numbers` module regarding a type hierarchy for numbers based on ABCs.)
1818

1919
The :mod:`collections` module has some concrete classes that derive from
2020
ABCs; these can, of course, be further derived. In addition the
@@ -23,7 +23,7 @@ a class or instance provides a particular interface, for example, is it
2323
hashable or a mapping.
2424

2525

26-
This module provides the following class:
26+
This module provides the following classes:
2727

2828
.. class:: ABCMeta
2929

@@ -127,6 +127,16 @@ This module provides the following class:
127127
available as a method of ``Foo``, so it is provided separately.
128128

129129

130+
.. class:: ABC
131+
132+
A helper class that has :class:`ABCMeta` as metaclass. :class:`ABC` is the
133+
standard class to inherit from in order to create an abstract base class,
134+
avoiding sometimes confusing metaclass usage.
135+
136+
Note that :class:`ABC` type is still :class:`ABCMeta`, therefore inheriting
137+
from :class:`ABC` requires usual precautions regarding metaclasses usage
138+
as multiple inheritance may lead to metaclass conflicts.
139+
130140
The :mod:`abc` module also provides the following decorators:
131141

132142
.. decorator:: abstractmethod

Lib/abc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,9 @@ def __subclasscheck__(cls, subclass):
226226
# No dice; update negative cache
227227
cls._abc_negative_cache.add(subclass)
228228
return False
229+
230+
class ABC(metaclass=ABCMeta):
231+
"""Helper class that provides a standard way to create an ABC using
232+
inheritance.
233+
"""
234+
pass

Lib/test/test_abc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ def bar(self): pass # abstract override of concrete
9696

9797
class TestABC(unittest.TestCase):
9898

99+
def test_ABC_helper(self):
100+
# create an ABC using the helper class and perform basic checks
101+
class C(abc.ABC):
102+
@classmethod
103+
@abc.abstractmethod
104+
def foo(cls): return cls.__name__
105+
self.assertEqual(type(C), abc.ABCMeta)
106+
self.assertRaises(TypeError, C)
107+
class D(C):
108+
@classmethod
109+
def foo(cls): return super().foo()
110+
self.assertEqual(D.foo(), 'D')
111+
99112
def test_abstractmethod_basics(self):
100113
@abc.abstractmethod
101114
def foo(self): pass

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Core and Builtins
163163
Library
164164
-------
165165

166+
- Add abc.ABC class to use inheritance rather than a direct invocation of
167+
ABCMeta metaclass. Patch by Bruno Dupuis.
168+
166169
- Expose the TCP_FASTOPEN and MSG_FASTOPEN flags in socket when they're
167170
available.
168171

0 commit comments

Comments
 (0)