Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b67596d8
Commit
b67596d8
authored
Dec 13, 2012
by
Andrew Svetlov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #16049: add abc.ABC helper class.
Patch by Bruno Dupuis.
parent
174bc1e3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
4 deletions
+36
-4
Doc/library/abc.rst
Doc/library/abc.rst
+14
-4
Lib/abc.py
Lib/abc.py
+6
-0
Lib/test/test_abc.py
Lib/test/test_abc.py
+13
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/abc.rst
View file @
b67596d8
...
...
@@ -12,9 +12,9 @@
--------------
This module provides the infrastructure for defining :term:`abstract base
classes <abstract base class>` (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` modul
e
regarding a type hierarchy for numbers based on ABCs.)
classes <abstract base class>` (ABCs) in Python, as outlined in :pep:`3119`;
see the PEP for why this was added to Python. (See also :pep:`3141` and th
e
: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 @@ a class or instance provides a particular interface, for example, is it
hashable or a mapping.
This module provides the following class:
This module provides the following class
es
:
.. class:: ABCMeta
...
...
@@ -127,6 +127,16 @@ This module provides the following class:
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
...
...
Lib/abc.py
View file @
b67596d8
...
...
@@ -226,3 +226,9 @@ class ABCMeta(type):
# 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
Lib/test/test_abc.py
View file @
b67596d8
...
...
@@ -96,6 +96,19 @@ class TestLegacyAPI(unittest.TestCase):
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
...
...
Misc/NEWS
View file @
b67596d8
...
...
@@ -163,6 +163,9 @@ Core and Builtins
Library
-------
- Add abc.ABC class to use inheritance rather than a direct invocation of
ABCMeta metaclass. Patch by Bruno Dupuis.
- Expose the TCP_FASTOPEN and MSG_FASTOPEN flags in socket when they'
re
available
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment