Skip to content
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

Combination of @classmethod @property and @abc.abstractmethod #93167

Closed
jbaudisch opened this issue May 24, 2022 · 2 comments
Closed

Combination of @classmethod @property and @abc.abstractmethod #93167

jbaudisch opened this issue May 24, 2022 · 2 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@jbaudisch
Copy link

Bug report

import abc

class A(abc.ABC):
    @classmethod
    @property
    @abc.abstractmethod
    def type(cls):
        raise NotImplementedError
    
class B(A, metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def other(self):
        raise NotImplementedError
    
class C(B):
    @classmethod
    @property
    def type(cls):
        return 'C'

    def other(self):
        print('other')

c = C()
c.other()

This script is failing with an NotImplementedError:

Traceback (most recent call last):
  File "C:\Users\Justin Baudisch\Desktop\test.py", line 10, in <module>
    class B(A, metaclass=abc.ABCMeta):
  File "C:\Program Files\Python310\lib\abc.py", line 107, in __new__
    _abc_init(cls)
  File "C:\Users\Justin Baudisch\Desktop\test.py", line 8, in type
    raise NotImplementedError
NotImplementedError

So the script executes the "type"-property. But why? This is not the expected behaviour (?).
If I remove the @property decorator and leave it as a method, it works fine. Same when I remove the @classmethod decorator.

Your environment

  • CPython versions tested on: Python 3.10
  • Operating system and architecture: Windows 11
@jbaudisch jbaudisch added the type-bug An unexpected behavior, bug, or error label May 24, 2022
@rhettinger rhettinger self-assigned this May 25, 2022
@alkasm
Copy link

alkasm commented Aug 8, 2022

Note: if the order of property & classmethod are flipped, the script behaves as expected. See also: #89519

@jbaudisch
Copy link
Author

Note: if the order of property & classmethod are flipped, the script behaves as expected. See also: #89519

Yes, this error is obsolete in Python 3.11, because "class methods can no longer wrap other descriptors such as property".
https://docs.python.org/3.11/library/functions.html?highlight=classmethod#classmethod

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants