Skip to content

Commit 04f74cd

Browse files
authored
Update allowed Python dunder method set (#944)
Fixes #943
1 parent acbec79 commit 04f74cd

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313
* Updated dozens of type annotations in the compiler to satisfy MyPy 1.11 (#910)
1414
* Update the `StreamReader` methods to stop using the term "token" to refer to individual UTF-8 characters (#915)
15+
* Update the list of Python dunder methods which are allowed to be implemented for all `deftype*` and `reify*` types (#943)
1516

1617
### Fixed
1718
* Fix a bug where `.` characters were not allowed in keyword names (#899)

src/basilisp/lang/util.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,91 +86,113 @@ def is_abstract(tp: Type) -> bool:
8686
)
8787

8888

89-
# Trimmed list of __dunder__ methods generated by using this command:
89+
# Trimmed list of __dunder__ methods generated by using this shell command:
9090
#
91-
# find "$(poetry env info -p)/lib/python3.6" \
92-
# -name '*.py' \
93-
# -exec egrep \
94-
# -oh '__[A-Za-z_][A-Za-z_0-9]*__' '{}' \; \
95-
# | sort | uniq
91+
# curl https://raw.githubusercontent.com/python/cpython/main/Doc/reference/datamodel.rst \
92+
# | egrep -oh '__[a-z_][A-Za-z_0-9]*__' \
93+
# | sort \
94+
# | uniq
95+
#
96+
# Running the above command will yield a list of dunders from the Python 'Data Model'
97+
# documentation page, but many of those matches will be false positives. Many hits
98+
# are object properties (such as __doc__ and __name__) and others are not dunders
99+
# at all (such as the unfortunately named __future__ module).
100+
#
101+
# Unfortunately we can't introspect this list from Python itself, so it's going to be
102+
# a moving target as the data model changes over time.
103+
#
104+
# Note that some Python standard library modules and packages define their own dunder
105+
# methods which are not documented in the data model documentation which are included
106+
# in separate sets below.
96107
OBJECT_DUNDER_METHODS = frozenset(
97108
{
98109
"__abs__",
99110
"__add__",
100111
"__aenter__",
101112
"__aexit__",
102113
"__aiter__",
114+
"__and__",
115+
"__anext__",
103116
"__await__",
117+
"__bool__",
118+
"__buffer__",
104119
"__bytes__",
105120
"__call__",
121+
"__ceil__",
122+
"__class_getitem__",
106123
"__complex__",
107124
"__contains__",
108125
"__del__",
109126
"__delattr__",
127+
"__delete__",
110128
"__delitem__",
111-
"__delslice__",
112129
"__dict__",
113130
"__dir__",
114-
"__div__",
115131
"__divmod__",
116132
"__enter__",
117133
"__eq__",
118134
"__exit__",
119135
"__float__",
136+
"__floor__",
120137
"__floordiv__",
138+
"__format__",
121139
"__ge__",
140+
"__get__",
122141
"__getattr__",
123142
"__getattribute__",
124143
"__getitem__",
125-
"__getslice__",
126-
"__getstate__",
127144
"__gt__",
128145
"__hash__",
129146
"__iadd__",
130147
"__iand__",
131-
"__idiv__",
132148
"__ifloordiv__",
133149
"__ilshift__",
134150
"__imatmul__",
135151
"__imod__",
136152
"__imul__",
153+
"__index__",
137154
"__init__",
155+
"__init_subclass__",
138156
"__instancecheck__",
139157
"__int__",
140158
"__invert__",
141159
"__ior__",
142160
"__ipow__",
161+
"__irshift__",
143162
"__isub__",
144163
"__iter__",
145164
"__itruediv__",
146165
"__ixor__",
147166
"__le__",
148167
"__len__",
168+
"__length_hint__",
149169
"__lshift__",
170+
"__lt__",
171+
"__match_args__",
150172
"__matmul__",
173+
"__missing__",
151174
"__mod__",
175+
"__mro_entries__",
152176
"__mul__",
153177
"__ne__",
154178
"__neg__",
155-
"__next__",
156179
"__new__",
157-
"__not__",
180+
"__next__",
181+
"__or__",
158182
"__pos__",
159183
"__pow__",
184+
"__prepare__",
160185
"__radd__",
161186
"__rand__",
162-
"__rcmp__",
163-
"__rdiv__",
164187
"__rdivmod__",
165-
"__reduce__",
166-
"__reduce_ex__",
188+
"__release_buffer__",
167189
"__repr__",
190+
"__reversed__",
168191
"__rfloordiv__",
169192
"__rlshift__",
170193
"__rmatmul__",
171194
"__rmod__",
172195
"__rmul__",
173-
"__rne__",
174196
"__ror__",
175197
"__round__",
176198
"__rpow__",
@@ -179,17 +201,32 @@ def is_abstract(tp: Type) -> bool:
179201
"__rsub__",
180202
"__rtruediv__",
181203
"__rxor__",
204+
"__set__",
205+
"__set_name__",
182206
"__setattr__",
183207
"__setitem__",
184-
"__setslice__",
185-
"__setstate__",
208+
"__sizeof__",
209+
"__slots__",
186210
"__str__",
187211
"__sub__",
188212
"__subclasscheck__",
189-
"__subclasshook__",
190213
"__truediv__",
214+
"__trunc__",
191215
"__xor__",
192216
}
217+
|
218+
# Support for ABCs
219+
{"__subclasshook__"}
220+
|
221+
# Support for pickling
222+
{
223+
"__getnewargs_ex__",
224+
"__getnewargs__",
225+
"__getstate__",
226+
"__reduce__",
227+
"__reduce_ex__",
228+
"__setstate__",
229+
}
193230
)
194231

195232

0 commit comments

Comments
 (0)