<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/76664>76664</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang/python] Add type annotation to libclang Python binding
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
linux4life798
</td>
</tr>
</table>
<pre>
The libclang Python binding needs type annotations. Type annotations are important for libraries because they enable the IDE to help users learn the API and steer them in the correct direction through code completions. Furthermore, they allow the IDEs + static checkers to identify a large range of misuse errors.
This topic has been discussed a bit in [DeinAlptraum's conversation on Discourse](https://discourse.llvm.org/t/type-annotations-for-libclang-python-bindings/70644).
---
There are a few issues that would need to be addressed before we can simply merge type annotations into the python files.
1. The straightforward way to implement type annotations requires Python 3, but the main [cindex.py still makes mention of Python 2](https://github.com/llvm/llvm-project/blob/90c397fc56b7a04dd53cdad8103de1ead9686104/clang/bindings/python/clang/cindex.py#L113-L124). We should be dropping Python 2 support.
2. Type annotations have evolved a bit throughout Python 3 (ex. shift from using `List[str]` to `list[str]` OR `Optional[str]` to `str | None`). Using the newer syntax/style may make the library incompatible with earlier versions of Python 3. So, we need to figure out a reasonable Python 3 version cutoff and implement CI checks to ensure compatibility with the earliest version we want to support.
Python 3.7 might be a good cutoff. This aligns with [Debian buster](https://wiki.debian.org/Python#Supported_Python_Versions).
Helpful Resources:
* https://discourse.llvm.org/t/type-annotations-for-libclang-python-bindings/70644/9
* https://typing.readthedocs.io/en/latest/source/best_practices.html
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VsFy2zYQ_RrosiMOBYqUeNDBjqppZjJNJknbYwYEliRqEGCxoGX-fQegJCeqc-ihMzZpA8Tu2337HimIdGcRD6x8ZOVxJabQO38w2k4vW6Nb3NX7VePUfPjaIxjdSCNsB5_m0DsLjbZK2w4soiII84ggrHVBBO0sZfD1bgWER9DD6Hw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