Eric V. Smith <e...@trueblade.com> added the comment:

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

----------
title: Inheritance from Protocol with property in dataclass makes them 
non-instantiatable -> Inheritance from Protocol with property in class makes 
them non-instantiatable

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue47237>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to