Skip to content

Commit 67bbec6

Browse files
author
Michael Johansen
committed
Constrain types for python scalar class. Allow prerelease for nitypes dependency.
Signed-off-by: Michael Johansen <[email protected]>
1 parent 610bc05 commit 67bbec6

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ protobuf = {version=">=4.21"}
1313
ni-measurement-plugin-sdk = {version=">=2.3"}
1414
typing-extensions = ">=4.13.2"
1515
streamlit = ">=1.24"
16-
# Temporary dependency until ni-apis-python is available.
17-
nitypes = ">=0.1.0dev1"
16+
nitypes = {version=">=0.1.0dev1", allow-prereleases=true}
1817

1918
[tool.poetry.group.dev.dependencies]
2019
types-grpcio = ">=1.0"

src/nipanel/converters/protobuf_types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
"""Classes to convert between measurement specific protobuf types and containers."""
22

3-
from typing import Any, Type
3+
from typing import Type, TypeAlias, Union
44

55
from ni.protobuf.types import scalar_pb2
66
from nitypes.scalar import Scalar
77

88
from nipanel.converters import Converter
99

10+
_AnyScalarType: TypeAlias = Union[bool, int, float, str]
1011

11-
class ScalarConverter(Converter[Scalar[Any], scalar_pb2.ScalarData]):
12+
13+
class ScalarConverter(Converter[Scalar[_AnyScalarType], scalar_pb2.ScalarData]):
1214
"""A converter for Scalar objects."""
1315

1416
@property
@@ -21,7 +23,7 @@ def protobuf_message(self) -> Type[scalar_pb2.ScalarData]:
2123
"""The type-specific protobuf message for the Python type."""
2224
return scalar_pb2.ScalarData
2325

24-
def to_protobuf_message(self, python_value: Scalar[Any]) -> scalar_pb2.ScalarData:
26+
def to_protobuf_message(self, python_value: Scalar[_AnyScalarType]) -> scalar_pb2.ScalarData:
2527
"""Convert the Python Scalar to a protobuf scalar_pb2.ScalarData."""
2628
message = self.protobuf_message()
2729
message.units = python_value.units
@@ -38,7 +40,7 @@ def to_protobuf_message(self, python_value: Scalar[Any]) -> scalar_pb2.ScalarDat
3840

3941
return message
4042

41-
def to_python_value(self, protobuf_value: scalar_pb2.ScalarData) -> Scalar[Any]:
43+
def to_python_value(self, protobuf_value: scalar_pb2.ScalarData) -> Scalar[_AnyScalarType]:
4244
"""Convert the protobuf message to a Python Scalar."""
4345
if protobuf_value.units is None:
4446
raise ValueError("protobuf.units cannot be None.")

tests/unit/test_protobuf_type_conversion.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def test___scalar_protobuf_value_unset___convert___throws_type_error() -> None:
7474
def test___scalar_protobuf_units_unset___convert___python_units_blank() -> None:
7575
protobuf_value = ScalarData()
7676
protobuf_value.bool_value = True
77-
print(f"Protobuf units: {protobuf_value.units!r}")
7877

7978
converter = ScalarConverter()
8079
python_value = converter.to_python_value(protobuf_value)

0 commit comments

Comments
 (0)