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

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Mon May 12 22:08:06 PDT 2025


================
@@ -1913,7 +1916,7 @@ def type(self):
         return self._type
 
     @property
-    def canonical(self):
+    def canonical(self) -> Cursor | None:
         """Return the canonical Cursor corresponding to this Cursor.
----------------
DeinAlptraum wrote:

Okay, I did find a way to do this after all. I made the `Config` object keep a null-cursor instance on hand and use this to implement a `Cursor.is_null` (rather than calling `cursor_getNullCursor` every time), and added a guard that assert that the cursor is not null on all method and property calls.

@Endilll I don't entirely understand what you mean by 
> should drop | None part of type hint

considering
> Instead of the assert you propose, I think all Cursor methods should assert that self is not a null cursor,

Unless we change the annotation of `Cursor.from_cursor_result` to return type `Cursor` or add an assert or similar inside the `canonical` property that the return is not `None`, the type checker won't except that the return type of `canonical` is just `Cursor`

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


More information about the cfe-commits mailing list