Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should set_application_registry work with GenericUnitRegistry objects? #2079

Open
cometbeetle opened this issue Nov 22, 2024 · 0 comments
Open

Comments

@cometbeetle
Copy link

cometbeetle commented Nov 22, 2024

Right now, the ApplicationRegistry.set method (in pint.registry) requires that the new_registry parameter be an instance of UnitRegistry or LazyRegistry, and therefore does not allow instances of GenericUnitRegistry to be used:

def set(self, new_registry):
    """Set the new registry

    Parameters
    ----------
    new_registry : ApplicationRegistry or LazyRegistry or UnitRegistry
        The new registry.

    See Also
    --------
    set_application_registry
    """
    if isinstance(new_registry, type(self)):
        new_registry = new_registry.get()

    if not isinstance(new_registry, (LazyRegistry, UnitRegistry)):
        raise TypeError("Expected UnitRegistry; got %s" % type(new_registry))
    logger.debug(
        "Changing app registry from %r to %r.", self._registry, new_registry
    )
    self._registry = new_registry

I was wondering if this is the intended behavior, or if it would make sense to also allow GenericUnitRegistry objects to be set as the application registry. I'd like to be able to use a custom generic unit registry class (as described here in the docs) with set_application_registry in a manner similar to the following:

class MyRegistry(
    pint.registry.GenericUnitRegistry[MyCustomQuantity, MyCustomUnit]
):
    Quantity = MyCustomQuantity
    Unit = MyCustomUnit

reg = MyRegistry()
pint.set_application_registry(reg)

Currently, doing something like that just raises a TypeError because of the behavior of ApplicationRegistry.set. Perhaps ApplicationRegistry.set could be adjusted to also allow GenericUnitRegistry objects?

@cometbeetle cometbeetle changed the title Should set_application_registry work with GenericUnitRegistry objects? Should set_application_registry work with GenericUnitRegistry objects? Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant