This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2022年04月06日 08:41 by Germandrummer92, last changed 2022年04月11日 14:59 by admin.
| Messages (5) | |||
|---|---|---|---|
| msg416846 - (view) | Author: Daniel Draper (Germandrummer92) | Date: 2022年04月06日 08:41 | |
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: ```python from typing import Protocol from dataclasses import dataclass class SomeProtocol(Protocol): @property def some_value(self) -> str: ... @dataclass class SomeDataclasss(SomeProtocol): some_value: str if __name__ == '__main__': a = SomeDataclasss(some_value="value") # this crashes with AttributeError: can't set attribute 'some_value' ``` 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 ```python a: SomeProtocol = SomeDataclass() ``` |
|||
| msg416853 - (view) | Author: Eric V. Smith (eric.smith) * (Python committer) | Date: 2022年04月06日 10:36 | |
Here's the error without dataclasses: ------ from typing import Protocol class SomeProtocol(Protocol): @property def some_value(self) -> str: ... class SomeClass(SomeProtocol): def __init__(self, some_value): self.some_value = some_value if __name__ == '__main__': a = SomeClass(some_value="value") ------ 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 And here it is without Protocol: -------- class SomeProperty: @property def some_value(self) -> str: ... class SomeClass(SomeProperty): def __init__(self, some_value): self.some_value = some_value if __name__ == '__main__': a = SomeClass(some_value="value") -------- 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 |
|||
| msg416880 - (view) | Author: Guido van Rossum (gvanrossum) * (Python committer) | Date: 2022年04月06日 15:43 | |
So is the conclusion that this should be closed as "not a bug"? |
|||
| msg416881 - (view) | Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) | Date: 2022年04月06日 15:46 | |
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. |
|||
| msg416882 - (view) | Author: Eric V. Smith (eric.smith) * (Python committer) | Date: 2022年04月06日 15:54 | |
What would dataclasses do that's different from a regular class? |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:59:58 | admin | set | github: 91393 |
| 2022年04月06日 15:54:34 | eric.smith | set | messages: + msg416882 |
| 2022年04月06日 15:46:01 | JelleZijlstra | set | messages: + msg416881 |
| 2022年04月06日 15:43:49 | gvanrossum | set | messages: + msg416880 |
| 2022年04月06日 10:42:11 | eric.smith | set | title: Inheritance from Protocol with property in class makes them non-instantiatable -> Inheritance from base class with property in class makes them non-instantiatable |
| 2022年04月06日 10:36:45 | eric.smith | set | messages:
+ msg416853 title: Inheritance from Protocol with property in dataclass makes them non-instantiatable -> Inheritance from Protocol with property in class makes them non-instantiatable |
| 2022年04月06日 08:52:21 | AlexWaygood | set | nosy:
+ gvanrossum, eric.smith, JelleZijlstra, kj, AlexWaygood |
| 2022年04月06日 08:41:16 | Germandrummer92 | create | |