Skip to content

Inconsistent unit validation error handling for y_unit in diffusion models #248

Closed

Description

@markbujehein

Description

Unit validation error handling in diffusion_model_base is inconsistent between x_unit and y_unit.

@henrikjacobsenfys isolated the source: When an invalid unit (such as an integer 123) is passed as x_unit, diffusion_model_base explicitly checks that it is convertible by calling _assert_convertible_to_mev, which correctly raises a scipp.UnitError. However, passing an invalid unit to y_unit bypasses explicit validation and fails later with a generic TypeError.

This inconsistency forces unit tests to expect different exceptions for the exact same logical error. This was highlighted in #246 , but appears to be a general issue. Multiple unit tests in tests/unit/easydynamics/sample_model/diffusion_model currently use the same hotfix where invalid y_unit inputs are explicitly parameterized to expect a TypeError.

Steps to Reproduce

In the unit test (e.g., inside test_chudley_elliott_diffusion.py), parameterizing invalid unit tests currently requires expecting different exceptions:

# For x_unit, explicit validation correctly raises a scipp.UnitError
(
    {'x_unit': 123, 'scale': 1.0, 'residence_time': 1.0, 'jump_length': 1.0},
    UnitError,
    'Invalid unit',
),
# For y_unit, we currently have to expect a TypeError as a workaround
(
    {'y_unit': 123, 'scale': 1.0, 'residence_time': 1.0, 'jump_length': 1.0},
    TypeError,
    None,
),

Behavior

  • Actual Behavior: Invalid x_unit raises UnitError, while an invalid y_unit falls through to raise a scipp.TypeError. Unit tests are currently hardcoded to accommodate this mismatch.
  • Expected Behavior: Both invalid x_unit and y_unit should be caught during initialization and raise a UnitError with a clear error message (e.g., 'Invalid unit').
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [priority] ⚠️ label neededAutomatically added to issues without a [priority] label[scope] ⚠️ label neededAutomatically added to issues and PRs without a [scope] label

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions