From 5dcd849d5de727053411397da18a73c524f22091 Mon Sep 17 00:00:00 2001 From: Jakob Keller <57402305+jakob-keller@users.noreply.github.com> Date: Sun, 6 Aug 2023 19:00:31 +0200 Subject: [PATCH] test(models): support float-style formatting for Fraction instances under Python 3.12 (https://github.com/python/cpython/pull/100161) --- tests/models/test_measurement.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/models/test_measurement.py b/tests/models/test_measurement.py index 24fa9c5..bf93239 100644 --- a/tests/models/test_measurement.py +++ b/tests/models/test_measurement.py @@ -3,6 +3,7 @@ import dataclasses import decimal import fractions +import sys import pytest @@ -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):