From e62a7e9af0b5ad24aaf9ae8be486d2396e54bac3 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sun, 3 Nov 2024 08:15:21 +0900 Subject: [PATCH] Replace `Literal` and `Protocol` with runtime-referable symbols. (#651) * Remove `Literal` from `hints` and add the typing symbol to `shelllink`. * Move `_MethodTypeDesc` from `hints` to `tools.codegenerator.typeannotator`. --- comtypes/hints.pyi | 15 ++------------- comtypes/shelllink.py | 6 +++--- comtypes/tools/codegenerator/typeannotator.py | 14 +++++++------- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/comtypes/hints.pyi b/comtypes/hints.pyi index b67c8ffa..27c24cfa 100644 --- a/comtypes/hints.pyi +++ b/comtypes/hints.pyi @@ -3,17 +3,11 @@ # - utilities for type hints. import ctypes import sys -from typing import Any as Any, ClassVar, Generic, NoReturn, TypeVar, overload +from typing import Any as Any, ClassVar, Generic, NoReturn, Protocol, TypeVar, overload from typing import Optional, Union as _UnionT -from typing import List, Tuple as Tuple, Type +from typing import Tuple as Tuple, Type from typing import Callable, Iterator, Sequence -if sys.version_info >= (3, 8): - from typing import Protocol - from typing import Literal as Literal -else: - from typing_extensions import Protocol - from typing_extensions import Literal as Literal if sys.version_info >= (3, 9): from typing import Annotated as Annotated else: @@ -85,11 +79,6 @@ class FirstComItfOf(Generic[_T_coclass]): as an argument. """ -class _MethodTypeDesc(Protocol): - arguments: List[Tuple[Any, str, List[str], Optional[Any]]] - idlflags: List[str] - name: str - _P_Put = ParamSpec("_P_Put") _R_Put = TypeVar("_R_Put") _P_PutRef = ParamSpec("_P_PutRef") diff --git a/comtypes/shelllink.py b/comtypes/shelllink.py index a0cf0359..6b656848 100644 --- a/comtypes/shelllink.py +++ b/comtypes/shelllink.py @@ -2,7 +2,7 @@ from ctypes import POINTER from ctypes import byref, create_string_buffer, create_unicode_buffer from ctypes.wintypes import DWORD, WIN32_FIND_DATAA, WIN32_FIND_DATAW, MAX_PATH -from typing import Tuple, TYPE_CHECKING +from typing import Tuple, TYPE_CHECKING, Literal from comtypes import IUnknown, GUID, COMMETHOD, HRESULT, CoClass @@ -143,7 +143,7 @@ def ShowCmd(self) -> int: ... def ShowCmd(self, piShowCmd: int) -> None: ... def SetIconLocation(self, pszIconPath: bytes, iIcon: int) -> hints.Hresult: ... def SetRelativePath( - self, pszPathRel: bytes, dwReserved: hints.Literal[0] + self, pszPathRel: bytes, dwReserved: Literal[0] ) -> hints.Hresult: ... def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... def SetPath(self, pszFile: bytes) -> hints.Hresult: ... @@ -278,7 +278,7 @@ def ShowCmd(self) -> int: ... def ShowCmd(self, piShowCmd: int) -> None: ... def SetIconLocation(self, pszIconPath: str, iIcon: int) -> hints.Hresult: ... def SetRelativePath( - self, pszPathRel: str, dwReserved: hints.Literal[0] + self, pszPathRel: str, dwReserved: Literal[0] ) -> hints.Hresult: ... def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... def SetPath(self, pszFile: str) -> hints.Hresult: ... diff --git a/comtypes/tools/codegenerator/typeannotator.py b/comtypes/tools/codegenerator/typeannotator.py index 7a6aef72..efd617fc 100644 --- a/comtypes/tools/codegenerator/typeannotator.py +++ b/comtypes/tools/codegenerator/typeannotator.py @@ -1,19 +1,19 @@ import abc import keyword -from typing import Any, Generic, TypeVar, TYPE_CHECKING +from typing import Any, Generic, Protocol, TypeVar from typing import Dict, List, Optional, Sequence, Tuple from typing import Iterable, Iterator from comtypes.tools import typedesc -if TYPE_CHECKING: - from comtypes import hints +class _MethodTypeDesc(Protocol): + arguments: List[Tuple[Any, str, List[str], Optional[Any]]] + idlflags: List[str] + name: str -if TYPE_CHECKING: - _T_MTD = TypeVar("_T_MTD", bound=hints._MethodTypeDesc) -else: - _T_MTD = TypeVar("_T_MTD") + +_T_MTD = TypeVar("_T_MTD", bound=_MethodTypeDesc) class _MethodAnnotator(abc.ABC, Generic[_T_MTD]):