From f3cd227b6796fbcc4f97aae7cc0c254f1bece690 Mon Sep 17 00:00:00 2001 From: Ken Kroenlein <51962276+kroenlein@users.noreply.github.com> Date: Thu, 15 Aug 2024 08:30:04 -0600 Subject: [PATCH] Update compatibility to include latest Pint (#222) --- gemd/__version__.py | 2 +- gemd/units/citrine_en.txt | 13 +++++++++---- gemd/units/impl.py | 20 ++++++++++++++++++-- setup.py | 4 ++-- test_requirements.txt | 2 +- tests/units/test_parser.py | 10 +++++++++- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/gemd/__version__.py b/gemd/__version__.py index c377db3d..254c1211 100644 --- a/gemd/__version__.py +++ b/gemd/__version__.py @@ -1 +1 @@ -__version__ = "2.1.8" +__version__ = "2.1.9" diff --git a/gemd/units/citrine_en.txt b/gemd/units/citrine_en.txt index 31feb966..f43c6e3b 100644 --- a/gemd/units/citrine_en.txt +++ b/gemd/units/citrine_en.txt @@ -1,6 +1,6 @@ # Citrine customized units input file for Pint, based explicitly on the Pint defaults as -# included release 0.23 (https://github.com/hgrecco/pint/releases/tag/0.23). The Pint Authors hold -# copyright and are documented in https://github.com/hgrecco/pint/blob/0.23/AUTHORS. +# included release 0.24.3 (https://github.com/hgrecco/pint/releases/tag/0.24.3). The Pint Authors hold +# copyright and are documented in https://github.com/hgrecco/pint/blob/0.24.3/AUTHORS. # # The original copyright statement for the constants file reads: # @@ -116,7 +116,7 @@ pico- = 1e-12 = p- nano- = 1e-9 = n- # The micro (U+00B5) and Greek mu (U+03BC) are both valid prefixes, # and they often use the same glyph. -micro- = 1e-6 = µ- = μ- = u- +micro- = 1e-6 = µ- = μ- = u- = mu- = mc- milli- = 1e-3 = m- centi- = 1e-2 = c- deci- = 1e-1 = d- @@ -269,7 +269,7 @@ hectare = 100 * are = ha = Hectare # Volume [volume] = [length] ** 3 -liter = decimeter ** 3 = l = L = litre = Liter = Litre +liter = decimeter ** 3 = l = L = ℓ = litre = Liter = Litre cubic_centimeter = centimeter ** 3 = cc = Cubic_Centimeter lambda = microliter = λ = Lambda stere = meter ** 3 = _ = Stere @@ -543,12 +543,17 @@ buckingham = debye * angstrom = Buckingham bohr_magneton = e * hbar / (2 * m_e) = µ_B = mu_B nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N +# Refractive index +[refractive_index] = [] +refractive_index_unit = [] = RIU + # Logaritmic Unit Definition # Unit = scale; logbase; logfactor # x_dB = [logfactor] * log( x_lin / [scale] ) / log( [logbase] ) # Logaritmic Units of dimensionless quantity: [ https://en.wikipedia.org/wiki/Level_(logarithmic_quantity) ] +decibelwatt = watt; logbase: 10; logfactor: 10 = dBW decibelmilliwatt = 1e-3 watt; logbase: 10; logfactor: 10 = dBm decibelmicrowatt = 1e-6 watt; logbase: 10; logfactor: 10 = dBu diff --git a/gemd/units/impl.py b/gemd/units/impl.py index ee3f378e..fcc1ec0c 100644 --- a/gemd/units/impl.py +++ b/gemd/units/impl.py @@ -305,8 +305,24 @@ def _format_clean(unit, registry, **options): Responsibility for this piece of clean-up has been shifted to a custom class. """ - from pint.formatting import _FORMATTERS - return _FORMATTERS["D"](unit, registry, **options) + try: # Informal route changed in 0.22 + from pint.formatting import _FORMATTERS + formatter = _FORMATTERS["D"] + except ImportError: # pragma: no cover + from pint import Unit + formatter_obj = registry.formatter._formatters["D"] + + def _surrogate_formatter(unit, registry, **options): + try: + parsed = Unit(unit) + return formatter_obj.format_unit(parsed) + except ValueError: + parsed = Unit(unit) + return formatter_obj.format_quantity(unit) + + formatter = _surrogate_formatter + + return formatter(unit, registry, **options) @functools.lru_cache(maxsize=1024) diff --git a/setup.py b/setup.py index 9602ea6a..81d26e45 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ 'tests.units': ['test_units.txt'] }, install_requires=[ - "pint>=0.21,<0.24", + "pint>=0.21,<0.25,!=0.22,!=0.23", # pint 0.22,0.23 have a bad interaction w/ numpy >= 2 "deprecation>=2.1.0,<3", "typing_extensions>=4.8,<5", "importlib-resources>=5.3,<7" @@ -52,7 +52,7 @@ "pandas>=2.0.3,<3" ], "tests.entity.bounds": [ - "numpy>=1.24.4,<2", + "numpy>=1.24.4,<3", "pandas>=2.0.3,<3" ] }, diff --git a/test_requirements.txt b/test_requirements.txt index cd3f154c..ef3cd8bc 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -2,5 +2,5 @@ flake8==7.0.0 flake8-docstrings==1.7.0 pytest==8.0.0 pytest-cov==4.1.0 -pandas>=2.0.3,<2.2.0 +pandas>=2.0.3,<2.2.0 # includes a compatible version of numpy derp==0.1.1 diff --git a/tests/units/test_parser.py b/tests/units/test_parser.py index 7c2f86e4..3706399d 100644 --- a/tests/units/test_parser.py +++ b/tests/units/test_parser.py @@ -222,4 +222,12 @@ def test_deprecation(): megapascals = parse_units("MPa", return_unit=True) with pytest.warns(DeprecatedWarning): stringified = f"{megapascals:clean}" - assert megapascals == parse_units(stringified, return_unit=True) + assert megapascals == parse_units(stringified, return_unit=False) + + from pint import Quantity + with pytest.warns(DeprecatedWarning): + assert f"{Quantity('5 MPa'):clean}" == f"5 {stringified}" + + from pint import Unit + with pytest.warns(DeprecatedWarning): + assert f"{Unit('MPa'):clean}" == stringified