|
1 | 1 | """Implementation of units.""" |
2 | 2 | from deprecation import deprecated |
3 | 3 | import functools |
4 | | -from importlib_resources import files |
| 4 | +from importlib.resources import files |
5 | 5 | import os |
6 | 6 | from pathlib import Path |
7 | 7 | import re |
8 | 8 | from tempfile import TemporaryDirectory |
9 | 9 | from typing import Union, List, Tuple, Generator, Any |
| 10 | +try: |
| 11 | + from typing import TypeAlias # Python 3.10+ |
| 12 | +except ImportError: # pragma nocover |
| 13 | + from typing_extensions import TypeAlias # Python 3.9 |
10 | 14 |
|
11 | 15 | from pint import UnitRegistry, register_unit_format |
12 | | -try: # Pint 0.23 migrated the location of this method, and augmented it |
13 | | - from pint.pint_eval import tokenizer |
14 | | -except ImportError: # pragma: no cover |
15 | | - from pint.compat import tokenizer |
| 16 | +from pint.pint_eval import tokenizer |
16 | 17 | from tokenize import NAME, NUMBER, OP, ERRORTOKEN, TokenInfo |
17 | 18 | # alias the error that is thrown when units are incompatible |
18 | 19 | # this helps to isolate the dependence on pint |
19 | | -from pint.errors import DimensionalityError as IncompatibleUnitsError # noqa Import |
20 | | -from pint.errors import UndefinedUnitError, DefinitionSyntaxError # noqa Import |
| 20 | +from pint.errors import DimensionalityError as IncompatibleUnitsError |
| 21 | +from pint.errors import UndefinedUnitError, DefinitionSyntaxError |
| 22 | +from pint.registry import GenericUnitRegistry |
21 | 23 |
|
22 | 24 | # Store directories so they don't get auto-cleaned until exit |
23 | 25 | _TEMP_DIRECTORY = TemporaryDirectory() |
@@ -216,43 +218,29 @@ def _unmangle_scaling(input_string: str) -> str: |
216 | 218 | return input_string |
217 | 219 |
|
218 | 220 |
|
219 | | -try: # pragma: no cover |
220 | | - # Pint 0.23 modified the preferred way to derive a custom class |
221 | | - # https://pint.readthedocs.io/en/0.23/advanced/custom-registry-class.html |
222 | | - from pint.registry import GenericUnitRegistry |
223 | | - from typing_extensions import TypeAlias |
| 221 | +# Standard approach to creating a custom registry class: |
| 222 | +# https://pint.readthedocs.io/en/0.23/advanced/custom-registry-class.html |
224 | 223 |
|
225 | | - class _ScaleFactorUnit(UnitRegistry.Unit): |
226 | | - """Child class of Units for generating units w/ clean scaling factors.""" |
| 224 | +class _ScaleFactorUnit(UnitRegistry.Unit): |
| 225 | + """Child class of Units for generating units w/ clean scaling factors.""" |
227 | 226 |
|
228 | | - def __format__(self, format_spec): |
229 | | - result = super().__format__(format_spec) |
230 | | - return _unmangle_scaling(result) |
| 227 | + def __format__(self, format_spec): |
| 228 | + result = super().__format__(format_spec) |
| 229 | + return _unmangle_scaling(result) |
231 | 230 |
|
232 | | - class _ScaleFactorQuantity(UnitRegistry.Quantity): |
233 | | - """Child class of Quantity for generating units w/ clean scaling factors.""" |
234 | 231 |
|
235 | | - pass |
236 | | - |
237 | | - class _ScaleFactorRegistry(GenericUnitRegistry[_ScaleFactorQuantity, _ScaleFactorUnit]): |
238 | | - """UnitRegistry class that uses _GemdUnits.""" |
| 232 | +class _ScaleFactorQuantity(UnitRegistry.Quantity): |
| 233 | + """Child class of Quantity for generating units w/ clean scaling factors.""" |
239 | 234 |
|
240 | | - Quantity: TypeAlias = _ScaleFactorQuantity |
241 | | - Unit: TypeAlias = _ScaleFactorUnit |
| 235 | + pass |
242 | 236 |
|
243 | | -except ImportError: # pragma: no cover |
244 | | - # https://pint.readthedocs.io/en/0.21/advanced/custom-registry-class.html |
245 | | - class _ScaleFactorUnit(UnitRegistry.Unit): |
246 | | - """Child class of Units for generating units w/ clean scaling factors.""" |
247 | 237 |
|
248 | | - def __format__(self, format_spec): |
249 | | - result = super().__format__(format_spec) |
250 | | - return _unmangle_scaling(result) |
| 238 | +class _ScaleFactorRegistry(GenericUnitRegistry[_ScaleFactorQuantity, _ScaleFactorUnit]): |
| 239 | + """UnitRegistry class that uses _GemdUnits.""" |
251 | 240 |
|
252 | | - class _ScaleFactorRegistry(UnitRegistry): |
253 | | - """UnitRegistry class that uses _GemdUnits.""" |
| 241 | + Quantity: TypeAlias = _ScaleFactorQuantity |
| 242 | + Unit: TypeAlias = _ScaleFactorUnit |
254 | 243 |
|
255 | | - _unit_class = _ScaleFactorUnit |
256 | 244 |
|
257 | 245 | _REGISTRY: _ScaleFactorRegistry = None # global requires it be defined in this scope |
258 | 246 |
|
|
0 commit comments