Skip to content

Commit

Permalink
Correct ValidatedProperty initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Apr 23, 2024
1 parent 7515841 commit f479c9e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
11 changes: 5 additions & 6 deletions src/property_utils/properties/validated_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def __init__(self, value: float, unit: Optional[UnitDescriptor] = None) -> None:
f"cannot create {self.__class__.__name__} with {unit} units; "
f"expected {self.generic_unit_descriptor} units. "
)
self.validate_value(value)

self.__post_init__()

def __post_init__(self) -> None:
self.validate_value(self.value)

@abstractmethod
def validate_value(self, value: float) -> None:
Expand All @@ -63,8 +67,3 @@ def validate_value(self, value: float) -> None:

def __repr__(self) -> str:
return f"<{self.__class__.__name__}: {self.value} {self.unit}>"

def __setattr__(self, __name: str, __value: Any) -> None:
if __name == "value":
self.validate_value(__value)
super().__setattr__(__name, __value)
16 changes: 0 additions & 16 deletions src/property_utils/tests/properties/test_validated_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,3 @@ def test_with_valid_value(self):
@args({"value": 5, "unit": Unit1.a})
def test_with_unit(self):
self.assert_result("5 a")


@add_to(validated_property_test_suite)
class TestValidatedPropertySetValue(TestValidatedProperty):
def subject(self, value):
prop = BiggerThan5Prop(10, Unit1.A)
prop.value = value
return prop

@args({"value": 11})
def test_with_valid_value(self):
self.assert_result("11 A")

@args({"value": 2})
def test_with_invalid_value(self):
self.assert_validation_error()

0 comments on commit f479c9e

Please sign in to comment.