[clang] [libclang/python] Add strict typing to clang Python bindings (#76664) (PR #78114)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 31 01:46:00 PDT 2024


================
@@ -3257,21 +3323,21 @@ def reparse(self, unsaved_files=None, options=0):
         if unsaved_files is None:
             unsaved_files = []
 
-        unsaved_files_array = 0
+        unsaved_files_array: int | Array[_CXUnsavedFile] = 0
         if len(unsaved_files):
             unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
             for i, (name, contents) in enumerate(unsaved_files):
                 if hasattr(contents, "read"):
                     contents = contents.read()
-                contents = b(contents)
+                binary_contents = b(contents)
----------------
DeinAlptraum wrote:

Assigning an incompatible type to a variable of a different type is a type error. Changing `binary_contents` back to `contents` produces a type error like this on that line:
`clang/cindex.py:3332: error: Incompatible types in assignment (expression has type "bytes", variable has type "str | TextIOWrapper")`
Good point with the repetition though, I'll make separate PRs for these to collect this into a common function.

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


More information about the cfe-commits mailing list