Skip to content

Commit bf835be

Browse files
committed
Correct ValidatedProperty initializer
1 parent 7515841 commit bf835be

File tree

2 files changed

+6
-23
lines changed

2 files changed

+6
-23
lines changed

src/property_utils/properties/validated_property.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, ClassVar, Optional
2+
from typing import ClassVar, Optional
33
from abc import abstractmethod
44

55
from property_utils.units.descriptors import UnitDescriptor, GenericUnitDescriptor
@@ -50,7 +50,11 @@ def __init__(self, value: float, unit: Optional[UnitDescriptor] = None) -> None:
5050
f"cannot create {self.__class__.__name__} with {unit} units; "
5151
f"expected {self.generic_unit_descriptor} units. "
5252
)
53-
self.validate_value(value)
53+
54+
self.__post_init__()
55+
56+
def __post_init__(self) -> None:
57+
self.validate_value(self.value)
5458

5559
@abstractmethod
5660
def validate_value(self, value: float) -> None:
@@ -63,8 +67,3 @@ def validate_value(self, value: float) -> None:
6367

6468
def __repr__(self) -> str:
6569
return f"<{self.__class__.__name__}: {self.value} {self.unit}>"
66-
67-
def __setattr__(self, __name: str, __value: Any) -> None:
68-
if __name == "value":
69-
self.validate_value(__value)
70-
super().__setattr__(__name, __value)

src/property_utils/tests/properties/test_validated_property.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,3 @@ def test_with_valid_value(self):
8787
@args({"value": 5, "unit": Unit1.a})
8888
def test_with_unit(self):
8989
self.assert_result("5 a")
90-
91-
92-
@add_to(validated_property_test_suite)
93-
class TestValidatedPropertySetValue(TestValidatedProperty):
94-
def subject(self, value):
95-
prop = BiggerThan5Prop(10, Unit1.A)
96-
prop.value = value
97-
return prop
98-
99-
@args({"value": 11})
100-
def test_with_valid_value(self):
101-
self.assert_result("11 A")
102-
103-
@args({"value": 2})
104-
def test_with_invalid_value(self):
105-
self.assert_validation_error()

0 commit comments

Comments
 (0)