You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inheritance from base class with property in class makes them non-instantiatable #91393
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:
assignee=Noneclosed_at=Nonecreated_at=<Date2022-04-06.08:41:16.184>labels= ['type-bug', 'library', '3.10']
title='Inheritance from base class with property in class makes them non-instantiatable'updated_at=<Date2022-04-06.15:54:34.248>user='https://github.com/Germandrummer92'
When removing the explicit inheritiance mypy also correctly typechecks the dataclass implementation when doing something like, only the explicit inheritance seems to fail in python
Traceback (most recent call last):
File "foo.py", line 12, in <module>
a = SomeClass(some_value="value")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "foo.py", line 9, in __init__self.some_value = some_value
^^^^^^^^^^^^^^^AttributeError: property 'some_value' of 'SomeClass' object has no setter
Traceback (most recent call last):
File "foo.py", line 10, in <module>
a = SomeClass(some_value="value")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "foo.py", line 7, in __init__self.some_value = some_value
^^^^^^^^^^^^^^^AttributeError: property 'some_value' of 'SomeClass' object has no setter
changed the title [-]Inheritance from Protocol with property in dataclass makes them non-instantiatable[/-][+]Inheritance from Protocol with property in class makes them non-instantiatable[/+]on Apr 6, 2022
changed the title [-]Inheritance from Protocol with property in dataclass makes them non-instantiatable[/-][+]Inheritance from Protocol with property in class makes them non-instantiatable[/+]on Apr 6, 2022
changed the title [-]Inheritance from Protocol with property in class makes them non-instantiatable[/-][+]Inheritance from base class with property in class makes them non-instantiatable[/+]on Apr 6, 2022
changed the title [-]Inheritance from Protocol with property in class makes them non-instantiatable[/-][+]Inheritance from base class with property in class makes them non-instantiatable[/+]on Apr 6, 2022
I think the behavior with regular classes is expected (that's just how inheritance works), but a case could be made that dataclasses should handle this case specially.
$ python test.py
Traceback (most recent call last):
File "/private/tmp/test.py", line 24, in <module>
y: Proto = Explicit(42)
TypeError: Can't instantiate abstract class Explicit with abstract method prop
$ mypy test.py
Success: no issues found in 1 source file
I see this as a perhaps surprising result of how all these things work, not as a bug. Note that Protocol isn't needed, the same error is given if Proto doesn't inherit from Protocol. A workaround would be to set a default value in the Explicit subclass (prop: int = 0). But mostly my view is "don't do that".
Activity
Germandrummer92 commentedon Apr 6, 2022
Hi,
According to https://peps.python.org/pep-0544/#explicitly-declaring-implementation it should be possible to explicitly inherit from Protocols. This however breaks the dataclass constructor when using the @Property decorator in the protocol, see this example:
The pattern of @Property in the protocol is one taken from the mypy docs (see https://mypy.readthedocs.io/en/stable/protocols.html#recursive-protocols for example).
When removing the explicit inheritiance mypy also correctly typechecks the dataclass implementation when doing something like, only the explicit inheritance seems to fail in python
ericvsmith commentedon Apr 6, 2022
Here's the error without dataclasses:
------
And here it is without Protocol:
--------
[-]Inheritance from Protocol with property in dataclass makes them non-instantiatable[/-][+]Inheritance from Protocol with property in class makes them non-instantiatable[/+][-]Inheritance from Protocol with property in dataclass makes them non-instantiatable[/-][+]Inheritance from Protocol with property in class makes them non-instantiatable[/+][-]Inheritance from Protocol with property in class makes them non-instantiatable[/-][+]Inheritance from base class with property in class makes them non-instantiatable[/+][-]Inheritance from Protocol with property in class makes them non-instantiatable[/-][+]Inheritance from base class with property in class makes them non-instantiatable[/+]gvanrossum commentedon Apr 6, 2022
So is the conclusion that this should be closed as "not a bug"?
JelleZijlstra commentedon Apr 6, 2022
I think the behavior with regular classes is expected (that's just how inheritance works), but a case could be made that dataclasses should handle this case specially.
ericvsmith commentedon Apr 6, 2022
What would dataclasses do that's different from a regular class?
Kentzo commentedon Oct 22, 2022
With CPython 3.10.7 and mypy 0.982
The results are:
gvanrossum commentedon Oct 22, 2022
I see this as a perhaps surprising result of how all these things work, not as a bug. Note that
Protocolisn't needed, the same error is given ifProtodoesn't inherit fromProtocol. A workaround would be to set a default value in theExplicitsubclass (prop: int = 0). But mostly my view is "don't do that".Kentzo commentedon Oct 22, 2022
Another workaround that removes inheritance but preserves explicit type checking at the place of definition:
vnmabus commentedon Apr 2, 2025
Would this work somehow with the
ReadOnlyextension proposed in PEP 767?JelleZijlstra commentedon Apr 2, 2025
Yes, ReadOnly should fix this.