[clang] [libclang/python] Fix some type errors, add type annotations (PR #98745)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 13 07:18:03 PDT 2024


================
@@ -64,48 +64,81 @@
 
 from ctypes import *
 
-import collections.abc
 import os
+import sys
 from enum import Enum
 
+from typing import (
+    Any,
+    Callable,
+    Generic,
+    Optional,
+    Type as TType,
+    TypeVar,
+    TYPE_CHECKING,
+    Union as TUnion,
+)
+from typing_extensions import Protocol, TypeAlias
+
+if TYPE_CHECKING:
+    from ctypes import _Pointer
+
+    StrPath: TypeAlias = TUnion[str, os.PathLike[str]]
+    LibFunc: TypeAlias = TUnion[
+        "tuple[str, Optional[list[Any]]]",
+        "tuple[str, Optional[list[Any]], Any]",
+        "tuple[str, Optional[list[Any]], Any, Callable[..., Any]]",
+    ]
+    CObjP: TypeAlias = _Pointer[Any]
+
+    TSeq = TypeVar("TSeq", covariant=True)
+
+    class NoSliceSequence(Protocol[TSeq]):
+        def __len__(self) -> int:
+            ...
+
+        def __getitem__(self, key: int) -> TSeq:
+            ...
----------------
DeinAlptraum wrote:

This defines the proper interface for all the classes with "Iterator" in their name. None of them are actually iterators in terms of the Python protocol `Iterator`, as they are expected to support `__len__` and __getitem__`. They're the closest to the `Sequence` protocol, but without the support for slicing.

https://github.com/llvm/llvm-project/pull/98745


More information about the cfe-commits mailing list