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

[feat] Improve the error message of the check_dims and check_units with multiple results #88

Open
Routhleck opened this issue Dec 27, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Routhleck
Copy link
Member

For example:

@check_dims(result=(second.dim, volt.dim))
def d_function(true_result):
    """
    If true_result is True, return values in seconds and volts; otherwise, return values in volts and seconds.
    """
    if true_result:
        return 5 * second, 3 * volt
    else:
        return 3 * volt, 5 * second

d_function(False)

The output is:

DimensionMismatchError: The return value of function 'd_function' was expected to have dimension s but was '3 * volt' (unit is m^2 kg s^-3 A^-1).

This error message is not intuitive for users, and they don’t know which return value has a problem.

Perhaps we can tell what the dims or units of the return values should be and compare them to what they currently are.

The following codes are where the error is thrown now

brainunit/brainunit/_base.py

Lines 4534 to 4544 in 4a063d4

def _check_dim(f, val, dim):
dim = DIMENSIONLESS if dim is None else dim
if not have_same_dim(val, dim):
unit = get_dim_for_display(dim)
error_message = (
"The return value of function "
f"'{f.__name__}' was expected to have "
f"dimension {unit} but was "
f"'{val}'"
)
raise DimensionMismatchError(error_message, get_dim(val))

brainunit/brainunit/_base.py

Lines 4859 to 4868 in 4a063d4

def _check_unit(f, val, unit):
unit = UNITLESS if unit is None else unit
if not has_same_unit(val, unit):
error_message = (
"The return value of function "
f"'{f.__name__}' was expected to have "
f"unit {get_unit(val)} but was "
f"'{val}'"
)
raise UnitMismatchError(error_message, get_unit(val))

@Routhleck Routhleck added the enhancement New feature or request label Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant