I have a dataclass with an attribute 'open' of type float. I added a property setter which shall throw a ValueError in case it gets set to a negative value:
@open.setter
def open(self, value: float) -> None:
if value < 0.0:
raise ValueError("Open value must be positive.")
self.open = value
The error I am getting is:
File "<string>", line 4, in __init__
File "/home/PATH/file.py", line 29, in open
if value < 0.0:
^^^^^^^^^^^
TypeError: '<' not supported between instances of 'property' and 'float'
I don't really get what I am doing wrong here. Thanks for any advice!
The OP was not a MRE.
The code below is. And it demonstrates a solution to the problem.
The key is that you want
self.opento be a setter method, so the (private) value belongs in a different object namedself._open.There's no conflict between these names and the popular builtin open(). But as a rule of thumb, it's typically a good idea to avoid such names. We steer clear of shadowing and human confusion.