Skip to content

Commit

Permalink
test(models): support float-style formatting for Fraction instances u…
Browse files Browse the repository at this point in the history
…nder Python 3.12 (python/cpython#100161)
  • Loading branch information
jakob-keller committed Aug 6, 2023
1 parent 1da3a45 commit 5dcd849
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions tests/models/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dataclasses
import decimal
import fractions
import sys

import pytest

Expand Down Expand Up @@ -128,13 +129,23 @@ def test_unit_symbol(unit):
)
def test_format(measurement, format_spec):
if format_spec and isinstance(measurement.magnitude, fractions.Fraction):
with pytest.raises(TypeError):
format(measurement, format_spec)
else:
formatted = format(measurement.magnitude, format_spec)
if measurement.prefix.symbol or measurement._unit_symbol:
formatted += f" {measurement.prefix.symbol}{measurement._unit_symbol}"
assert format(measurement, format_spec) == formatted
if sys.version_info < (3, 12):
with pytest.raises(TypeError):
format(measurement, format_spec)
return

if format_spec == "n":
with pytest.raises(
ValueError,
match="Invalid format specifier 'n' for object of type 'Fraction'",
):
format(measurement, format_spec)
return

formatted = format(measurement.magnitude, format_spec)
if measurement.prefix.symbol or measurement._unit_symbol:
formatted += f" {measurement.prefix.symbol}{measurement._unit_symbol}"
assert format(measurement, format_spec) == formatted


def test_str(measurement):
Expand Down

0 comments on commit 5dcd849

Please sign in to comment.