| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,32 +127,71 @@ This module provides the following class: | |||
| 127 | 127 | available as a method of ``Foo``, so it is provided separately. | |
| 128 | 128 | ||
| 129 | 129 | ||
| 130 | - It also provides the following decorators: | ||
| 130 | + The :mod:`abc` module also provides the following decorators: | ||
| 131 | 131 | ||
| 132 | 132 | .. decorator:: abstractmethod(function) | |
| 133 | 133 | ||
| 134 | 134 | A decorator indicating abstract methods. | |
| 135 | 135 | ||
| 136 | - Using this decorator requires that the class's metaclass is :class:`ABCMeta` or | ||
| 137 | - is derived from it. | ||
| 138 | - A class that has a metaclass derived from :class:`ABCMeta` | ||
| 139 | - cannot be instantiated unless all of its abstract methods and | ||
| 140 | - properties are overridden. | ||
| 141 | - The abstract methods can be called using any of the normal 'super' call | ||
| 142 | - mechanisms. | ||
| 136 | + Using this decorator requires that the class's metaclass is :class:`ABCMeta` | ||
| 137 | + or is derived from it. A class that has a metaclass derived from | ||
| 138 | + :class:`ABCMeta` cannot be instantiated unless all of its abstract methods | ||
| 139 | + and properties are overridden. The abstract methods can be called using any | ||
| 140 | + of the normal 'super' call mechanisms. :func:`abstractmethod` may be used | ||
| 141 | + to declare abstract methods for properties and descriptors. | ||
| 143 | 142 | ||
| 144 | 143 | Dynamically adding abstract methods to a class, or attempting to modify the | |
| 145 | 144 | abstraction status of a method or class once it is created, are not | |
| 146 | 145 | supported. The :func:`abstractmethod` only affects subclasses derived using | |
| 147 | 146 | regular inheritance; "virtual subclasses" registered with the ABC's | |
| 148 | 147 | :meth:`register` method are not affected. | |
| 149 | 148 | ||
| 150 | - Usage:: | ||
| 149 | + When :func:`abstractmethod` is applied in combination with other method | ||
| 150 | + descriptors, it should be applied as the innermost decorator, as shown in | ||
| 151 | + the following usage examples:: | ||
| 151 | 152 | ||
| 152 | 153 | class C(metaclass=ABCMeta): | |
| 153 | 154 | @abstractmethod | |
| 154 | 155 | def my_abstract_method(self, ...): | |
| 155 | 156 | ... | |
| 157 | + @classmethod | ||
| 158 | + @abstractmethod | ||
| 159 | + def my_abstract_classmethod(cls, ...): | ||
| 160 | + ... | ||
| 161 | + @staticmethod | ||
| 162 | + @abstractmethod | ||
| 163 | + def my_abstract_staticmethod(...): | ||
| 164 | + ... | ||
| 165 | + | ||
| 166 | + @property | ||
| 167 | + @abstractmethod | ||
| 168 | + def my_abstract_property(self): | ||
| 169 | + ... | ||
| 170 | + @my_abstract_property.setter | ||
| 171 | + @abstractmethod | ||
| 172 | + def my_abstract_property(self, val): | ||
| 173 | + ... | ||
| 174 | + | ||
| 175 | + @abstractmethod | ||
| 176 | + def _get_x(self): | ||
| 177 | + ... | ||
| 178 | + @abstractmethod | ||
| 179 | + def _set_x(self, val): | ||
| 180 | + ... | ||
| 181 | + x = property(_get_x, _set_x) | ||
| 182 | + | ||
| 183 | + In order to correctly interoperate with the abstract base class machinery, | ||
| 184 | + the descriptor must identify itself as abstract using | ||
| 185 | + :attr:`__isabstractmethod__`. In general, this attribute should be ``True`` | ||
| 186 | + if any of the methods used to compose the descriptor are abstract. For | ||
| 187 | + example, Python's built-in property does the equivalent of:: | ||
| 188 | + | ||
| 189 | + class Descriptor: | ||
| 190 | + ... | ||
| 191 | + @property | ||
| 192 | + def __isabstractmethod__(self): | ||
| 193 | + return any(getattr(f, '__isabstractmethod__', False) for | ||
| 194 | + f in (self._fget, self._fset, self._fdel)) | ||
| 156 | 195 | ||
| 157 | 196 | .. note:: | |
| 158 | 197 | ||
@@ -177,6 +216,8 @@ It also provides the following decorators: | |||
| 177 | 216 | ... | |
| 178 | 217 | ||
| 179 | 218 | .. versionadded:: 3.2 | |
| 219 | + .. deprecated:: 3.3 | ||
| 220 | + Use :class:`classmethod` with :func:`abstractmethod` instead | ||
| 180 | 221 | ||
| 181 | 222 | ||
| 182 | 223 | .. decorator:: abstractstaticmethod(function) | |
@@ -192,18 +233,19 @@ It also provides the following decorators: | |||
| 192 | 233 | ... | |
| 193 | 234 | ||
| 194 | 235 | .. versionadded:: 3.2 | |
| 236 | + .. deprecated:: 3.3 | ||
| 237 | + Use :class:`staticmethod` with :func:`abstractmethod` instead | ||
| 195 | 238 | ||
| 196 | 239 | ||
| 197 | 240 | .. decorator:: abstractproperty(fget=None, fset=None, fdel=None, doc=None) | |
| 198 | 241 | ||
| 199 | 242 | A subclass of the built-in :func:`property`, indicating an abstract property. | |
| 200 | 243 | ||
| 201 | - Using this function requires that the class's metaclass is :class:`ABCMeta` or | ||
| 202 | - is derived from it. | ||
| 203 | - A class that has a metaclass derived from :class:`ABCMeta` cannot be | ||
| 204 | - instantiated unless all of its abstract methods and properties are overridden. | ||
| 205 | - The abstract properties can be called using any of the normal | ||
| 206 | - 'super' call mechanisms. | ||
| 244 | + Using this function requires that the class's metaclass is :class:`ABCMeta` | ||
| 245 | + or is derived from it. A class that has a metaclass derived from | ||
| 246 | + :class:`ABCMeta` cannot be instantiated unless all of its abstract methods | ||
| 247 | + and properties are overridden. The abstract properties can be called using | ||
| 248 | + any of the normal 'super' call mechanisms. | ||
| 207 | 249 | ||
| 208 | 250 | Usage:: | |
| 209 | 251 | ||
@@ -220,6 +262,9 @@ It also provides the following decorators: | |||
| 220 | 262 | def setx(self, value): ... | |
| 221 | 263 | x = abstractproperty(getx, setx) | |
| 222 | 264 | ||
| 265 | + .. deprecated:: 3.3 | ||
| 266 | + Use :class:`property` with :func:`abstractmethod` instead | ||
| 267 | + | ||
| 223 | 268 | ||
| 224 | 269 | .. rubric:: Footnotes | |
| 225 | 270 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -352,6 +352,23 @@ curses | |||
| 352 | 352 | ||
| 353 | 353 | (Contributed by Iñigo Serna in :issue:`6755`) | |
| 354 | 354 | ||
| 355 | + abc | ||
| 356 | + --- | ||
| 357 | + | ||
| 358 | + Improved support for abstract base classes containing descriptors composed with | ||
| 359 | + abstract methods. The recommended approach to declaring abstract descriptors is | ||
| 360 | + now to provide :attr:`__isabstractmethod__` as a dynamically updated | ||
| 361 | + property. The built-in descriptors have been updated accordingly. | ||
| 362 | + | ||
| 363 | + * :class:`abc.abstractproperty` has been deprecated, use :class:`property` | ||
| 364 | + with :func:`abc.abstractmethod` instead. | ||
| 365 | + * :class:`abc.abstractclassmethod` has been deprecated, use | ||
| 366 | + :class:`classmethod` with :func:`abc.abstractmethod` instead. | ||
| 367 | + * :class:`abc.abstractstaticmethod` has been deprecated, use | ||
| 368 | + :class:`property` with :func:`abc.abstractmethod` instead. | ||
| 369 | + | ||
| 370 | + (Contributed by Darren Dale in :issue:`11610`) | ||
| 371 | + | ||
| 355 | 372 | faulthandler | |
| 356 | 373 | ------------ | |
| 357 | 374 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -473,6 +473,7 @@ PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); | |||
| 473 | 473 | PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); | |
| 474 | 474 | PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); | |
| 475 | 475 | PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); | |
| 476 | + PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); | ||
| 476 | 477 | PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); | |
| 477 | 478 | PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); | |
| 478 | 479 | PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,8 @@ def my_abstract_method(self, ...): | |||
| 26 | 26 | ||
| 27 | 27 | ||
| 28 | 28 | class abstractclassmethod(classmethod): | |
| 29 | - """A decorator indicating abstract classmethods. | ||
| 29 | + """ | ||
| 30 | + A decorator indicating abstract classmethods. | ||
| 30 | 31 | ||
| 31 | 32 | Similar to abstractmethod. | |
| 32 | 33 | ||
@@ -36,6 +37,9 @@ class C(metaclass=ABCMeta): | |||
| 36 | 37 | @abstractclassmethod | |
| 37 | 38 | def my_abstract_classmethod(cls, ...): | |
| 38 | 39 | ... | |
| 40 | + | ||
| 41 | + 'abstractclassmethod' is deprecated. Use 'classmethod' with | ||
| 42 | + 'abstractmethod' instead. | ||
| 39 | 43 | """ | |
| 40 | 44 | ||
| 41 | 45 | __isabstractmethod__ = True | |
@@ -46,7 +50,8 @@ def __init__(self, callable): | |||
| 46 | 50 | ||
| 47 | 51 | ||
| 48 | 52 | class abstractstaticmethod(staticmethod): | |
| 49 | - """A decorator indicating abstract staticmethods. | ||
| 53 | + """ | ||
| 54 | + A decorator indicating abstract staticmethods. | ||
| 50 | 55 | ||
| 51 | 56 | Similar to abstractmethod. | |
| 52 | 57 | ||
@@ -56,6 +61,9 @@ class C(metaclass=ABCMeta): | |||
| 56 | 61 | @abstractstaticmethod | |
| 57 | 62 | def my_abstract_staticmethod(...): | |
| 58 | 63 | ... | |
| 64 | + | ||
| 65 | + 'abstractstaticmethod' is deprecated. Use 'staticmethod' with | ||
| 66 | + 'abstractmethod' instead. | ||
| 59 | 67 | """ | |
| 60 | 68 | ||
| 61 | 69 | __isabstractmethod__ = True | |
@@ -66,7 +74,8 @@ def __init__(self, callable): | |||
| 66 | 74 | ||
| 67 | 75 | ||
| 68 | 76 | class abstractproperty(property): | |
| 69 | - """A decorator indicating abstract properties. | ||
| 77 | + """ | ||
| 78 | + A decorator indicating abstract properties. | ||
| 70 | 79 | ||
| 71 | 80 | Requires that the metaclass is ABCMeta or derived from it. A | |
| 72 | 81 | class that has a metaclass derived from ABCMeta cannot be | |
@@ -88,7 +97,11 @@ class C(metaclass=ABCMeta): | |||
| 88 | 97 | def getx(self): ... | |
| 89 | 98 | def setx(self, value): ... | |
| 90 | 99 | x = abstractproperty(getx, setx) | |
| 100 | + | ||
| 101 | + 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod' | ||
| 102 | + instead. | ||
| 91 | 103 | """ | |
| 104 | + | ||
| 92 | 105 | __isabstractmethod__ = True | |
| 93 | 106 | ||
| 94 | 107 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | TODO: Fill out more detailed documentation on the operators.""" | |
| 7 | 7 | ||
| 8 | - from abc import ABCMeta, abstractmethod, abstractproperty | ||
| 8 | + from abc import ABCMeta, abstractmethod | ||
| 9 | 9 | ||
| 10 | 10 | __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] | |
| 11 | 11 | ||
@@ -50,15 +50,17 @@ def __bool__(self): | |||
| 50 | 50 | """True if self != 0. Called for bool(self).""" | |
| 51 | 51 | return self != 0 | |
| 52 | 52 | ||
| 53 | - @abstractproperty | ||
| 53 | + @property | ||
| 54 | + @abstractmethod | ||
| 54 | 55 | def real(self): | |
| 55 | 56 | """Retrieve the real component of this number. | |
| 56 | 57 | ||
| 57 | 58 | This should subclass Real. | |
| 58 | 59 | """ | |
| 59 | 60 | raise NotImplementedError | |
| 60 | 61 | ||
| 61 | - @abstractproperty | ||
| 62 | + @property | ||
| 63 | + @abstractmethod | ||
| 62 | 64 | def imag(self): | |
| 63 | 65 | """Retrieve the imaginary component of this number. | |
| 64 | 66 | ||
@@ -272,11 +274,13 @@ class Rational(Real): | |||
| 272 | 274 | ||
| 273 | 275 | __slots__ = () | |
| 274 | 276 | ||
| 275 | - @abstractproperty | ||
| 277 | + @property | ||
| 278 | + @abstractmethod | ||
| 276 | 279 | def numerator(self): | |
| 277 | 280 | raise NotImplementedError | |
| 278 | 281 | ||
| 279 | - @abstractproperty | ||
| 282 | + @property | ||
| 283 | + @abstractmethod | ||
| 280 | 284 | def denominator(self): | |
| 281 | 285 | raise NotImplementedError | |
| 282 | 286 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments