[clang] [libclang/python] Add type annotations to the TranslationUnit class (PR #180876)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 10 19:17:19 PST 2026


================
@@ -3490,11 +3504,12 @@ def from_source(
         etc. e.g. ["-Wall", "-I/path/to/include"].
 
         In-memory file content can be provided via unsaved_files. This is a
-        list of 2-tuples. The first element is the filename (str or
+        list of 2-tuples. The first element is the filename (str, bytes or
         PathLike). The second element defines the content. Content can be
-        provided as str source code or as file objects (anything with a read()
-        method). If a file object is being used, content will be read until EOF
-        and the read cursor will not be reset to its original position.
+        provided as str or bytes source code, or as file objects (anything with
+        a read() method). If a file object is being used, content will be read
+        until EOF and the read cursor will not be reset to its original
+        position.
----------------
DeinAlptraum wrote:

I wasn't entirely sure what to do with this part:
We usually just pass filename arguments into `os.fspath` for conversion, which works with `str`, `bytes` and any type that defines a `__fspath__` function that returns either `str` or `bytes`, but the doc comment here says that we only allow `str` and `PathLike`, not `bytes`. This has always worked with `bytes` too.

So we have the choices of
1. annotating this correctly as accepting `bytes` as well and adapt the doc accordingly
2. annotating correctly without adapting the doc (that it also works with `bytes` remains a secret feature one could infer from the type annotation)
3. adapting the type annotation to the doc to not accept `bytes` (that it also works with `bytes` remains a secret feature one could infer only from looking at the code, thought we do not guarantee any kind of support)

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


More information about the cfe-commits mailing list