python-attrs / python-attrs/attrs

Abstract property can't be found by mixins when property is a `attr.ib`

Open
#1,000 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

Same context here

The following runs fine:

from abc import ABC, abstractmethod
import attr

class A(ABC):
    @abstractmethod
    def prop(self):
        pass

    def foo(self):
        print(self.prop())

class B:
    def prop(self):
        return 5

class C(B, A):
    pass

C()

Here, C has a function prop inherited from B which is found first in the MRO order so A wouldn't complain about its existence. I tried this with a dataclass as well:

from abc import ABC, abstractmethod
from dataclasses import dataclass, field

class A(ABC):
    @property
    @abstractmethod
    def prop(self):
        pass

    def foo(self):
        print(self.prop())

@dataclass
class B:
    prop: int = field(default=5)

class C(B, A):
    pass

C()

However, this doesn't work with attr:

from abc import ABC, abstractmethod
import attr

@attr.s
class A(ABC):
    @property
    @abstractmethod
    def prop(self):
        pass

    def foo(self):
        print(self.prop())

@attr.s
class B:
    prop: int = attr.ib(default=5)

@attr.s
class C(B, A):
    pass

C()

Running it:

❯ python test.py                                                                                  
Traceback (most recent call last):
  File "test_mro_abstract_property.py", line 22, in <module>
    C()
TypeError: Can't instantiate abstract class C with abstract methods prop

Python version: 3.8.3, attrs version 22.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproducer using @attr.s, attr.ib, ABC, and abstract property inheritance. Trace why C() remains abstract when B provides prop, and verify that the corrected behavior allows C() to instantiate and resolve prop to 5 without breaking the dataclass comparison case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.