-
-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to create an abstract classmethod #50117
Comments
Is there a way to define an abstract classmethod? The two obvious ways Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import abc
>>> class C(metaclass=abc.ABCMeta):
... @abc.abstractmethod
... @classmethod
... def f(cls): print(42)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in C
File "/usr/lib/python3.0/abc.py", line 24, in abstractmethod
funcobj.__isabstractmethod__ = True
AttributeError: 'classmethod' object has no attribute '__isabstractmethod__'
>>> class C(metaclass=abc.ABCMeta):
... @classmethod
... @abc.abstractmethod
... def f(cls): print(42)
...
>>> class D(C): pass
...
>>> D.f()
42 |
Please ask questions like this first on python-list or the c.l.p or |
@abstractmethod
@classmethod
def ...
doesn't work because classmethod objects doesn't have a __dict__, so setting arbitrary attributes don't work, and abstractmethod tries to set the __isabstractmethod__ atribute to True. The other order: The situation is the same with staticmethod. One possible solution would be adding a descriptor to classmethod (and staticmethod), with the name "__isabstractmethod__", which on __get__ would check its underlying callable for this attribute, and on __set__ would set this attribute on that callable. I think this way both order should work. |
Here is a patch, which adds a descriptor to classmethod and staticmethod. __get__(self, inst, owner): __set__(self, inst, value): |
The patch doesn't check that instantiating these methods work at all. |
If I understand correctly, some tests are needed for the instantiation of classes with abstract static/classmethods. I added them in issue5867a.diff. |
Thank you. I'm not an ABC expert but it looks ok. Guido, what do you think? |
As you figured out it is not yet supported. I object to making changes to the classmethod implementation. I expect the best thing to do is to add a new @abc.abstractclassmethod decorator defined in pure Python (maybe the definition of abstractproperty provides a hint on how to do this). You may want to define @abc.abstractstaticmethod as well. |
I'm attaching a new patch adding the abc.abstractclassmethod and abc.abstractstaticmethod decorators. |
The patch looks fine code-wise, but it also needs a doc addition in Doc/library/abc.rst. |
I'm attaching a new patch containing also some documentation for the two new decorators. The doc is rather terse, and english is not my first language, so please let me know if some corrections are needed. |
Looks good. |
Applied in r84124. Thanks for the patch. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: