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

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 from warnings import warn as _warn
8 7
9 def abstractmethod(funcobj): 8 def abstractmethod(funcobj):
10 """A decorator indicating abstract methods. 9 """A decorator indicating abstract methods.
11 10
12 Requires that the metaclass is ABCMeta or derived from it. A 11 Requires that the metaclass is ABCMeta or derived from it. A
13 class that has a metaclass derived from ABCMeta cannot be 12 class that has a metaclass derived from ABCMeta cannot be
14 instantiated unless all of its abstract methods are overridden. 13 instantiated unless all of its abstract methods are overridden.
15 The abstract methods can be called using any of the normal 14 The abstract methods can be called using any of the normal
16 'super' call mechanisms. 15 'super' call mechanisms.
17 16
18 Usage: 17 Usage:
19 18
20 class C(metaclass=ABCMeta): 19 class C(metaclass=ABCMeta):
21 @abstractmethod 20 @abstractmethod
22 def my_abstract_method(self, ...): 21 def my_abstract_method(self, ...):
23 ... 22 ...
24 """ 23 """
25 funcobj.__isabstractmethod__ = True 24 funcobj.__isabstractmethod__ = True
26 return funcobj 25 return funcobj
27 26
28 27
29 class abstractclassmethod(classmethod): 28 class abstractclassmethod(classmethod):
30 """A decorator indicating abstract classmethods. 29 """
30 A decorator indicating abstract classmethods.
31 31
32 Similar to abstractmethod. 32 Similar to abstractmethod.
33 33
34 Usage: 34 Usage:
35 35
36 class C(metaclass=ABCMeta): 36 class C(metaclass=ABCMeta):
37 @abstractclassmethod 37 @abstractclassmethod
38 def my_abstract_classmethod(cls, ...): 38 def my_abstract_classmethod(cls, ...):
39 ... 39 ...
40
41 'abstractclassmethod' is deprecated. Use 'classmethod' with
42 'abstractmethod' instead.
40 """ 43 """
41 44
42 __isabstractmethod__ = True 45 __isabstractmethod__ = True
43 46
44 def __init__(self, callable): 47 def __init__(self, callable):
45 callable.__isabstractmethod__ = True 48 callable.__isabstractmethod__ = True
46 super().__init__(callable) 49 super().__init__(callable)
47 50
48 51
49 class abstractstaticmethod(staticmethod): 52 class abstractstaticmethod(staticmethod):
50 """A decorator indicating abstract staticmethods. 53 """
54 A decorator indicating abstract staticmethods.
51 55
52 Similar to abstractmethod. 56 Similar to abstractmethod.
53 57
54 Usage: 58 Usage:
55 59
56 class C(metaclass=ABCMeta): 60 class C(metaclass=ABCMeta):
57 @abstractstaticmethod 61 @abstractstaticmethod
58 def my_abstract_staticmethod(...): 62 def my_abstract_staticmethod(...):
59 ... 63 ...
64
65 'abstractstaticmethod' is deprecated. Use 'staticmethod' with
66 'abstractmethod' instead.
60 """ 67 """
61 68
62 __isabstractmethod__ = True 69 __isabstractmethod__ = True
63 70
64 def __init__(self, callable): 71 def __init__(self, callable):
65 callable.__isabstractmethod__ = True 72 callable.__isabstractmethod__ = True
66 super().__init__(callable) 73 super().__init__(callable)
67 74
68 75
69 class abstractproperty(property): 76 class abstractproperty(property):
70 """A decorator indicating abstract properties. 77 """
78 A decorator indicating abstract properties.
71 79
72 Requires that the metaclass is ABCMeta or derived from it. A 80 Requires that the metaclass is ABCMeta or derived from it. A
73 class that has a metaclass derived from ABCMeta cannot be 81 class that has a metaclass derived from ABCMeta cannot be
74 instantiated unless all of its abstract properties are overridden. 82 instantiated unless all of its abstract properties are overridden.
75 The abstract properties can be called using any of the normal 83 The abstract properties can be called using any of the normal
76 'super' call mechanisms. 84 'super' call mechanisms.
77 85
78 Usage: 86 Usage:
79 87
80 class C(metaclass=ABCMeta): 88 class C(metaclass=ABCMeta):
81 @abstractproperty 89 @abstractproperty
82 def my_abstract_property(self): 90 def my_abstract_property(self):
83 ... 91 ...
84 92
85 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
86 abstract property using the 'long' form of property declaration: 94 abstract property using the 'long' form of property declaration:
87 95
88 class C(metaclass=ABCMeta): 96 class C(metaclass=ABCMeta):
89 def getx(self): ... 97 def getx(self): ...
90 def setx(self, value): ... 98 def setx(self, value): ...
91 x = abstractproperty(getx, setx) 99 x = abstractproperty(getx, setx)
92 """ 100
93 101 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod'
102 instead.
103 """
104
94 __isabstractmethod__ = True 105 __isabstractmethod__ = True
95
96 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
97 _warn(
98 "abstractproperty will be removed in future versions. "
99 "Use 'property' in conjunction with 'abstractmethod' instead.",
100 DeprecationWarning, stacklevel=2
101 )
102 super().__init__(self, fget, fset, fdel, doc)
103 106
104 107
105 class ABCMeta(type): 108 class ABCMeta(type):
106 109
107 """Metaclass for defining Abstract Base Classes (ABCs). 110 """Metaclass for defining Abstract Base Classes (ABCs).
108 111
109 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
110 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
111 unrelated concrete classes (even built-in classes) and unrelated 114 unrelated concrete classes (even built-in classes) and unrelated
112 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
216 cls._abc_cache.add(subclass) 219 cls._abc_cache.add(subclass)
217 return True 220 return True
218 # Check if it's a subclass of a subclass (recursive) 221 # Check if it's a subclass of a subclass (recursive)
219 for scls in cls.__subclasses__(): 222 for scls in cls.__subclasses__():
220 if issubclass(subclass, scls): 223 if issubclass(subclass, scls):
221 cls._abc_cache.add(subclass) 224 cls._abc_cache.add(subclass)
222 return True 225 return True
223 # No dice; update negative cache 226 # No dice; update negative cache
224 cls._abc_negative_cache.add(subclass) 227 cls._abc_negative_cache.add(subclass)
225 return False 228 return False
LEFTRIGHT

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