Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(167627)

Delta Between Two Patch Sets: Lib/abc.py

Issue 11610: Improving property to accept abstract methods
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 8 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Include/object.h ('k') | Lib/numbers.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright 2007 Google, Inc. All Rights Reserved. 1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """Abstract Base Classes (ABCs) according to PEP 3119.""" 4 """Abstract Base Classes (ABCs) according to PEP 3119."""
5 5
6 from _weakrefset import WeakSet 6 from _weakrefset import WeakSet
7 7
8 def abstractmethod(funcobj): 8 def abstractmethod(funcobj):
9 """A decorator indicating abstract methods. 9 """A decorator indicating abstract methods.
10 10
11 Requires that the metaclass is ABCMeta or derived from it. A 11 Requires that the metaclass is ABCMeta or derived from it. A
12 class that has a metaclass derived from ABCMeta cannot be 12 class that has a metaclass derived from ABCMeta cannot be
13 instantiated unless all of its abstract methods are overridden. 13 instantiated unless all of its abstract methods are overridden.
14 The abstract methods can be called using any of the normal 14 The abstract methods can be called using any of the normal
15 'super' call mechanisms. 15 'super' call mechanisms.
16 16
17 Usage: 17 Usage:
18 18
19 class C(metaclass=ABCMeta): 19 class C(metaclass=ABCMeta):
20 @abstractmethod 20 @abstractmethod
21 def my_abstract_method(self, ...): 21 def my_abstract_method(self, ...):
22 ... 22 ...
23 @classmethod
24 @abstractmethod
25 def my_abstract_classmethod(self, ...):
26 ...
27 @staticmethod
28 @abstractmethod
29 def my_abstract_method(self, ...):
30 ...
31
32 @property
33 @abstractmethod
34 def my_abstract_property(self):
35 ...
36 @my_abstract_property.setter
37 @abstractmethod
38 def my_abstract_property(self, val):
39 ...
40
41 @abstractmethod
42 def _get_x(self):
43 ...
44 @abstractmethod
45 def _set_x(self, val):
46 ...
47 x = property(_get_x, _set_x)
48 """ 23 """
49 funcobj.__isabstractmethod__ = True 24 funcobj.__isabstractmethod__ = True
50 return funcobj 25 return funcobj
51 26
52 27
53 class abstractclassmethod(classmethod): 28 class abstractclassmethod(classmethod):
54 """ 29 """
55 .. deprecated:: 3.3
Benjamin Peterson 2011/11/29 02:45:54 Don't use reST in doc strings.
56 Use :class:`classmethod` and :func:`abstractmethod` instead.
57
58 A decorator indicating abstract classmethods. 30 A decorator indicating abstract classmethods.
59 31
60 Similar to abstractmethod. 32 Similar to abstractmethod.
61 33
62 Usage: 34 Usage:
63 35
64 class C(metaclass=ABCMeta): 36 class C(metaclass=ABCMeta):
65 @abstractclassmethod 37 @abstractclassmethod
66 def my_abstract_classmethod(cls, ...): 38 def my_abstract_classmethod(cls, ...):
67 ... 39 ...
40
41 'abstractclassmethod' is deprecated. Use 'classmethod' with
42 'abstractmethod' instead.
68 """ 43 """
69 44
70 __isabstractmethod__ = True 45 __isabstractmethod__ = True
71 46
72 def __init__(self, callable): 47 def __init__(self, callable):
73 import warnings
Benjamin Peterson 2011/11/29 02:45:54 I don't think these should give a warning at the m
dsdale24 2011/11/29 17:20:36 This was added in response to the comment at http:
Benjamin Peterson 2011/11/29 17:27:46 Yes, but the old way is harmless and there's no re
74 warnings.warn("abstractclassmethod is deprecated",
75 DeprecationWarning, 2)
76 callable.__isabstractmethod__ = True 48 callable.__isabstractmethod__ = True
77 super().__init__(callable) 49 super().__init__(callable)
78 50
79 51
80 class abstractstaticmethod(staticmethod): 52 class abstractstaticmethod(staticmethod):
81 """ 53 """
82 .. deprecated:: 3.3
83 Use :class:`staticmethod` and :func:`abstractmethod` instead.
84
85 A decorator indicating abstract staticmethods. 54 A decorator indicating abstract staticmethods.
86 55
87 Similar to abstractmethod. 56 Similar to abstractmethod.
88 57
89 Usage: 58 Usage:
90 59
91 class C(metaclass=ABCMeta): 60 class C(metaclass=ABCMeta):
92 @abstractstaticmethod 61 @abstractstaticmethod
93 def my_abstract_staticmethod(...): 62 def my_abstract_staticmethod(...):
94 ... 63 ...
64
65 'abstractstaticmethod' is deprecated. Use 'staticmethod' with
66 'abstractmethod' instead.
95 """ 67 """
96 68
97 __isabstractmethod__ = True 69 __isabstractmethod__ = True
98 70
99 def __init__(self, callable): 71 def __init__(self, callable):
100 import warnings
101 warnings.warn("abstractstaticmethod is deprecated",
102 DeprecationWarning, 2)
103 callable.__isabstractmethod__ = True 72 callable.__isabstractmethod__ = True
104 super().__init__(callable) 73 super().__init__(callable)
105 74
75
106 class abstractproperty(property): 76 class abstractproperty(property):
107 """ 77 """
108 .. deprecated:: 3.3
109 Use :class:`property` and :func:`abstractmethod` instead.
110
111 A decorator indicating abstract properties. 78 A decorator indicating abstract properties.
112 79
113 Requires that the metaclass is ABCMeta or derived from it. A 80 Requires that the metaclass is ABCMeta or derived from it. A
114 class that has a metaclass derived from ABCMeta cannot be 81 class that has a metaclass derived from ABCMeta cannot be
115 instantiated unless all of its abstract properties are overridden. 82 instantiated unless all of its abstract properties are overridden.
116 The abstract properties can be called using any of the normal 83 The abstract properties can be called using any of the normal
117 'super' call mechanisms. 84 'super' call mechanisms.
118 85
119 Usage: 86 Usage:
120 87
121 class C(metaclass=ABCMeta): 88 class C(metaclass=ABCMeta):
122 @abstractproperty 89 @abstractproperty
123 def my_abstract_property(self): 90 def my_abstract_property(self):
124 ... 91 ...
125 92
126 This defines a read-only property; you can also define a read-write 93 This defines a read-only property; you can also define a read-write
127 abstract property using the 'long' form of property declaration: 94 abstract property using the 'long' form of property declaration:
128 95
129 class C(metaclass=ABCMeta): 96 class C(metaclass=ABCMeta):
130 def getx(self): ... 97 def getx(self): ...
131 def setx(self, value): ... 98 def setx(self, value): ...
132 x = abstractproperty(getx, setx) 99 x = abstractproperty(getx, setx)
133 100
134 """ 101 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod'
102 instead.
103 """
104
135 __isabstractmethod__ = True 105 __isabstractmethod__ = True
136
137 def __init__(self, *args, **kwargs):
138 import warnings
139 warnings.warn("abstractproperty is deprecated", DeprecationWarning, 2)
140 super().__init__(*args, **kwargs)
141 106
142 107
143 class ABCMeta(type): 108 class ABCMeta(type):
144 109
145 """Metaclass for defining Abstract Base Classes (ABCs). 110 """Metaclass for defining Abstract Base Classes (ABCs).
146 111
147 Use this metaclass to create an ABC. An ABC can be subclassed 112 Use this metaclass to create an ABC. An ABC can be subclassed
148 directly, and then acts as a mix-in class. You can also register 113 directly, and then acts as a mix-in class. You can also register
149 unrelated concrete classes (even built-in classes) and unrelated 114 unrelated concrete classes (even built-in classes) and unrelated
150 ABCs as 'virtual subclasses' -- these and their descendants will 115 ABCs as 'virtual subclasses' -- these and their descendants will
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 cls._abc_cache.add(subclass) 219 cls._abc_cache.add(subclass)
255 return True 220 return True
256 # Check if it's a subclass of a subclass (recursive) 221 # Check if it's a subclass of a subclass (recursive)
257 for scls in cls.__subclasses__(): 222 for scls in cls.__subclasses__():
258 if issubclass(subclass, scls): 223 if issubclass(subclass, scls):
259 cls._abc_cache.add(subclass) 224 cls._abc_cache.add(subclass)
260 return True 225 return True
261 # No dice; update negative cache 226 # No dice; update negative cache
262 cls._abc_negative_cache.add(subclass) 227 cls._abc_negative_cache.add(subclass)
263 return False 228 return False
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+