Skip to content

Commit 0ea7bb9

Browse files
committed
Adds default_units to Property
1 parent 04540a6 commit 0ea7bb9

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/property_utils/properties/property.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from dataclasses import dataclass, replace
6-
from typing import Type, Optional
6+
from typing import Type, Optional, ClassVar
77
from math import isclose
88

99
try:
@@ -63,17 +63,23 @@ class Property:
6363
value: float
6464
unit: UnitDescriptor
6565
unit_converter: Optional[Type[UnitConverter]] = None
66+
default_units: ClassVar[Optional[UnitDescriptor]] = None
6667

67-
def __init__(self, value: float, unit: UnitDescriptor) -> None:
68+
def __init__(self, value: float, unit: Optional[UnitDescriptor] = None) -> None:
6869
if not isinstance(value, (float, int)):
6970
raise PropertyValidationError(
7071
f"cannot create Property; invalid 'value': {value}; expected numeric. "
7172
)
73+
74+
if unit is None and self.default_units is not None:
75+
unit = self.default_units
76+
7277
if not isinstance(unit, (MeasurementUnit, Dimension, CompositeDimension)):
7378
raise PropertyValidationError(
7479
f"cannot create Property; invalid 'unit': {unit}. Expected an instance"
7580
" of one of: MeasurementUnit, Dimension, CompositeDimension. "
7681
)
82+
7783
self.value = value
7884
self.unit = unit
7985

src/property_utils/tests/properties/test_property.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from unittest_extensions import args, TestCase
44

55
from property_utils.properties.property import Property, p
6-
from property_utils.units.descriptors import CompositeDimension
76
from property_utils.units.units import NonDimensionalUnit, PressureUnit
87
from property_utils.exceptions.properties.property import (
98
PropertyExponentError,
@@ -14,8 +13,9 @@
1413
Unit3,
1514
Unit4,
1615
Unit6,
17-
Unit7,
1816
Unit8,
17+
PropUnit1,
18+
PropUnit2,
1919
generic_dimension_1,
2020
generic_composite_dimension,
2121
)
@@ -67,6 +67,30 @@ def test_does_not_initialize_converter(self):
6767
self.assertIsNone(self.result().unit_converter)
6868

6969

70+
@add_to(property_test_suite)
71+
class TestPropertyDefaultUnits(TestProperty):
72+
def subject(self, **kwargs):
73+
return PropUnit1(**kwargs)
74+
75+
@args({"value": 1, "unit": Unit1.A})
76+
def test_with_units(self):
77+
self.assert_result("1 A")
78+
79+
@args({"value": 1})
80+
def test_with_no_units(self):
81+
self.assert_result("1 a")
82+
83+
84+
@add_to(property_test_suite)
85+
class TestPropertyWithoutDefaultUnits(TestProperty):
86+
def subject(self, value):
87+
return PropUnit2(value)
88+
89+
@args({"value": 10})
90+
def test_raises_validation_error(self):
91+
self.assert_validation_error()
92+
93+
7094
@add_to(property_test_suite)
7195
class TestPropertyConstructor(TestCase):
7296
def subject(self, **kwargs):

0 commit comments

Comments
 (0)