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

Float/Double/FloatArray1D/DoubleArray1D parameters do not allow integer default values #824

Closed
bkeryan opened this issue Aug 1, 2024 · 0 comments · Fixed by #832
Closed
Labels
bug Something isn't working triage New issue that needs to be reviewed

Comments

@bkeryan
Copy link
Collaborator

bkeryan commented Aug 1, 2024

Bug Report

In Python, int is generally duck-type compatible with float. In most cases, functions that expect a float will also accept an int.

Python type checking tools like mypy explicitly support "duck type compatibility" between int and float: https://mypy.readthedocs.io/en/stable/duck_type_compatibility.html

However, if a measurement plugin uses an int in the default value of a Float, Double, FloatArray1D, or DoubleArray1D configuration, it gets an error: TypeError: Unexpected type <class 'int'> in the default value for 'float_array_data'. Expected type: <class 'float'>.

Repro or Code Sample

@measurement_service.configuration("sample_rate", nims.DataType.Double, 1000)

Expected Behavior

It works.

Current Behavior

TypeError: Unexpected type <class 'int'> in the default value for 'sample_rate'. Expected type: <class 'float'>.

Possible Solution

Add this to _validate_default_value_type_for_basic_type:

if expected_type is float and isinstance(default_value, int):
    return

Update tests that check for an error in this case:

FAILED tests/unit/test_metadata.py::test___default_value_different_from_type___validate___raises_type_exception[DataType.Double-1-annotations6] - Failed: DID NOT RAISE <class 'TypeError'>
FAILED tests/unit/test_service.py::test___measurement_service___add_configuration_with_mismatch_default_value__raises_type_error[Float-DataType.Float-1] - Failed: DID NOT RAISE <class 'TypeError'>

Context

This came up in the code review for #807

Your Environment

  • OS & Device: Windows
  • ni-measurement-plugin-sdk-service version: 1.0.0
  • MeasurementLink version: 24.8.x
  • Python version: 3.9

AB#2819852

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issue that needs to be reviewed
Projects
None yet
1 participant