[issue47237] Inheritance from Protocol with property in dataclass makes them non-instantiatable

2022-04-06 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +AlexWaygood, JelleZijlstra, eric.smith, gvanrossum, kj

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47237] Inheritance from Protocol with property in dataclass makes them non-instantiatable

2022-04-06 Thread Daniel Draper


New submission from Daniel Draper :

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()
```

--
components: Library (Lib)
messages: 416846
nosy: Germandrummer92
priority: normal
severity: normal
status: open
title: Inheritance from Protocol with property in dataclass makes them 
non-instantiatable
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com