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

Delta Between Two Patch Sets: Lib/abc.py

Issue 11610: Improving property to accept abstract methods
Left Patch Set: Created 8 years, 8 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 """ 23 """
24 funcobj.__isabstractmethod__ = True 24 funcobj.__isabstractmethod__ = True
25 return funcobj 25 return funcobj
26 26
27 27
28 class abstractclassmethod(classmethod): 28 class abstractclassmethod(classmethod):
29 """A decorator indicating abstract classmethods. 29 """
30 A decorator indicating abstract classmethods.
30 31
31 Similar to abstractmethod. 32 Similar to abstractmethod.
32 33
33 Usage: 34 Usage:
34 35
35 class C(metaclass=ABCMeta): 36 class C(metaclass=ABCMeta):
36 @abstractclassmethod 37 @abstractclassmethod
37 def my_abstract_classmethod(cls, ...): 38 def my_abstract_classmethod(cls, ...):
38 ... 39 ...
40
41 'abstractclassmethod' is deprecated. Use 'classmethod' with
42 'abstractmethod' instead.
39 """ 43 """
40 44
41 __isabstractmethod__ = True 45 __isabstractmethod__ = True
42 46
43 def __init__(self, callable): 47 def __init__(self, callable):
44 callable.__isabstractmethod__ = True 48 callable.__isabstractmethod__ = True
45 super().__init__(callable) 49 super().__init__(callable)
46 50
47 51
48 class abstractstaticmethod(staticmethod): 52 class abstractstaticmethod(staticmethod):
49 """A decorator indicating abstract staticmethods. 53 """
54 A decorator indicating abstract staticmethods.
50 55
51 Similar to abstractmethod. 56 Similar to abstractmethod.
52 57
53 Usage: 58 Usage:
54 59
55 class C(metaclass=ABCMeta): 60 class C(metaclass=ABCMeta):
56 @abstractstaticmethod 61 @abstractstaticmethod
57 def my_abstract_staticmethod(...): 62 def my_abstract_staticmethod(...):
58 ... 63 ...
64
65 'abstractstaticmethod' is deprecated. Use 'staticmethod' with
66 'abstractmethod' instead.
59 """ 67 """
60 68
61 __isabstractmethod__ = True 69 __isabstractmethod__ = True
62 70
63 def __init__(self, callable): 71 def __init__(self, callable):
64 callable.__isabstractmethod__ = True 72 callable.__isabstractmethod__ = True
65 super().__init__(callable) 73 super().__init__(callable)
66 74
67 75
68 class abstractproperty(property): 76 class abstractproperty(property):
69 """A decorator indicating abstract properties. 77 """
78 A decorator indicating abstract properties.
70 79
71 Requires that the metaclass is ABCMeta or derived from it. A 80 Requires that the metaclass is ABCMeta or derived from it. A
72 class that has a metaclass derived from ABCMeta cannot be 81 class that has a metaclass derived from ABCMeta cannot be
73 instantiated unless all of its abstract properties are overridden. 82 instantiated unless all of its abstract properties are overridden.
74 The abstract properties can be called using any of the normal 83 The abstract properties can be called using any of the normal
75 'super' call mechanisms. 84 'super' call mechanisms.
76 85
77 Usage: 86 Usage:
78 87
79 class C(metaclass=ABCMeta): 88 class C(metaclass=ABCMeta):
80 @abstractproperty 89 @abstractproperty
81 @abstractmethod 90 def my_abstract_property(self):
gvanrossum 2011/03/21 03:25:07 You can't just change the semantics of @abstractpr
dsdale24 2011/03/21 13:09:06 The patch itself is actually completely backward-c
82 def x(self): 91 ...
83 ... 92
84 @x.setter 93 This defines a read-only property; you can also define a read-write
dsdale24 2011/03/21 13:09:06 I forgot to insert an @abstractmethod after @x.set
85 def x(self, val): 94 abstract property using the 'long' form of property declaration:
86 ... 95
87 96 class C(metaclass=ABCMeta):
88 You can also define an abstract property using the 'long' form of 97 def getx(self): ...
89 property declaration: 98 def setx(self, value): ...
90
91 class D(metaclass=ABCMeta):
92 @abstractmethod
93 def getx(self):
94 ...
95 @abstractmethod
96 def setx(self, value):
97 ...
98 x = abstractproperty(getx, setx) 99 x = abstractproperty(getx, setx)
99 100
100 Subclasses will not be instantiable until the property definition 101 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod'
101 is fully concrete: 102 instead.
102
103 class E(C):
104 @C.x.getter
105 def x(self):
106 ...
107
108 class F(E):
109 @D.x.setter
110 def x(self, val):
111 ...
112
113 In this example, 'E' is not instantiable, because the 'x' setter
114 is abstract. 'F.x' is a regular property , and 'F' is instantiable.
115 """ 103 """
116 104
117 __isabstractmethod__ = True 105 __isabstractmethod__ = True
118
119 def getter(self, fun):
120 "Descriptor to change the getter on a property"
121 return abstractproperty.create_property(fun, self.fset, self.fdel,
dsdale24 2011/03/22 22:45:04 I think the calls in getter, setter, deleter need
122 self.__doc__)
123
124 def setter(self, fun):
125 "Descriptor to change the setter on a property"
126 return abstractproperty.create_property(self.fget, fun, self.fdel,
127 self.__doc__)
128
129 def deleter(self, fun):
130 "Descriptor to change the deleter on a property"
131 return abstractproperty.create_property(self.fget, self.fset, fun,
132 self.__doc__)
133
134 @classmethod
135 def create_property(cls, fget=None, fset=None, fdel=None, doc=None):
136 for f in (fget, fset, fdel):
137 if (f is not None) and getattr(f, '__isabstractmethod__', False):
138 # has abstract methods, return an abstract property
139 return cls(fget, fset, fdel, doc)
140 # does not have abstract methods, return a concrete property
141 return property(fget, fset, fdel, doc)
142 106
143 107
144 class ABCMeta(type): 108 class ABCMeta(type):
145 109
146 """Metaclass for defining Abstract Base Classes (ABCs). 110 """Metaclass for defining Abstract Base Classes (ABCs).
147 111
148 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
149 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
150 unrelated concrete classes (even built-in classes) and unrelated 114 unrelated concrete classes (even built-in classes) and unrelated
151 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
255 cls._abc_cache.add(subclass) 219 cls._abc_cache.add(subclass)
256 return True 220 return True
257 # Check if it's a subclass of a subclass (recursive) 221 # Check if it's a subclass of a subclass (recursive)
258 for scls in cls.__subclasses__(): 222 for scls in cls.__subclasses__():
259 if issubclass(subclass, scls): 223 if issubclass(subclass, scls):
260 cls._abc_cache.add(subclass) 224 cls._abc_cache.add(subclass)
261 return True 225 return True
262 # No dice; update negative cache 226 # No dice; update negative cache
263 cls._abc_negative_cache.add(subclass) 227 cls._abc_negative_cache.add(subclass)
264 return False 228 return False
LEFTRIGHT

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