[clang] [libclang/python] Derive library function types from annotations (PR #142120)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 4 07:44:06 PDT 2025


================
@@ -3986,256 +3987,588 @@ def set_property(self, property, value):
 # Now comes the plumbing to hook up the C library.
 
 # Register callback types
-translation_unit_includes_callback = CFUNCTYPE(
+translation_unit_includes_callback: TypeAlias = CFUNCTYPE(  # type: ignore [valid-type]
     None, c_object_p, POINTER(SourceLocation), c_uint, py_object
 )
-cursor_visit_callback = CFUNCTYPE(c_int, Cursor, Cursor, py_object)
-fields_visit_callback = CFUNCTYPE(c_int, Cursor, py_object)
+cursor_visit_callback: TypeAlias = CFUNCTYPE(c_int, Cursor, Cursor, py_object)  # type: ignore [valid-type]
+fields_visit_callback: TypeAlias = CFUNCTYPE(c_int, Cursor, py_object)  # type: ignore [valid-type]
+
+
+def _get_annotations() -> list[LibFunc]:
+    def str_to_type(typename: str) -> type:
----------------
Endilll wrote:

Every line in this function feels very unfortunate. Maybe instead we use a different approach:
```python
class CPointer(_Pointer, typing.Generic):
  pass

def clang_CompilationDatabase_dispose(self, arg1: CPointer[c_void_p]) -> None: ...
```

then use `typing.get_args()` to get the argument passed to `CPointer`.

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


More information about the cfe-commits mailing list