Skip to content

Commit 768e578

Browse files
committed
Add clarification about from_string tests limitations
`test_to_and_from_string()` is **extremely** fragile, even the most innocent change can break it, so we add some extra comments to make sure other people don't attempt to change it and avoid them some headaches. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 7fc9844 commit 768e578

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

tests/timeseries/test_quantities.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,9 @@ def test_invalid_multiplications() -> None:
599599
quantity *= 200.0 # type: ignore
600600

601601

602+
# We can't use _QUANTITY_TYPES here, because it will break the tests, as hypothesis
603+
# will generate more values, some of which are unsupported by the quantities. See the
604+
# test comment for more details.
602605
@pytest.mark.parametrize("quantity_type", [Power, Voltage, Current, Energy, Frequency])
603606
@pytest.mark.parametrize("exponent", [0, 3, 6, 9])
604607
@hypothesis.settings(
@@ -623,8 +626,14 @@ def test_to_and_from_string(
623626
generation and regenerate it with the more appropriate unit and precision.
624627
"""
625628
quantity = quantity_type.__new__(quantity_type)
626-
# pylint: disable=protected-access
627-
quantity._base_value = value * 10**exponent
629+
quantity._base_value = value * 10**exponent # pylint: disable=protected-access
630+
# The above should be replaced with:
631+
# quantity = quantity_type._new( # pylint: disable=protected-access
632+
# value, exponent=10**exponent
633+
# )
634+
# But we can't do that now, because, you guessed it, it will also break the tests
635+
# (_new() will use 10.0**exponent instead of 10**exponent, which seems to have some
636+
# effect on the tests.
628637
quantity_str = f"{quantity:.{exponent}}"
629638
from_string = quantity_type.from_string(quantity_str)
630639
try:

0 commit comments

Comments
 (0)