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

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Wed May 14 22:05:44 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:
----------------
DeinAlptraum wrote:

I don't really like either option to be honest.
For the `__getattribute__` approach, it would be easy for future contributors to overlook this if they implement new functionality on `Cursor` and run into unforeseen issues when they explicitly don't want a null-Cursor guard.
On the flipside, applying this manually as a decorator has the opposite problem, where it is easy to accidentally forget to add it when implementing a new function.

I've decorated all methods for now (except what's required for the null-check itself), but do you think there are any that we should leave out? 
Or on the contrary, should we only decorate those functions where this leads to issues with `from_result`? I'm afraid this might make the interface inconsistent though, in a way that's not very clear to the user 

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


More information about the cfe-commits mailing list