Skip to content

Commit 8d2eefc

Browse files
authored
[releases/2.3] Update to Python 3.9 syntax (#1182)
Update to Python 3.9 syntax (#1092) * Run pyupgrade --py39-plus * Add `from __future__ import annotations` to all files that use `from typing import...` so we can use PEP 604 type hints * Run `pyupgrade --py39-plus` again * Run ni-python-styleguide and fix issues * generator: Update Python syntax in codegen templates * examples: Update _helpers.py and type hints * examples: Run ni-python-styleguide and fix errors, revert a couple format strings * service: Delete types from docstrings * Re-run pyupgrade --py39-plus (cherry picked from commit addd51b)
1 parent 67199a6 commit 8d2eefc

141 files changed

Lines changed: 997 additions & 780 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/game_of_life/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Helper classes and functions for measurement plug-in examples."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
from typing import Any, Callable, TypeVar
68

79
import click
810

911

10-
class TestStandSupport(object):
12+
class TestStandSupport:
1113
"""Class that communicates with TestStand."""
1214

1315
_PIN_MAP_ID_VAR = "NI.MeasurementPlugIns.PinMapId"

examples/game_of_life/measurement.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
import random
55
import threading
66
import time
7-
from typing import Any, Generator, List, Tuple
7+
from collections.abc import Generator
8+
from typing import Any
89

910
import click
1011
import grpc
1112
import ni_measurement_plugin_sdk_service as nims
1213
from _helpers import configure_logging, verbosity_option
13-
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import xydata_pb2
14+
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types import (
15+
xydata_pb2,
16+
)
1417

1518
service_directory = pathlib.Path(__file__).resolve().parent
1619
measurement_service = nims.MeasurementService(
1720
service_config_path=service_directory / "game_of_life.serviceconfig",
1821
ui_file_paths=[service_directory / "game_of_life.measui"],
1922
)
2023

21-
Grid = List[List[bool]]
24+
Grid = list[list[bool]]
2225

2326
INFINITE_GENERATIONS = -1
2427

@@ -32,7 +35,7 @@
3235
@measurement_service.output("generation", nims.DataType.UInt32)
3336
def measure(
3437
width: int, height: int, update_interval_msec: int, max_generations: int
35-
) -> Generator[Tuple[xydata_pb2.DoubleXYData, int], None, None]:
38+
) -> Generator[tuple[xydata_pb2.DoubleXYData, int], None, None]:
3639
"""Streaming measurement that returns Conway's Game of Life grid as DoubleXYData."""
3740
cancellation_event = threading.Event()
3841
measurement_service.context.add_cancel_callback(cancellation_event.set)

examples/nidaqmx_analog_input/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Helper classes and functions for measurement plug-in examples."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
from typing import Any, Callable, TypeVar
68

79
import click
810

911

10-
class TestStandSupport(object):
12+
class TestStandSupport:
1113
"""Class that communicates with TestStand."""
1214

1315
_PIN_MAP_ID_VAR = "NI.MeasurementPlugIns.PinMapId"

examples/nidaqmx_analog_input/measurement.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import pathlib
55
import sys
6-
from typing import List, Tuple
76

87
import click
98
import ni_measurement_plugin_sdk_service as nims
@@ -31,7 +30,7 @@
3130
@measurement_service.configuration("sample_rate", nims.DataType.Double, 1000.0)
3231
@measurement_service.configuration("number_of_samples", nims.DataType.UInt64, 100)
3332
@measurement_service.output("acquired_samples", nims.DataType.DoubleArray1D)
34-
def measure(pin_name: str, sample_rate: float, number_of_samples: int) -> Tuple[List[float]]:
33+
def measure(pin_name: str, sample_rate: float, number_of_samples: int) -> tuple[list[float]]:
3534
"""Perform a finite analog input measurement with NI-DAQmx."""
3635
logging.info(
3736
"Executing measurement: pin_name=%s sample_rate=%g number_of_samples=%d",
@@ -68,7 +67,7 @@ def cancel_callback() -> None:
6867
return (voltage_values,)
6968

7069

71-
def _log_measured_values(samples: List[float], max_samples_to_display: int = 5) -> None:
70+
def _log_measured_values(samples: list[float], max_samples_to_display: int = 5) -> None:
7271
"""Log the measured values."""
7372
if len(samples) > max_samples_to_display:
7473
for index, value in enumerate(samples[0 : max_samples_to_display - 1]):

examples/nidcpower_source_dc_voltage/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Helper classes and functions for measurement plug-in examples."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
from typing import Any, Callable, TypeVar
68

79
import click
810

911

10-
class TestStandSupport(object):
12+
class TestStandSupport:
1113
"""Class that communicates with TestStand."""
1214

1315
_PIN_MAP_ID_VAR = "NI.MeasurementPlugIns.PinMapId"

examples/nidcpower_source_dc_voltage/measurement.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import sys
88
import threading
99
import time
10+
from collections.abc import Iterable
1011
from contextlib import ExitStack
11-
from typing import TYPE_CHECKING, Iterable, List, NamedTuple, Tuple
12+
from typing import TYPE_CHECKING, NamedTuple
1213

1314
import click
1415
import grpc
@@ -69,7 +70,7 @@ def measure(
6970
current_limit: float,
7071
current_limit_range: float,
7172
source_delay: float,
72-
) -> Tuple[List[int], List[str], List[float], List[float], List[bool]]:
73+
) -> tuple[list[int], list[str], list[float], list[float], list[bool]]:
7374
"""Source and measure a DC voltage with an NI SMU."""
7475
logging.info("Executing measurement: pin_names=%s voltage_level=%g", pin_names, voltage_level)
7576

@@ -106,12 +107,12 @@ def measure(
106107
channels, cancellation_event, nidcpower.enums.Event.SOURCE_COMPLETE, timeout
107108
)
108109

109-
measurements: List[_Measurement] = []
110+
measurements: list[_Measurement] = []
110111
measured_sites, measured_pins = [], []
111112
for session_info in session_infos:
112113
channels = session_info.session.channels[session_info.channel_list]
113114
# Measure the voltage and current for each output of the session.
114-
session_measurements: List[_Measurement] = channels.measure_multiple()
115+
session_measurements: list[_Measurement] = channels.measure_multiple()
115116

116117
for measurement, channel_mapping in zip(
117118
session_measurements, session_info.channel_mappings

examples/nidcpower_source_dc_voltage_with_multiplexer/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Helper classes and functions for measurement plug-in examples."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
from typing import Any, Callable, TypeVar
68

79
import click
810

911

10-
class TestStandSupport(object):
12+
class TestStandSupport:
1113
"""Class that communicates with TestStand."""
1214

1315
_PIN_MAP_ID_VAR = "NI.MeasurementPlugIns.PinMapId"

examples/nidcpower_source_dc_voltage_with_multiplexer/measurement.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
import threading
99
import time
10-
from typing import TYPE_CHECKING, NamedTuple, Tuple
10+
from typing import TYPE_CHECKING, NamedTuple
1111

1212
import click
1313
import grpc
@@ -63,7 +63,7 @@ def measure(
6363
current_limit: float,
6464
current_limit_range: float,
6565
source_delay: float,
66-
) -> Tuple[float, float]:
66+
) -> tuple[float, float]:
6767
"""Source and measure a DC voltage with an NI SMU connected via an NI-SWITCH multiplexer."""
6868
logging.info("Executing measurement: pin_name=%s voltage_level=%g", pin_name, voltage_level)
6969

examples/nidigital_spi/_helpers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Helper classes and functions for measurement plug-in examples."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
from typing import Any, Callable, TypeVar
68

79
import click
810

911

10-
class TestStandSupport(object):
12+
class TestStandSupport:
1113
"""Class that communicates with TestStand."""
1214

1315
_PIN_MAP_ID_VAR = "NI.MeasurementPlugIns.PinMapId"

examples/nidigital_spi/measurement.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Test a SPI device using an NI Digital Pattern instrument."""
22

3+
from __future__ import annotations
4+
35
import logging
46
import pathlib
57
import sys
6-
from typing import Iterable, Tuple, Union
8+
from collections.abc import Iterable
79

810
import click
911
import ni_measurement_plugin_sdk_service as nims
@@ -42,7 +44,7 @@ def measure(
4244
timing_file_path: str,
4345
pattern_file_path: str,
4446
load_files: bool,
45-
) -> Tuple:
47+
) -> tuple:
4648
"""Test a SPI device using an NI Digital Pattern instrument."""
4749
logging.info("Starting test: pin_names=%s", pin_names)
4850

@@ -80,7 +82,7 @@ def measure(
8082

8183

8284
def _resolve_relative_path(
83-
directory_path: pathlib.Path, file_path: Union[str, pathlib.Path]
85+
directory_path: pathlib.Path, file_path: str | pathlib.Path
8486
) -> pathlib.Path:
8587
file_path = pathlib.Path(file_path)
8688
if file_path.is_absolute():

0 commit comments

Comments
 (0)