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

Combining @staticmethod and @abc.abstractmethod fails at runtime #2651

Open
DanielHerding opened this Issue Oct 11, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@DanielHerding

DanielHerding commented Oct 11, 2018

I'm trying to convert one of my Python modules using Cython. Cython successfully generates a .pyd file, but when I try to import it, it fails:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
hello 5**
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 10, in init example
    @abc.abstractmethod
  File "C:\Program Files\Python3\lib\abc.py", line 23, in abstractmethod
    funcobj.__isabstractmethod__ = True
AttributeError: attribute '__isabstractmethod__' of 'staticmethod' objects is not writable

The same error occurs on a different Python version on Linux with a Cython-generated .so file:

Python 3.4.2 (default, Sep 25 2018, 22:02:39)
[GCC 4.9.2] on linux
>>> import example
hello 5**
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 10, in init example
    @abc.abstractmethod
  File "/usr/lib/python3.4/abc.py", line 25, in abstractmethod
    funcobj.__isabstractmethod__ = True
AttributeError: attribute '__isabstractmethod__' of 'staticmethod' objects is not writable

My module contains a method which is both abstract and static:

#!python
#cython: language_level=3

import abc

print('hello', 5, end='**\n') # Python3 syntax, breaks on Python2

class c(abc.ABC):
    @staticmethod
    @abc.abstractmethod
    def foo():
        raise NotImplementedError

According to https://docs.python.org/3/library/abc.html#abc.abstractmethod this is legal, as long as the @staticmethod decorator comes first. If I remove the @abstractmethod annotation, I can cythonize and import the module. It also works if i use @abstractstaticmethod, but that decorator is deprecated in abc.

Hint: In pure Python, a "'__isabstractmethod__' of 'staticmethod' objects is not writable" occurs when placing @abc.abstractmethod before @staticmethod: https://bugs.python.org/issue16267
Maybe Cython changes the decorator order?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment