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

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Thu May 1 04:08:25 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:

This uses `Cursor.from_cursor_result` to create the return value. That function returns `None` if it is passed the Null-Cursor, leading to the `Cursor | None` return type.
That said, the input to it is `clang_getCanonicalCursor` which does not seem to ever return the Null-Cursor, so we could theoretically constrain the return type here to just `Cursor`, by asserting `isinstance(the_return_value, Cursor)`, but this is a bit ugly... what do you think?

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


More information about the cfe-commits mailing list