unyt: unable to use custom unit_registry nor modify base_value for unit in default_unit_registry

  • unyt version: 2.9.5
  • Python version: 3.10.1
  • Operating System: macOS Ventura 13.2

Description

I have two issues. The first is when creating a custom unit_registry I can’t seem to be able to be able to push the changes in any meaningful way to use the units in said registry. The second is when adding custom units to the default unit registry, I can’t modify the base_value after the fact.

What I Did

import unyt

# ISSUE 1
my_registry = unyt.UnitRegistry()
unyt.unit_object.define_unit('bushel', 0.5*unyt.kg, registry=my_registry)
a = 2. * unyt.bushel

Traceback (most recent call last):
  File "/Users/adamcohan/Desktop/Clinic/pygreet/testing_units/testing_registries.py", line 5, in <module>
    a = 2. * unyt.bushel
AttributeError: module 'unyt' has no attribute 'bushel'

# ISSUE 2
unyt.unit_object.define_unit('bushel', 0.5*unyt.kg)
print(unyt.bushel.base_value)
print(unyt.unit_registry.default_unit_registry['bushel'])
unyt.unit_registry.default_unit_registry.modify('bushel', 0.1*unyt.kg)
print(unyt.bushel.base_value)
print(unyt.unit_registry.default_unit_registry['bushel'])

0.5
(0.5, (mass), 0.0, '\\rm{bushel}', False)
0.5
(0.1, (mass), 0.0, '\\rm{bushel}', False)

About this issue

  • Original URL
  • State: closed
  • Created 8 months ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

I’ve identified a bug in caching as the source of the current behaviour. #476 addresses it

We shouldn’t allow modifying the base_value in the default unit registry–it’s too risky. We should raise an explicit error for this.