| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,6 +157,32 @@ It also provides the following decorators: | |||
| 157 | 157 | multiple-inheritance. | |
| 158 | 158 | ||
| 159 | 159 | ||
| 160 | + .. decorator:: abstractclassmethod(function) | ||
| 161 | + | ||
| 162 | + A subclass of the built-in :func:`classmethod`, indicating an abstract | ||
| 163 | + classmethod. Otherwise it is similar to :func:`abstractmethod`. | ||
| 164 | + | ||
| 165 | + Usage:: | ||
| 166 | + | ||
| 167 | + class C(metaclass=ABCMeta): | ||
| 168 | + @abstractclassmethod | ||
| 169 | + def my_abstract_classmethod(cls, ...): | ||
| 170 | + ... | ||
| 171 | + | ||
| 172 | + | ||
| 173 | + .. decorator:: abstractstaticmethod(function) | ||
| 174 | + | ||
| 175 | + A subclass of the built-in :func:`staticmethod`, indicating an abstract | ||
| 176 | + staticmethod. Otherwise it is similar to :func:`abstractmethod`. | ||
| 177 | + | ||
| 178 | + Usage:: | ||
| 179 | + | ||
| 180 | + class C(metaclass=ABCMeta): | ||
| 181 | + @abstractstaticmethod | ||
| 182 | + def my_abstract_staticmethod(...): | ||
| 183 | + ... | ||
| 184 | + | ||
| 185 | + | ||
| 160 | 186 | .. function:: abstractproperty(fget=None, fset=None, fdel=None, doc=None) | |
| 161 | 187 | ||
| 162 | 188 | A subclass of the built-in :func:`property`, indicating an abstract property. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,46 @@ def my_abstract_method(self, ...): | |||
| 25 | 25 | return funcobj | |
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | + class abstractclassmethod(classmethod): | ||
| 29 | + """A decorator indicating abstract classmethods. | ||
| 30 | + | ||
| 31 | + Similar to abstractmethod. | ||
| 32 | + | ||
| 33 | + Usage: | ||
| 34 | + | ||
| 35 | + class C(metaclass=ABCMeta): | ||
| 36 | + @abstractclassmethod | ||
| 37 | + def my_abstract_classmethod(cls, ...): | ||
| 38 | + ... | ||
| 39 | + """ | ||
| 40 | + | ||
| 41 | + __isabstractmethod__ = True | ||
| 42 | + | ||
| 43 | + def __init__(self, callable): | ||
| 44 | + callable.__isabstractmethod__ = True | ||
| 45 | + super().__init__(callable) | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + class abstractstaticmethod(staticmethod): | ||
| 49 | + """A decorator indicating abstract staticmethods. | ||
| 50 | + | ||
| 51 | + Similar to abstractmethod. | ||
| 52 | + | ||
| 53 | + Usage: | ||
| 54 | + | ||
| 55 | + class C(metaclass=ABCMeta): | ||
| 56 | + @abstractstaticmethod | ||
| 57 | + def my_abstract_staticmethod(...): | ||
| 58 | + ... | ||
| 59 | + """ | ||
| 60 | + | ||
| 61 | + __isabstractmethod__ = True | ||
| 62 | + | ||
| 63 | + def __init__(self, callable): | ||
| 64 | + callable.__isabstractmethod__ = True | ||
| 65 | + super().__init__(callable) | ||
| 66 | + | ||
| 67 | + | ||
| 28 | 68 | class abstractproperty(property): | |
| 29 | 69 | """A decorator indicating abstract properties. | |
| 30 | 70 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -34,8 +34,46 @@ class D(C): | |||
| 34 | 34 | def foo(self): return super().foo | |
| 35 | 35 | self.assertEqual(D().foo, 3) | |
| 36 | 36 | ||
| 37 | + def test_abstractclassmethod_basics(self): | ||
| 38 | + @abc.abstractclassmethod | ||
| 39 | + def foo(cls): pass | ||
| 40 | + self.assertEqual(foo.__isabstractmethod__, True) | ||
| 41 | + @classmethod | ||
| 42 | + def bar(cls): pass | ||
| 43 | + self.assertEqual(hasattr(bar, "__isabstractmethod__"), False) | ||
| 44 | + | ||
| 45 | + class C(metaclass=abc.ABCMeta): | ||
| 46 | + @abc.abstractclassmethod | ||
| 47 | + def foo(cls): return cls.__name__ | ||
| 48 | + self.assertRaises(TypeError, C) | ||
| 49 | + class D(C): | ||
| 50 | + @classmethod | ||
| 51 | + def foo(cls): return super().foo() | ||
| 52 | + self.assertEqual(D.foo(), 'D') | ||
| 53 | + self.assertEqual(D().foo(), 'D') | ||
| 54 | + | ||
| 55 | + def test_abstractstaticmethod_basics(self): | ||
| 56 | + @abc.abstractstaticmethod | ||
| 57 | + def foo(): pass | ||
| 58 | + self.assertEqual(foo.__isabstractmethod__, True) | ||
| 59 | + @staticmethod | ||
| 60 | + def bar(): pass | ||
| 61 | + self.assertEqual(hasattr(bar, "__isabstractmethod__"), False) | ||
| 62 | + | ||
| 63 | + class C(metaclass=abc.ABCMeta): | ||
| 64 | + @abc.abstractstaticmethod | ||
| 65 | + def foo(): return 3 | ||
| 66 | + self.assertRaises(TypeError, C) | ||
| 67 | + class D(C): | ||
| 68 | + @staticmethod | ||
| 69 | + def foo(): return 4 | ||
| 70 | + self.assertEqual(D.foo(), 4) | ||
| 71 | + self.assertEqual(D().foo(), 4) | ||
| 72 | + | ||
| 37 | 73 | def test_abstractmethod_integration(self): | |
| 38 | - for abstractthing in [abc.abstractmethod, abc.abstractproperty]: | ||
| 74 | + for abstractthing in [abc.abstractmethod, abc.abstractproperty, | ||
| 75 | + abc.abstractclassmethod, | ||
| 76 | + abc.abstractstaticmethod]: | ||
| 39 | 77 | class C(metaclass=abc.ABCMeta): | |
| 40 | 78 | @abstractthing | |
| 41 | 79 | def foo(self): pass # abstract | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -90,6 +90,8 @@ Extensions | |||
| 90 | 90 | Library | |
| 91 | 91 | ------- | |
| 92 | 92 | ||
| 93 | + - Issue #5867: Add abc.abstractclassmethod and abc.abstractstaticmethod. | ||
| 94 | + | ||
| 93 | 95 | - Issue #9605: posix.getlogin() decodes the username with file filesystem | |
| 94 | 96 | encoding and surrogateescape error handler. Patch written by David Watson. | |
| 95 | 97 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments