From 4e97372610d17f2bf5cb01999d6b1f19058f9617 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 May 2025 00:30:40 +0000 Subject: [PATCH 1/2] Fix: Add pyright ignores for CFFI dynamic attributes in re.py The `lib` object in `src/stdlib/re.py` is created via `cffi.FFI().dlopen()`. Pyright, as a static analyzer, cannot detect the attributes (`free`, `free_matches`) that CFFI adds dynamically at runtime from the C library. This commit adds `# pyright: ignore [reportAttributeAccessIssue]` comments to the lines where these C functions are accessed through the `lib` object. This silences the false positive errors reported by pyright, acknowledging that these attributes are indeed available when the code executes. --- src/stdlib/re.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stdlib/re.py b/src/stdlib/re.py index 3a87f43..c39ed99 100644 --- a/src/stdlib/re.py +++ b/src/stdlib/re.py @@ -50,7 +50,7 @@ def search(self, text: str) -> str | None: result_bytes = cast(bytes, ffi.string(result)) return result_bytes.decode("utf-8") finally: - lib.free(ptr) + lib.free(ptr) # pyright: ignore [reportAttributeAccessIssue] return None def findall(self, text: str) -> list[str]: @@ -68,7 +68,7 @@ def findall(self, text: str) -> list[str]: i += 1 finally: # Free the allocated memory - lib.free_matches(matches_ptr) + lib.free_matches(matches_ptr) # pyright: ignore [reportAttributeAccessIssue] return matches def sub(self, replacement: str, text: str) -> str: @@ -82,7 +82,7 @@ def sub(self, replacement: str, text: str) -> str: result_bytes = cast(bytes, ffi.string(result)) return result_bytes.decode("utf-8") finally: - lib.free(ptr) + lib.free(ptr) # pyright: ignore [reportAttributeAccessIssue] return text # Return the original text if substitution fails From d187ca4030f5559ecb28b397c0ba06dfcf0c102a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 May 2025 00:33:34 +0000 Subject: [PATCH 2/2] Refactor: Use generic type: ignore for CFFI attributes in re.py Updates the pyright-specific ignore comments to the more generic `# type: ignore[attr-defined]` for broader compatibility with different type checkers. This addresses the false positive errors from static analyzers that cannot detect attributes dynamically added by CFFI at runtime. The `[attr-defined]` code is suitable for Mypy and also recognized by Pyright for this kind of attribute access issue. --- src/stdlib/re.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stdlib/re.py b/src/stdlib/re.py index c39ed99..b94bcd2 100644 --- a/src/stdlib/re.py +++ b/src/stdlib/re.py @@ -50,7 +50,7 @@ def search(self, text: str) -> str | None: result_bytes = cast(bytes, ffi.string(result)) return result_bytes.decode("utf-8") finally: - lib.free(ptr) # pyright: ignore [reportAttributeAccessIssue] + lib.free(ptr) # type: ignore[attr-defined] return None def findall(self, text: str) -> list[str]: @@ -68,7 +68,7 @@ def findall(self, text: str) -> list[str]: i += 1 finally: # Free the allocated memory - lib.free_matches(matches_ptr) # pyright: ignore [reportAttributeAccessIssue] + lib.free_matches(matches_ptr) # type: ignore[attr-defined] return matches def sub(self, replacement: str, text: str) -> str: @@ -82,7 +82,7 @@ def sub(self, replacement: str, text: str) -> str: result_bytes = cast(bytes, ffi.string(result)) return result_bytes.decode("utf-8") finally: - lib.free(ptr) # pyright: ignore [reportAttributeAccessIssue] + lib.free(ptr) # type: ignore[attr-defined] return text # Return the original text if substitution fails