Skip to content

Commit 23d81fe

Browse files
author
Michael Johansen
committed
Merge branch 'users/mjohanse/more_pb_converters' of https://github.com/ni/nipanel-python into users/mjohanse/more_pb_converters
2 parents 0308030 + 15dd565 commit 23d81fe

File tree

7 files changed

+101
-32
lines changed

7 files changed

+101
-32
lines changed

.github/workflows/check_nipanel.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ jobs:
3333
run: poetry run mypy --platform win32
3434
- name: Bandit security checks
3535
run: poetry run bandit -c pyproject.toml -r src/nipanel
36+
- name: Add virtualenv to the path for pyright-action
37+
run: echo "$(poetry env info --path)/bin" >> $GITHUB_PATH
38+
- name: Pyright static analysis (Linux)
39+
uses: jakebailey/pyright-action@b5d50e5cde6547546a5c4ac92e416a8c2c1a1dfe # v2.3.2
40+
with:
41+
python-platform: Linux
42+
version: PATH
43+
- name: Pyright static analysis (Windows)
44+
uses: jakebailey/pyright-action@b5d50e5cde6547546a5c4ac92e416a8c2c1a1dfe # v2.3.2
45+
with:
46+
python-platform: Windows
47+
version: PATH

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ def setup(sphinx):
7373
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
7474

7575

