[Python-checkins] r84124 - in python/branches/py3k: Doc/library/abc.rst Lib/abc.py Lib/test/test_abc.py Misc/NEWS
benjamin.peterson
python-checkins at python.org
Tue Aug 17 02:52:53 CEST 2010
Author: benjamin.peterson
Date: Tue Aug 17 02:52:52 2010
New Revision: 84124
Log:
add support for abstract class and static methods #5867
Modified:
python/branches/py3k/Doc/library/abc.rst
python/branches/py3k/Lib/abc.py
python/branches/py3k/Lib/test/test_abc.py
python/branches/py3k/Misc/NEWS
Modified: python/branches/py3k/Doc/library/abc.rst
==============================================================================
--- python/branches/py3k/Doc/library/abc.rst (original)
+++ python/branches/py3k/Doc/library/abc.rst Tue Aug 17 02:52:52 2010
@@ -157,6 +157,32 @@
multiple-inheritance.
+.. decorator:: abstractclassmethod(function)
+
+ A subclass of the built-in :func:`classmethod`, indicating an abstract
+ classmethod. Otherwise it is similar to :func:`abstractmethod`.
+
+ Usage::
+
+ class C(metaclass=ABCMeta):
+ @abstractclassmethod
+ def my_abstract_classmethod(cls, ...):
+ ...
+
+
+.. decorator:: abstractstaticmethod(function)
+
+ A subclass of the built-in :func:`staticmethod`, indicating an abstract
+ staticmethod. Otherwise it is similar to :func:`abstractmethod`.
+
+ Usage::
+
+ class C(metaclass=ABCMeta):
+ @abstractstaticmethod
+ def my_abstract_staticmethod(...):
+ ...
+
+
.. function:: abstractproperty(fget=None, fset=None, fdel=None, doc=None)
A subclass of the built-in :func:`property`, indicating an abstract property.
Modified: python/branches/py3k/Lib/abc.py
==============================================================================
--- python/branches/py3k/Lib/abc.py (original)
+++ python/branches/py3k/Lib/abc.py Tue Aug 17 02:52:52 2010
@@ -25,6 +25,46 @@
return funcobj
+class abstractclassmethod(classmethod):
+ """A decorator indicating abstract classmethods.
+
+ Similar to abstractmethod.
+
+ Usage:
+
+ class C(metaclass=ABCMeta):
+ @abstractclassmethod
+ def my_abstract_classmethod(cls, ...):
+ ...
+ """
+
+ __isabstractmethod__ = True
+
+ def __init__(self, callable):
+ callable.__isabstractmethod__ = True
+ super().__init__(callable)
+
+
+class abstractstaticmethod(staticmethod):
+ """A decorator indicating abstract staticmethods.
+
+ Similar to abstractmethod.
+
+ Usage:
+
+ class C(metaclass=ABCMeta):
+ @abstractstaticmethod
+ def my_abstract_staticmethod(...):
+ ...
+ """
+
+ __isabstractmethod__ = True
+
+ def __init__(self, callable):
+ callable.__isabstractmethod__ = True
+ super().__init__(callable)
+
+
class abstractproperty(property):
"""A decorator indicating abstract properties.
Modified: python/branches/py3k/Lib/test/test_abc.py
==============================================================================
--- python/branches/py3k/Lib/test/test_abc.py (original)
+++ python/branches/py3k/Lib/test/test_abc.py Tue Aug 17 02:52:52 2010
@@ -34,8 +34,46 @@
def foo(self): return super().foo
self.assertEqual(D().foo, 3)
+ def test_abstractclassmethod_basics(self):
+ @abc.abstractclassmethod
+ def foo(cls): pass
+ self.assertEqual(foo.__isabstractmethod__, True)
+ @classmethod
+ def bar(cls): pass
+ self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
+
+ class C(metaclass=abc.ABCMeta):
+ @abc.abstractclassmethod
+ def foo(cls): return cls.__name__
+ self.assertRaises(TypeError, C)
+ class D(C):
+ @classmethod
+ def foo(cls): return super().foo()
+ self.assertEqual(D.foo(), 'D')
+ self.assertEqual(D().foo(), 'D')
+
+ def test_abstractstaticmethod_basics(self):
+ @abc.abstractstaticmethod
+ def foo(): pass
+ self.assertEqual(foo.__isabstractmethod__, True)
+ @staticmethod
+ def bar(): pass
+ self.assertEqual(hasattr(bar, "__isabstractmethod__"), False)
+
+ class C(metaclass=abc.ABCMeta):
+ @abc.abstractstaticmethod
+ def foo(): return 3
+ self.assertRaises(TypeError, C)
+ class D(C):
+ @staticmethod
+ def foo(): return 4
+ self.assertEqual(D.foo(), 4)
+ self.assertEqual(D().foo(), 4)
+
def test_abstractmethod_integration(self):
- for abstractthing in [abc.abstractmethod, abc.abstractproperty]:
+ for abstractthing in [abc.abstractmethod, abc.abstractproperty,
+ abc.abstractclassmethod,
+ abc.abstractstaticmethod]:
class C(metaclass=abc.ABCMeta):
@abstractthing
def foo(self): pass # abstract
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Tue Aug 17 02:52:52 2010
@@ -90,6 +90,8 @@
Library
-------
+- Issue #5867: Add abc.abstractclassmethod and abc.abstractstaticmethod.
+
- Issue #9605: posix.getlogin() decodes the username with file filesystem
encoding and surrogateescape error handler. Patch written by David Watson.
More information about the Python-checkins
mailing list