[clang] [libclang/python] Add typing annotations for the Cursor class (PR #138103)

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Thu May 15 09:26:28 PDT 2025


================
@@ -1552,68 +1553,85 @@ class Cursor(Structure):
 
     _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)]
 
+    _tu: TranslationUnit
+
+    # This ensures that no operations are possible on null cursors
+    # by guarding all method calls with a not-null assert
+    def __getattribute__(self, key: str) -> object:
----------------
Endilll wrote:

> I've decorated all methods for now (except what's required for the null-check itself), 

Thank you, I like it better than `__getattribute__` solution.

> but do you think there are any that we should leave out?

I looked at all of them, and I don't think we need to leave out more than you already did. However, I found a source of null cursors, and left a comment there.

> Or on the contrary, should we only decorate those functions using any of the from_result methods? I'm afraid this might make the interface inconsistent though, in a way that's not very clear to the user

No, mapping of null cursors to `None` is an invariant for the whole `Cursor` class, so your new decorator should be put on almost anything. Maybe worth leaving comments on methods which are not decorated, to make it clear that it is a very intentional omission.

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


More information about the cfe-commits mailing list