Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/check_nipanel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ jobs:
run: poetry run mypy --platform win32
- name: Bandit security checks
run: poetry run bandit -c pyproject.toml -r src/nipanel
- name: Add virtualenv to the path for pyright-action
run: echo "$(poetry env info --path)/bin" >> $GITHUB_PATH
- name: Pyright static analysis (Linux)
uses: jakebailey/pyright-action@b5d50e5cde6547546a5c4ac92e416a8c2c1a1dfe # v2.3.2
with:
python-platform: Linux
version: PATH
- name: Pyright static analysis (Windows)
uses: jakebailey/pyright-action@b5d50e5cde6547546a5c4ac92e416a8c2c1a1dfe # v2.3.2
with:
python-platform: Windows
version: PATH
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ types-protobuf = ">=4.21"
bandit = { version = ">=1.7", extras = ["toml"] }
ni-python-styleguide = ">=0.4.1"
mypy = ">=1.0"
pyright = { version = ">=1.1.400", extras = ["nodejs"] }

[tool.poetry.group.test.dependencies]
pytest = ">=7.2"
Expand Down Expand Up @@ -81,3 +82,7 @@ skips = [
[tool.pytest.ini_options]
addopts = "--doctest-modules --strict-markers"
testpaths = ["src/nipanel", "tests"]

[tool.pyright]
include = ["examples/", "src/", "tests/"]
exclude = ["src/ni/protobuf/types/", "src/ni/pythonpanel/v1/"]
40 changes: 20 additions & 20 deletions src/nipanel/converters/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def to_protobuf_message(self, python_value: bool) -> wrappers_pb2.BoolValue:
"""Convert the Python bool to a protobuf wrappers_pb2.BoolValue."""
return self.protobuf_message(value=python_value)

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


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

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


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

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


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

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


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

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


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

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


class BytesCollectionConverter(
Expand All @@ -167,10 +167,10 @@ def to_protobuf_message(
return self.protobuf_message(values=python_value)

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


class FloatCollectionConverter(
Expand All @@ -195,10 +195,10 @@ def to_protobuf_message(
return self.protobuf_message(values=python_value)

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


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

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


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

def to_python_value(
self, protobuf_value: python_panel_types_pb2.StringCollection
self, protobuf_message: python_panel_types_pb2.StringCollection
) -> Collection[str]:
"""Convert the protobuf message to a Python collection of strings."""
return list(protobuf_value.values)
return list(protobuf_message.values)
10 changes: 5 additions & 5 deletions src/nipanel/converters/protobuf_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def to_protobuf_message(self, python_value: Scalar[_AnyScalarType]) -> scalar_pb

return message

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

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

value = getattr(protobuf_value, pb_type)
return Scalar(value, protobuf_value.units)
value = getattr(protobuf_message, pb_type)
return Scalar(value, protobuf_message.units)
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Fixtures for testing."""

from collections.abc import Generator
from concurrent.futures import ThreadPoolExecutor
from typing import cast

import grpc
import pytest
from grpc.framework.foundation import logging_pool
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import (
PythonPanelServiceStub,
)
from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub

from tests.utils._fake_python_panel_service import FakePythonPanelService

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