Python Property in Abstract Base can become method upon inherit

Viewed 26
from abc import ABC, abstractmethod

class BasketballPlayer(ABC):

    move1 = 'pass'
    move2 = 'dribble'
    move3 = 'shoot'

    @property
    @abstractmethod
    def name(self):
        pass



class Player2(BasketballPlayer):
    def name(self):
        pass


y = Player2()

I expect an error on the line y = Player2() because the name is suddenly declared as a method instead of a property. Is this a bug? or something I did wrong?

1 Answers

Equally Python won't care if you implement @abstractmethod as class variable. I am not sure what is logic behind this, perhaps good question to ask on: https://github.com/python/cpython/issues

from abc import ABC, abstractmethod, abstractproperty

class MyAbstractClass(ABC):
    @property
    @abstractmethod
    def name(self):
        pass

    @abstractmethod
    def run(self):
        pass

class Child(MyAbstractClass):
    name = "foo"
    run = "foo"


c = Child()
print(c.name)

I found similar question with answer here: enforcement for abstract properties in python3

Related