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

[releases/2.1] Cherry-pick: Add test case to check mypy error in generated client and fix existing mypy errors #929

Merged
merged 2 commits into from
Sep 24, 2024
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
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<%!
import re
from typing import Any
%>\
\
<%page args="class_name, display_name, configuration_metadata, output_metadata, service_class, configuration_parameters_with_type_and_default_values, measure_api_parameters, output_parameters_with_type, built_in_import_modules, custom_import_modules, enum_by_class_name, configuration_parameters_type_url"/>\
\
<%
def _replace_enum_class_type(input_string: str) -> str:
"""Replace enum class type representation with the enum name."""
pattern = r"<enum '([^']+)'>"
return re.sub(pattern, r"\1", input_string)

configuration_metadata = _replace_enum_class_type(str(configuration_metadata))
if output_metadata:
output_metadata = _replace_enum_class_type(str(output_metadata))
def _format_default_value(value: Any) -> Any:
if isinstance(value, str):
return repr(value)
else:
return value
%>\
\

"""Generated client API for the ${display_name | repr} measurement plug-in."""

from __future__ import annotations

import logging
import threading
% if len(enum_by_class_name):
Expand All @@ -35,6 +35,7 @@ from typing import ${", ".join(sorted(typing_imports))}

import grpc
from google.protobuf import any_pb2, descriptor_pool
from google.protobuf.type_pb2 import Field
from ni_measurement_plugin_sdk_service._internal.stubs.ni.measurementlink.measurement.v2 import (
measurement_service_pb2 as v2_measurement_service_pb2,
measurement_service_pb2_grpc as v2_measurement_service_pb2_grpc,
Expand Down Expand Up @@ -111,10 +112,44 @@ class ${class_name}:
self._pin_map_client = pin_map_client
self._stub: Optional[v2_measurement_service_pb2_grpc.MeasurementServiceStub] = None
self._measure_response: Optional[
Generator[v2_measurement_service_pb2.MeasureResponse, None, None]
grpc.CallIterator[v2_measurement_service_pb2.MeasureResponse]
] = None
self._configuration_metadata = ${configuration_metadata}
self._output_metadata = ${output_metadata}
self._configuration_metadata = {
% for key, value in configuration_metadata.items():
${key}: ParameterMetadata(
display_name=${value.display_name | repr},
type=Field.Kind.ValueType(${value.type}),
repeated=${value.repeated},
default_value=${_format_default_value(value.default_value)},
annotations=${value.annotations | n, repr},
message_type=${value.message_type | repr},
field_name=${value.field_name | repr},
% if value.enum_type:
enum_type=${value.enum_type.__name__}
% else:
enum_type=${value.enum_type}
% endif
),
% endfor
}
self._output_metadata = {
% for key, value in output_metadata.items():
${key}: ParameterMetadata(
display_name=${value.display_name | repr},
type=Field.Kind.ValueType(${value.type}),
repeated=${value.repeated},
default_value=${value.default_value},
annotations=${value.annotations | n, repr},
message_type=${value.message_type | repr},
field_name=${value.field_name | repr},
% if value.enum_type:
enum_type=${value.enum_type.__name__}
% else:
enum_type=${value.enum_type}
% endif
),
% endfor
}
if grpc_channel is not None:
self._stub = v2_measurement_service_pb2_grpc.MeasurementServiceStub(grpc_channel)
self._create_file_descriptor()
Expand All @@ -134,7 +169,7 @@ class ${class_name}:
self._pin_map_context = val

@property
def sites(self) -> List[int]:
def sites(self) -> Optional[List[int]]:
"""The sites where the measurement must be executed."""
return self._pin_map_context.sites

Expand Down
29 changes: 29 additions & 0 deletions packages/generator/tests/acceptance/test_client_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from typing import Generator

import mypy.api
import pytest
from ni_measurement_plugin_sdk_service.measurement.service import MeasurementService

Expand Down Expand Up @@ -167,6 +168,34 @@ def test___non_streaming_measurement___create_client___render_with_proper_line_e
_assert_line_ending(temp_directory / filename)


def test___non_streaming_measurement___create_client___render_without_mypy_error(
create_client: CliRunnerFunction,
tmp_path_factory: pytest.TempPathFactory,
non_streaming_measurement_service: MeasurementService,
) -> None:
temp_directory = tmp_path_factory.mktemp("measurement_plugin_client_files")
module_name = "non_streaming_data_measurement_client"
filename = f"{module_name}.py"

result = create_client(
[
"--measurement-service-class",
"ni.tests.NonStreamingDataMeasurement_Python",
"--module-name",
module_name,
"--class-name",
"NonStreamingDataMeasurementClient",
"--directory-out",
str(temp_directory),
]
)

mypy_result = mypy.api.run([str(temp_directory / filename)])
mypy_exit_status = mypy_result[2]
assert result.exit_code == 0
assert mypy_exit_status == 0


def _assert_equal(expected_path: pathlib.Path, result_path: pathlib.Path) -> None:
expected = expected_path.read_text()
result = result_path.read_text()
Expand Down
Loading