diff -r 0238cc842805 -r 527b55110fa4 Doc/library/abc.rst --- a/Doc/library/abc.rst Thu Dec 06 17:49:58 2012 -0500 +++ b/Doc/library/abc.rst Fri Dec 07 13:30:17 2012 +0100 @@ -12,9 +12,9 @@ -------------- This module provides the infrastructure for defining :term:`abstract base -classes ` (ABCs) in Python, as outlined in :pep:`3119`; see the PEP for why this -was added to Python. (See also :pep:`3141` and the :mod:`numbers` module -regarding a type hierarchy for numbers based on ABCs.) +classes ` (ABCs) in Python, as outlined in :pep:`3119`; +see the PEP for why this was added to Python. (See also :pep:`3141` and the +:mod:`numbers` module regarding a type hierarchy for numbers based on ABCs.) The :mod:`collections` module has some concrete classes that derive from ABCs; these can, of course, be further derived. In addition the @@ -23,7 +23,7 @@ hashable or a mapping. -This module provides the following class: +This module provides the following classes: .. class:: ABCMeta @@ -127,6 +127,16 @@ available as a method of ``Foo``, so it is provided separately. +.. class:: ABC + + A helper class that has :class:`ABCMeta` as metaclass. :class:`ABC` is the + standard class to inherit from in order to create an abstract base class, + avoiding sometimes confusing metaclass usage. + + Note that :class:`ABC` type is still :class:`ABCMeta`, therefore inheriting + from :class:`ABC` requires usual precautions regarding metaclasses usage + as multiple inheritance may lead to metaclass conflicts. + The :mod:`abc` module also provides the following decorators: .. decorator:: abstractmethod(function) diff -r 0238cc842805 -r 527b55110fa4 Lib/abc.py --- a/Lib/abc.py Thu Dec 06 17:49:58 2012 -0500 +++ b/Lib/abc.py Fri Dec 07 13:30:17 2012 +0100 @@ -226,3 +226,10 @@ # No dice; update negative cache cls._abc_negative_cache.add(subclass) return False + +class ABC(metaclass=ABCMeta): + """Helper class that provides a standard way to create an ABC using + inheritance. + """ + pass + diff -r 0238cc842805 -r 527b55110fa4 Lib/test/test_abc.py --- a/Lib/test/test_abc.py Thu Dec 06 17:49:58 2012 -0500 +++ b/Lib/test/test_abc.py Fri Dec 07 13:30:17 2012 +0100 @@ -96,6 +96,19 @@ class TestABC(unittest.TestCase): + def test_ABC_helper(self): + # create an ABC using the helper class and perform basic checks + class C(abc.ABC): + @classmethod + @abc.abstractmethod + def foo(cls): return cls.__name__ + self.assertEqual(type(C), abc.ABCMeta) + self.assertRaises(TypeError, C) + class D(C): + @classmethod + def foo(cls): return super().foo() + self.assertEqual(D.foo(), 'D') + def test_abstractmethod_basics(self): @abc.abstractmethod def foo(self): pass