Skip to content

REFACTOR: Update register unit functionality#532

Merged
hpohekar merged 6 commits into
mainfrom
fix/refactor_register_unit
May 27, 2026
Merged

REFACTOR: Update register unit functionality#532
hpohekar merged 6 commits into
mainfrom
fix/refactor_register_unit

Conversation

@hpohekar

@hpohekar hpohekar commented May 26, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces significant improvements to the UnitRegistry class, focusing on more flexible and robust custom unit registration, improved string-based lookup, and clearer error handling for unit name collisions. The documentation and tests are updated to reflect these changes, making it easier for users to register and access custom units both at construction and after instantiation.

Key changes:

Custom Unit Registration & Lookup

  • Added a custom_units parameter to the UnitRegistry constructor, allowing users to register custom units at the time of registry creation using a list of dictionaries. This enables more convenient and declarative unit setup. [1] [2] [3] [4]
  • Introduced the get_unit method and a Quantity method on UnitRegistry for string-based lookup and creation of quantities using custom units, improving usability and consistency.

Name Collision & Error Handling

  • Replaced the UnitAlreadyRegistered exception with a more specific UnitNameAlreadyRegistered exception, used consistently throughout the codebase for name-only collision detection (i.e., duplicate unit names, including built-ins). The check is now explicitly name-based, allowing equivalent units with different names. [1] [2] [3] [4] [5] [6] [7] [8]

Documentation Updates

  • Expanded and reorganized documentation to clearly explain how to register custom units at construction and after instantiation, how string-based lookup works, and how instance isolation and name collision detection operate. Examples and notes have been added for clarity. [1] [2]

These changes make it easier and safer to extend the unit system with custom definitions, while providing clear feedback and documentation for users.

@github-actions github-actions Bot added documentation Improvements or additions to documentation testing Anything related to tests bug Something isn't working labels May 26, 2026
@hpohekar hpohekar changed the title refactor: Update register_unit REFACTOR: Update register_unit May 26, 2026
@hpohekar hpohekar linked an issue May 26, 2026 that may be closed by this pull request
@hpohekar hpohekar marked this pull request as ready for review May 26, 2026 16:49
@hpohekar hpohekar requested a review from a team as a code owner May 26, 2026 16:49
@hpohekar hpohekar changed the title REFACTOR: Update register_unit REFACTOR: Update register unit functionality May 26, 2026
@github-actions github-actions Bot added the maintenance Package and maintenance related label May 26, 2026
Comment thread tests/test_register_unit.py

@seanpearsonuk seanpearsonuk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving subject to checks by @mkundu1. Thanks!

@hpohekar hpohekar requested a review from mkundu1 May 26, 2026 17:01
@mkundu1

mkundu1 commented May 26, 2026

Copy link
Copy Markdown
Collaborator

@hpohekar The following code gives some error:

from ansys.units import UnitRegistry, Quantity, get_si_value
ur = UnitRegistry()
ur.register_unit(unit="micron", composition="m", factor=1e-6)
q1 = Quantity(1, ur.get_unit("micron"))  # this works
print(get_si_value(q1))
q2 = Quantity(1, ur.get_unit("kg mol^-1"))  # this fails
print(get_si_value(q2))
q3 = Quantity(1, "kg mol^-1") # this works
print(get_si_value(q3))

I need a consistent way of accessing units when I'm using the custom registry.

@seanpearsonuk

Copy link
Copy Markdown
Collaborator

@hpohekar The following code gives some error:

from ansys.units import UnitRegistry, Quantity, get_si_value
ur = UnitRegistry()
ur.register_unit(unit="micron", composition="m", factor=1e-6)
q1 = Quantity(1, ur.get_unit("micron"))  # this works
print(get_si_value(q1))
q2 = Quantity(1, ur.get_unit("kg mol^-1"))  # this fails
print(get_si_value(q2))
q3 = Quantity(1, "kg mol^-1") # this works
print(get_si_value(q3))

I need a consistent way of accessing units from the custom registry.

There's no need to rush to a solution here. We could alternatively just add micron for now, and revisit improvements later.

@seanpearsonuk seanpearsonuk self-requested a review May 26, 2026 17:35
@hpohekar hpohekar marked this pull request as draft May 26, 2026 17:53
@hpohekar hpohekar marked this pull request as ready for review May 26, 2026 18:32
@hpohekar

Copy link
Copy Markdown
Collaborator Author

@hpohekar The following code gives some error:

from ansys.units import UnitRegistry, Quantity, get_si_value
ur = UnitRegistry()
ur.register_unit(unit="micron", composition="m", factor=1e-6)
q1 = Quantity(1, ur.get_unit("micron"))  # this works
print(get_si_value(q1))
q2 = Quantity(1, ur.get_unit("kg mol^-1"))  # this fails
print(get_si_value(q2))
q3 = Quantity(1, "kg mol^-1") # this works
print(get_si_value(q3))

I need a consistent way of accessing units when I'm using the custom registry.

@mkundu1 I have refactored to fix this. Please confirm. Thank you.

@mkundu1

mkundu1 commented May 26, 2026

Copy link
Copy Markdown
Collaborator

@hpohekar The following code gives some error:

from ansys.units import UnitRegistry, Quantity, get_si_value
ur = UnitRegistry()
ur.register_unit(unit="micron", composition="m", factor=1e-6)
q1 = Quantity(1, ur.get_unit("micron"))  # this works
print(get_si_value(q1))
q2 = Quantity(1, ur.get_unit("kg mol^-1"))  # this fails
print(get_si_value(q2))
q3 = Quantity(1, "kg mol^-1") # this works
print(get_si_value(q3))

I need a consistent way of accessing units when I'm using the custom registry.

@mkundu1 I have refactored to fix this. Please confirm. Thank you.

@hpohekar This is working fine now, thanks

@hpohekar hpohekar merged commit 69e094b into main May 27, 2026
27 of 28 checks passed
@hpohekar hpohekar deleted the fix/refactor_register_unit branch May 27, 2026 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation maintenance Package and maintenance related testing Anything related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with registering custom units

4 participants