From 007bfebc3ed24cb2451721857f987b785f97b0de Mon Sep 17 00:00:00 2001 From: Mike Prosser Date: Thu, 1 May 2025 13:51:35 -0500 Subject: [PATCH 1/3] _typing.py --- src/nipanel/_panel.py | 9 +-------- src/nipanel/_panel_client.py | 8 +------- src/nipanel/_typing.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/nipanel/_typing.py diff --git a/src/nipanel/_panel.py b/src/nipanel/_panel.py index 91a1c46..ae1e997 100644 --- a/src/nipanel/_panel.py +++ b/src/nipanel/_panel.py @@ -1,21 +1,14 @@ from __future__ import annotations -import sys from abc import ABC from types import TracebackType -from typing import TYPE_CHECKING import grpc from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool from nipanel._panel_client import PanelClient - -if TYPE_CHECKING: - if sys.version_info >= (3, 11): - from typing import Self - else: - from typing_extensions import Self +from nipanel._typing import Self class Panel(ABC): diff --git a/src/nipanel/_panel_client.py b/src/nipanel/_panel_client.py index 4b97440..34ab78b 100644 --- a/src/nipanel/_panel_client.py +++ b/src/nipanel/_panel_client.py @@ -4,7 +4,6 @@ import logging import threading -from typing import TYPE_CHECKING, Callable, TypeVar import grpc from ni.pythonpanel.v1.python_panel_service_pb2 import ConnectRequest, DisconnectRequest @@ -12,12 +11,7 @@ from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool -_T = TypeVar("_T") - -if TYPE_CHECKING: - from typing_extensions import ParamSpec - - _P = ParamSpec("_P") +from nipanel._typing import Callable, _T, _P _logger = logging.getLogger(__name__) diff --git a/src/nipanel/_typing.py b/src/nipanel/_typing.py new file mode 100644 index 0000000..ed26de5 --- /dev/null +++ b/src/nipanel/_typing.py @@ -0,0 +1,29 @@ +"""Single source for typing backports to avoid depending on typing_extensions at run time.""" + +from __future__ import annotations + +import sys +from typing import TYPE_CHECKING, Callable + +if sys.version_info >= (3, 11): + from typing import Self +elif TYPE_CHECKING: + from typing_extensions import Self +else: + Self = None + +if TYPE_CHECKING: + from typing_extensions import ParamSpec, TypeVar + + _P = ParamSpec("_P") + _T = TypeVar("_T") +else: + _P = None + _T = None + +__all__ = [ + "Callable", + "Self", + "_P", + "_T", +] From 0752f86fbfdc92569bc51fd875b77538e4be9680 Mon Sep 17 00:00:00 2001 From: Mike Prosser Date: Fri, 2 May 2025 09:44:08 -0500 Subject: [PATCH 2/3] only use _typing.py for types that don't exist in typing --- src/nipanel/_panel_client.py | 5 ++++- src/nipanel/_typing.py | 14 ++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/nipanel/_panel_client.py b/src/nipanel/_panel_client.py index 34ab78b..7eb97f7 100644 --- a/src/nipanel/_panel_client.py +++ b/src/nipanel/_panel_client.py @@ -4,6 +4,7 @@ import logging import threading +from typing import TypeVar, Callable import grpc from ni.pythonpanel.v1.python_panel_service_pb2 import ConnectRequest, DisconnectRequest @@ -11,8 +12,10 @@ from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool -from nipanel._typing import Callable, _T, _P +from nipanel._typing import ParamSpec +_P = ParamSpec("_P") +_T = TypeVar("_T") _logger = logging.getLogger(__name__) diff --git a/src/nipanel/_typing.py b/src/nipanel/_typing.py index ed26de5..634e5f2 100644 --- a/src/nipanel/_typing.py +++ b/src/nipanel/_typing.py @@ -3,7 +3,7 @@ from __future__ import annotations import sys -from typing import TYPE_CHECKING, Callable +from typing import TYPE_CHECKING if sys.version_info >= (3, 11): from typing import Self @@ -13,17 +13,11 @@ Self = None if TYPE_CHECKING: - from typing_extensions import ParamSpec, TypeVar - - _P = ParamSpec("_P") - _T = TypeVar("_T") + from typing_extensions import ParamSpec else: - _P = None - _T = None + from typing import TypeVar as ParamSpec __all__ = [ - "Callable", "Self", - "_P", - "_T", + "ParamSpec", ] From 8d11de5f7dcf3cb360d678c4344f6f63cb2cc675 Mon Sep 17 00:00:00 2001 From: Mike Prosser Date: Fri, 2 May 2025 13:03:22 -0500 Subject: [PATCH 3/3] better ParamSpec --- src/nipanel/_typing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nipanel/_typing.py b/src/nipanel/_typing.py index 634e5f2..59817c5 100644 --- a/src/nipanel/_typing.py +++ b/src/nipanel/_typing.py @@ -12,7 +12,9 @@ else: Self = None -if TYPE_CHECKING: +if sys.version_info >= (3, 10): + from typing import ParamSpec +elif TYPE_CHECKING: from typing_extensions import ParamSpec else: from typing import TypeVar as ParamSpec