Skip to content

Commit 61cbfa0

Browse files
committed
Narrow XYData subtype to np.float64
Signed-off-by: Michael Johansen <[email protected]>
1 parent 77fc1b1 commit 61cbfa0

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

packages/ni.protobuf.types/src/ni/protobuf/types/xydata_conversion.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
from typing import Any
6-
75
import numpy as np
86
from nitypes.xy_data import XYData
97

@@ -14,7 +12,7 @@
1412
)
1513

1614

17-
def float64_xydata_to_protobuf(value: XYData[Any], /) -> xydata_pb2.DoubleXYData:
15+
def float64_xydata_to_protobuf(value: XYData[np.float64], /) -> xydata_pb2.DoubleXYData:
1816
"""Convert a XYData python object to a protobuf xydata_pb2.DoubleXYData."""
1917
attributes = extended_properties_to_protobuf(value.extended_properties)
2018
xydata_message = xydata_pb2.DoubleXYData(

packages/ni.protobuf.types/tests/unit/test_xydata_conversion.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ def test___float64_xydata___convert___valid_doublexydata_protobuf() -> None:
8888
assert list(protobuf_value.y_data) == expected_y_data
8989

9090

91-
def test___int16_xydata___convert___valid_doublexydata_protobuf() -> None:
91+
def test___int16_xydata___convert___causes_mypy_error() -> None:
9292
python_value = XYData.from_arrays_1d(
9393
x_array=[1, 2, 3],
9494
y_array=[4, 5, 6],
9595
dtype=np.int16,
9696
)
9797

98-
protobuf_value = float64_xydata_to_protobuf(python_value)
98+
# The next line should generate a mypy error. If it doesn't, we'll get an 'unused
99+
# ignore' mypy error.
100+
protobuf_value = float64_xydata_to_protobuf(python_value) # type: ignore[arg-type]
99101

102+
# This conversion still works, so we might as well check it. Int values are converted to float.
100103
assert isinstance(protobuf_value, xydata_pb2.DoubleXYData)
101-
# Data values converted to float. Is this OK? Or should we raise an error here?
102104
assert list(protobuf_value.x_data) == [1.0, 2.0, 3.0]
103105
assert list(protobuf_value.y_data) == [4.0, 5.0, 6.0]
104106

0 commit comments

Comments
 (0)