diff options
author | Hernan Grecco <hgrecco@gmail.com> | 2023-05-11 17:11:03 -0300 |
---|---|---|
committer | Hernan Grecco <hgrecco@gmail.com> | 2023-05-11 17:11:03 -0300 |
commit | f8a7fed4bce13e545a4f1dc96057fcbf4067e835 (patch) | |
tree | a1ee895829db0b64cb9be7b80ca49f091b132b54 | |
parent | 1adc26a6f2b42ffaf2eb9e444dc00afc8947df2a (diff) | |
download | pint-f8a7fed4bce13e545a4f1dc96057fcbf4067e835.tar.gz |
Better SharedRegistryObjects creation
-rw-r--r-- | docs/user/defining-quantities.rst | 2 | ||||
-rw-r--r-- | pint/__init__.py | 1 | ||||
-rw-r--r-- | pint/facets/plain/quantity.py | 10 | ||||
-rw-r--r-- | pint/util.py | 12 |
4 files changed, 18 insertions, 7 deletions
diff --git a/docs/user/defining-quantities.rst b/docs/user/defining-quantities.rst index ec57454..297ddc8 100644 --- a/docs/user/defining-quantities.rst +++ b/docs/user/defining-quantities.rst @@ -72,7 +72,7 @@ Using string parsing Pint includes a powerful parser for detecting magnitudes and units (with or without prefixes) in strings. Calling the ``UnitRegistry()`` directly -invokes the parsing function: +invokes the parsing function ``UnitRegistry.parse_expression``: .. doctest:: diff --git a/pint/__init__.py b/pint/__init__.py index b8b5b43..d7f08d5 100644 --- a/pint/__init__.py +++ b/pint/__init__.py @@ -36,6 +36,7 @@ Quantity = UnitRegistry.Quantity Unit = UnitRegistry.Unit Measurement = UnitRegistry.Measurement Context = UnitRegistry.Context +Group = UnitRegistry.Group try: # pragma: no cover __version__ = version("pint") diff --git a/pint/facets/plain/quantity.py b/pint/facets/plain/quantity.py index f6c27cf..c06d02f 100644 --- a/pint/facets/plain/quantity.py +++ b/pint/facets/plain/quantity.py @@ -169,11 +169,11 @@ class PlainQuantity(Generic[MagnitudeT], PrettyIPython, SharedRegistryObject): ) -> PlainQuantity[Any]: ... - # @overload - # def __new__( - # cls, value: PlainQuantity[Any], units: UnitLike | None = None - # ) -> PlainQuantity[Any]: - # ... + @overload + def __new__( + cls, value: PlainQuantity[Any], units: UnitLike | None = None + ) -> PlainQuantity[Any]: + ... def __new__(cls, value, units=None): if is_upcast_type(type(value)): diff --git a/pint/util.py b/pint/util.py index afae829..950d223 100644 --- a/pint/util.py +++ b/pint/util.py @@ -21,6 +21,7 @@ from logging import NullHandler from numbers import Number from token import NAME, NUMBER import tokenize +import types from typing import ( TYPE_CHECKING, ClassVar, @@ -1142,4 +1143,13 @@ def create_class_with_registry( filling _REGISTRY class attribute with an actual instanced registry. """ - return type(base_class.__name__, (base_class,), dict(_REGISTRY=registry)) + class_body = { + "__module__": f"pint.{base_class.__name__}", + "_REGISTRY": registry, + } + + return types.new_class( + base_class.__name__, + bases=(base_class,), + exec_body=lambda ns: ns.update(class_body), + ) |