76-
# TODO: AB#3120159 - Update nipanel intersphinx mapping for nitypes
7776
intersphinx_mapping = {
7877
"grpc": ("https://grpc.github.io/grpc/python/", None),
7978
"measurement-plugin-python": (
8079
"https://measurement-plugin-python.readthedocs.io/en/latest/",
8180
None,
8281
),
82+
"nitypes": ("https://nitypes.readthedocs.io/en/latest/", None),
8383
"numpy": ("https://numpy.org/doc/stable/", None),
8484
"protobuf": ("https://googleapis.dev/python/protobuf/latest/", None),
8585
"python": ("https://docs.python.org/3", None),

poetry.lock

Lines changed: 53 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ types-protobuf = ">=4.21"
2424
bandit = { version = ">=1.7", extras = ["toml"] }
2525
ni-python-styleguide = ">=0.4.1"
2626
mypy = ">=1.0"
27+
pyright = { version = ">=1.1.400", extras = ["nodejs"] }
2728

2829
[tool.poetry.group.test.dependencies]
2930
pytest = ">=7.2"
@@ -81,3 +82,7 @@ skips = [
8182
[tool.pytest.ini_options]
8283
addopts = "--doctest-modules --strict-markers"
8384
testpaths = ["src/nipanel", "tests"]
85+
86+
[tool.pyright]
87+
include = ["examples/", "src/", "tests/"]
88+
exclude = ["src/ni/protobuf/types/", "src/ni/pythonpanel/v1/"]

src/nipanel/converters/builtin.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def to_protobuf_message(self, python_value: bool) -> wrappers_pb2.BoolValue:
2626
"""Convert the Python bool to a protobuf wrappers_pb2.BoolValue."""
2727
return self.protobuf_message(value=python_value)
2828

29-
def to_python_value(self, protobuf_value: wrappers_pb2.BoolValue) -> bool:
29+
def to_python_value(self, protobuf_message: wrappers_pb2.BoolValue) -> bool:
3030
"""Convert the protobuf message to a Python bool."""
31-
return protobuf_value.value
31+
return protobuf_message.value
3232

3333

3434
class BytesConverter(Converter[bytes, wrappers_pb2.BytesValue]):
@@ -48,9 +48,9 @@ def to_protobuf_message(self, python_value: bytes) -> wrappers_pb2.BytesValue:
4848
"""Convert the Python bytes string to a protobuf wrappers_pb2.BytesValue."""
4949
return self.protobuf_message(value=python_value)
5050

51-
def to_python_value(self, protobuf_value: wrappers_pb2.BytesValue) -> bytes:
51+
def to_python_value(self, protobuf_message: wrappers_pb2.BytesValue) -> bytes:
5252
"""Convert the protobuf message to a Python bytes string."""
53-
return protobuf_value.value
53+
return protobuf_message.value
5454

5555

5656
class FloatConverter(Converter[float, wrappers_pb2.DoubleValue]):
@@ -70,9 +70,9 @@ def to_protobuf_message(self, python_value: float) -> wrappers_pb2.DoubleValue:
7070
"""Convert the Python float to a protobuf wrappers_pb2.DoubleValue."""
7171
return self.protobuf_message(value=python_value)
7272

73-
def to_python_value(self, protobuf_value: wrappers_pb2.DoubleValue) -> float:
73+
def to_python_value(self, protobuf_message: wrappers_pb2.DoubleValue) -> float:
7474
"""Convert the protobuf message to a Python float."""
75-
return protobuf_value.value
75+
return protobuf_message.value
7676

7777

7878
class IntConverter(Converter[int, wrappers_pb2.Int64Value]):
@@ -92,9 +92,9 @@ def to_protobuf_message(self, python_value: int) -> wrappers_pb2.Int64Value:
9292
"""Convert the Python int to a protobuf wrappers_pb2.Int64Value."""
9393
return self.protobuf_message(value=python_value)
9494

95-
def to_python_value(self, protobuf_value: wrappers_pb2.Int64Value) -> int:
95+
def to_python_value(self, protobuf_message: wrappers_pb2.Int64Value) -> int:
9696
"""Convert the protobuf message to a Python int."""
97-
return protobuf_value.value
97+
return protobuf_message.value
9898

9999

100100
class StrConverter(Converter[str, wrappers_pb2.StringValue]):
@@ -114,9 +114,9 @@ def to_protobuf_message(self, python_value: str) -> wrappers_pb2.StringValue:
114114
"""Convert the Python str to a protobuf wrappers_pb2.StringValue."""
115115
return self.protobuf_message(value=python_value)
116116

117-
def to_python_value(self, protobuf_value: wrappers_pb2.StringValue) -> str:
117+
def to_python_value(self, protobuf_message: wrappers_pb2.StringValue) -> str:
118118
"""Convert the protobuf message to a Python string."""
119-
return protobuf_value.value
119+
return protobuf_message.value
120120

121121

122122
class BoolCollectionConverter(Converter[Collection[bool], python_panel_types_pb2.BoolCollection]):
@@ -139,10 +139,10 @@ def to_protobuf_message(
139139
return self.protobuf_message(values=python_value)
140140

141141
def to_python_value(
142-
self, protobuf_value: python_panel_types_pb2.BoolCollection
142+
self, protobuf_message: python_panel_types_pb2.BoolCollection
143143
) -> Collection[bool]:
144144
"""Convert the protobuf message to a Python collection of bools."""
145-
return list(protobuf_value.values)
145+
return list(protobuf_message.values)
146146

147147

148148
class BytesCollectionConverter(
@@ -167,10 +167,10 @@ def to_protobuf_message(
167167
return self.protobuf_message(values=python_value)
168168

169169
def to_python_value(
170-
self, protobuf_value: python_panel_types_pb2.ByteStringCollection
170+
self, protobuf_message: python_panel_types_pb2.ByteStringCollection
171171
) -> Collection[bytes]:
172172
"""Convert the protobuf message to a Python collection of byte strings."""
173-
return list(protobuf_value.values)
173+
return list(protobuf_message.values)
174174

175175

176176
class FloatCollectionConverter(
@@ -195,10 +195,10 @@ def to_protobuf_message(
195195
return self.protobuf_message(values=python_value)
196196

197197
def to_python_value(
198-
self, protobuf_value: python_panel_types_pb2.FloatCollection
198+
self, protobuf_message: python_panel_types_pb2.FloatCollection
199199
) -> Collection[float]:
200200
"""Convert the protobuf message to a Python collection of floats."""
201-
return list(protobuf_value.values)
201+
return list(protobuf_message.values)
202202

203203

204204
class IntCollectionConverter(Converter[Collection[int], python_panel_types_pb2.IntCollection]):
@@ -221,10 +221,10 @@ def to_protobuf_message(
221221
return self.protobuf_message(values=python_value)
222222

223223
def to_python_value(
224-
self, protobuf_value: python_panel_types_pb2.IntCollection
224+
self, protobuf_message: python_panel_types_pb2.IntCollection
225225
) -> Collection[int]:
226226
"""Convert the protobuf message to a Python collection of integers."""
227-
return list(protobuf_value.values)
227+
return list(protobuf_message.values)
228228

229229

230230
class StrCollectionConverter(Converter[Collection[str], python_panel_types_pb2.StringCollection]):
@@ -247,7 +247,7 @@ def to_protobuf_message(
247247
return self.protobuf_message(values=python_value)
248248

249249
def to_python_value(
250-
self, protobuf_value: python_panel_types_pb2.StringCollection
250+
self, protobuf_message: python_panel_types_pb2.StringCollection
251251
) -> Collection[str]:
252252
"""Convert the protobuf message to a Python collection of strings."""
253-
return list(protobuf_value.values)
253+
return list(protobuf_message.values)

src/nipanel/converters/protobuf_types.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ def to_protobuf_message(self, python_value: Scalar[_AnyScalarType]) -> scalar_pb
196196

197197
return message
198198

199-
def to_python_value(self, protobuf_value: scalar_pb2.ScalarData) -> Scalar[_AnyScalarType]:
199+
def to_python_value(self, protobuf_message: scalar_pb2.ScalarData) -> Scalar[_AnyScalarType]:
200200
"""Convert the protobuf message to a Python Scalar."""
201-
if protobuf_value.units is None:
201+
if protobuf_message.units is None:
202202
raise ValueError("protobuf.units cannot be None.")
203203

204-
pb_type = str(protobuf_value.WhichOneof("value"))
204+
pb_type = str(protobuf_message.WhichOneof("value"))
205205
if pb_type not in _SCALAR_TYPE_TO_PB_ATTR_MAP.values():
206206
raise ValueError(f"Unexpected value for protobuf_value.WhichOneOf: {pb_type}")
207207

208-
value = getattr(protobuf_value, pb_type)
209-
return Scalar(value, protobuf_value.units)
208+
value = getattr(protobuf_message, pb_type)
209+
return Scalar(value, protobuf_message.units)

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Fixtures for testing."""
22

33
from collections.abc import Generator
4+
from concurrent.futures import ThreadPoolExecutor
5+
from typing import cast
46

57
import grpc
68
import pytest
79
from grpc.framework.foundation import logging_pool
8-
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import (
9-
PythonPanelServiceStub,
10-
)
10+
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub
1111

1212
from tests.utils._fake_python_panel_service import FakePythonPanelService
1313

@@ -16,6 +16,8 @@
1616
def fake_python_panel_service() -> Generator[FakePythonPanelService]:
1717
"""Fixture to create a FakePythonPanelServicer for testing."""
1818
with logging_pool.pool(max_workers=10) as thread_pool:
19+
# _LoggingPool is not a ThreadPoolExecutor, but it's duck-typing compatible with one.
20+
thread_pool = cast(ThreadPoolExecutor, thread_pool)
1921
service = FakePythonPanelService()
2022
service.start(thread_pool)
2123
yield service

0 commit comments

Comments
 (0)