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

Side by Side Diff: Lib/abc.py

Issue 11610: Improving property to accept abstract methods
Patch Set: Created 8 years, 4 months 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:
View unified diff | Download patch
OLDNEW
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)
23 """ 48 """
24 funcobj.__isabstractmethod__ = True 49 funcobj.__isabstractmethod__ = True
25 return funcobj 50 return funcobj
26 51
27 52
28 class abstractclassmethod(classmethod): 53 class abstractclassmethod(classmethod):
29 """A decorator indicating abstract classmethods. 54 """
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 59
31 Similar to abstractmethod. 60 Similar to abstractmethod.
32 61
33 Usage: 62 Usage:
34 63
35 class C(metaclass=ABCMeta): 64 class C(metaclass=ABCMeta):
36 @abstractclassmethod 65 @abstractclassmethod
37 def my_abstract_classmethod(cls, ...): 66 def my_abstract_classmethod(cls, ...):
38 ... 67 ...
39 """ 68 """
40 69
41 __isabstractmethod__ = True 70 __isabstractmethod__ = True
42 71
43 def __init__(self, callable): 72 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)
44 callable.__isabstractmethod__ = True 76 callable.__isabstractmethod__ = True
45 super().__init__(callable) 77 super().__init__(callable)
46 78
47 79
48 class abstractstaticmethod(staticmethod): 80 class abstractstaticmethod(staticmethod):
49 """A decorator indicating abstract staticmethods. 81 """
82 .. deprecated:: 3.3
83 Use :class:`staticmethod` and :func:`abstractmethod` instead.
84
85 A decorator indicating abstract staticmethods.
50 86
51 Similar to abstractmethod. 87 Similar to abstractmethod.
52 88
53 Usage: 89 Usage:
54 90
55 class C(metaclass=ABCMeta): 91 class C(metaclass=ABCMeta):
56 @abstractstaticmethod 92 @abstractstaticmethod
57 def my_abstract_staticmethod(...): 93 def my_abstract_staticmethod(...):
58 ... 94 ...
59 """ 95 """
60 96
61 __isabstractmethod__ = True 97 __isabstractmethod__ = True
62 98
63 def __init__(self, callable): 99 def __init__(self, callable):
100 import warnings
101 warnings.warn("abstractstaticmethod is deprecated",
102 DeprecationWarning, 2)
64 callable.__isabstractmethod__ = True 103 callable.__isabstractmethod__ = True
65 super().__init__(callable) 104 super().__init__(callable)
66 105
106 class abstractproperty(property):
107 """
108 .. deprecated:: 3.3
109 Use :class:`property` and :func:`abstractmethod` instead.
67 110
68 class abstractproperty(property): 111 A decorator indicating abstract properties.
69 """A decorator indicating abstract properties.
70 112
71 Requires that the metaclass is ABCMeta or derived from it. A 113 Requires that the metaclass is ABCMeta or derived from it. A
72 class that has a metaclass derived from ABCMeta cannot be 114 class that has a metaclass derived from ABCMeta cannot be
73 instantiated unless all of its abstract properties are overridden. 115 instantiated unless all of its abstract properties are overridden.
74 The abstract properties can be called using any of the normal 116 The abstract properties can be called using any of the normal
75 'super' call mechanisms. 117 'super' call mechanisms.
76 118
77 Usage: 119 Usage:
78 120
79 class C(metaclass=ABCMeta): 121 class C(metaclass=ABCMeta):
80 @abstractproperty 122 @abstractproperty
81 def my_abstract_property(self): 123 def my_abstract_property(self):
82 ... 124 ...
83 125
84 This defines a read-only property; you can also define a read-write 126 This defines a read-only property; you can also define a read-write
85 abstract property using the 'long' form of property declaration: 127 abstract property using the 'long' form of property declaration:
86 128
87 class C(metaclass=ABCMeta): 129 class C(metaclass=ABCMeta):
88 def getx(self): ... 130 def getx(self): ...
89 def setx(self, value): ... 131 def setx(self, value): ...
90 x = abstractproperty(getx, setx) 132 x = abstractproperty(getx, setx)
133
91 """ 134 """
92 __isabstractmethod__ = True 135 __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)
93 141
94 142
95 class ABCMeta(type): 143 class ABCMeta(type):
96 144
97 """Metaclass for defining Abstract Base Classes (ABCs). 145 """Metaclass for defining Abstract Base Classes (ABCs).
98 146
99 Use this metaclass to create an ABC. An ABC can be subclassed 147 Use this metaclass to create an ABC. An ABC can be subclassed
100 directly, and then acts as a mix-in class. You can also register 148 directly, and then acts as a mix-in class. You can also register
101 unrelated concrete classes (even built-in classes) and unrelated 149 unrelated concrete classes (even built-in classes) and unrelated
102 ABCs as 'virtual subclasses' -- these and their descendants will 150 ABCs as 'virtual subclasses' -- these and their descendants will
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 cls._abc_cache.add(subclass) 254 cls._abc_cache.add(subclass)
207 return True 255 return True
208 # Check if it's a subclass of a subclass (recursive) 256 # Check if it's a subclass of a subclass (recursive)
209 for scls in cls.__subclasses__(): 257 for scls in cls.__subclasses__():
210 if issubclass(subclass, scls): 258 if issubclass(subclass, scls):
211 cls._abc_cache.add(subclass) 259 cls._abc_cache.add(subclass)
212 return True 260 return True
213 # No dice; update negative cache 261 # No dice; update negative cache
214 cls._abc_negative_cache.add(subclass) 262 cls._abc_negative_cache.add(subclass)
215 return False 263 return False
OLDNEW

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