-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the application registry as the default unit registry
- Loading branch information
Showing
5 changed files
with
36 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
from typing import Union | ||
|
||
import pint | ||
|
||
#: Default unit registry | ||
unit_registry = pint.UnitRegistry() | ||
#: Default unit registry (if not modified with :func:`.set_unit_registry`, it is the `application registry <https://pint.readthedocs.io/en/stable/getting/pint-in-your-projects.html#having-a-shared-registry>`_). | ||
unit_registry = pint.get_application_registry() | ||
|
||
|
||
def set_unit_registry(ureg: pint.UnitRegistry) -> None: | ||
def set_unit_registry(ureg: Union[pint.UnitRegistry, pint.ApplicationRegistry]) -> None: | ||
""" | ||
Set unit registry. By default, Pinttrs has its own registry. | ||
Set unit registry. By default, Pinttrs uses the | ||
`application registry <https://pint.readthedocs.io/en/stable/getting/pint-in-your-projects.html#having-a-shared-registry>`_). | ||
:param ureg: Unit registry. | ||
:raises: :class:`TypeError` if ``ureg`` is not a :class:`pint.UnitRegistry`. | ||
.. versionchanged:: 24.1.0 | ||
The default registry is now the application registry. | ||
""" | ||
global unit_registry | ||
if not isinstance(ureg, pint.UnitRegistry): | ||
raise TypeError("ureg must be a pint.UnitRegistry") | ||
if not isinstance(ureg, (pint.UnitRegistry, pint.ApplicationRegistry)): | ||
raise TypeError( | ||
"ureg must be a pint.UnitRegistry or pint.ApplicationRegistry instance" | ||
) | ||
unit_registry = ureg | ||
|
||
|
||
def get_unit_registry() -> pint.UnitRegistry: | ||
def get_unit_registry() -> Union[pint.UnitRegistry, pint.ApplicationRegistry]: | ||
""" | ||
Get default unit registry. | ||
Get default unit registry. By default, Pinttrs uses the | ||
`application registry <https://pint.readthedocs.io/en/stable/getting/pint-in-your-projects.html#having-a-shared-registry>`_). | ||
.. versionchanged:: 24.1.0 | ||
The default registry is now the application registry. | ||
""" | ||
global unit_registry | ||
return unit_registry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters