ABCMeta
adds __weakref__
but not to __slots__
#128866
Labels
extension-modules
C modules in the Modules dir
stdlib
Python modules in the Lib dir
type-bug
An unexpected behavior, bug, or error
Bug report
Bug description:
I stumbled upon the following error (code snippet from python 3.12.3 with gcc 13.2.0, but I also checked with 3.11 and 3.13):
This also happens when inheriting from
abc.ABC
. This surprised me since the documentation does not mention__weakref__
being added.Adding to my confusion, the common abstract base classes from
collections.abc
do not have this behavior (I only checkedMapping
,Sequence
andCollection
), i.e.,__weakref__
attribute is not added to the class.Another peculiarity here is that while a class with
metaclass =ABCMeta
is automatically weakref-able, and has a__weakref__
attribute, the__weakref__
cannot be found in its__slots__
, and in fact there might not even be a__slots__
attribute anywhere in the MRO!At the very least this warrant adding documentation to
abc.ABC
andabc.ABCMeta
. A better fix IMO is adding__weakref__
to__slots__
.Another solution (which I like the most) is not adding
__weakref__
to abstract classes to begin with, but I suspect this might break some code.CPython versions tested on:
3.11, 3.12, 3.13
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: