[Python-Dev] abstractmethod doesn't work in classes

Eugene Toder eltoder at gmail.com
Fri Apr 8 03:43:07 CEST 2011


Hello,

I've found that abstractmethod and similar decorators "don't work" in
classes, inherited from built-in types other than object.
For example:

>>> import abc
>>> class MyBase(metaclass=abc.ABCMeta):
	@abc.abstractmethod
	def foo(): pass

>>> MyBase()
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    MyBase()
TypeError: Can't instantiate abstract class MyBase with abstract methods foo

So far so good, but:

>>> class MyList(list, MyBase):
	pass

>>> MyList()
[]
>>> MyList.__abstractmethods__
frozenset({'foo'})

This is unexpected, since MyList still doesn't implement foo.
Should this be considered a bug? I don't see this in documentation.
The underlying reason is that __abstractmethods__ is checked in
object_new, but built-in types typically call tp_alloc directly, thus
skipping the check.

Eugene


More information about the Python-Dev mailing list