Skip to content

[libclang/python] Simplify __eq__ operators #140540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

DeinAlptraum
Copy link
Contributor

This allows us to remove a few type: ignores.

This allows us to remove a few type: ignores
@DeinAlptraum DeinAlptraum requested a review from Endilll May 19, 2025 13:37
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:as-a-library libclang and C++ API labels May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

This allows us to remove a few type: ignores.


Full diff: https://github.com/llvm/llvm-project/pull/140540.diff

1 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+3-9)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 6f7243cdf80ac..a9f69309f4f67 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -335,9 +335,7 @@ def is_in_system_header(self):
         return conf.lib.clang_Location_isInSystemHeader(self)  # type: ignore [no-any-return]
 
     def __eq__(self, other):
-        if not isinstance(other, SourceLocation):
-            return False
-        return conf.lib.clang_equalLocations(self, other)  # type: ignore [no-any-return]
+        return isinstance(other, SourceLocation) and conf.lib.clang_equalLocations(self, other)
 
     def __ne__(self, other):
         return not self.__eq__(other)
@@ -395,9 +393,7 @@ def end(self):
         return conf.lib.clang_getRangeEnd(self)  # type: ignore [no-any-return]
 
     def __eq__(self, other):
-        if not isinstance(other, SourceRange):
-            return False
-        return conf.lib.clang_equalRanges(self, other)  # type: ignore [no-any-return]
+        return isinstance(other, SourceRange) and conf.lib.clang_equalRanges(self, other)
 
     def __ne__(self, other):
         return not self.__eq__(other)
@@ -1599,9 +1595,7 @@ def from_location(tu: TranslationUnit, location: SourceLocation) -> Cursor | Non
 
     # This function is not null-guarded because it is used in cursor_null_guard itself
     def __eq__(self, other: object) -> bool:
-        if not isinstance(other, Cursor):
-            return False
-        return conf.lib.clang_equalCursors(self, other)  # type: ignore [no-any-return]
+        return isinstance(other, Cursor) and conf.lib.clang_equalCursors(self, other)
 
     # Not null-guarded for consistency with __eq__
     def __ne__(self, other: object) -> bool:

Copy link

github-actions bot commented May 19, 2025

✅ With the latest revision this PR passed the Python code formatter.

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the diff show that you add __ne__ operator, even though it's already present? What am I missing?

def __eq__(self, other):
if not isinstance(other, SourceRange):
return False
return conf.lib.clang_equalRanges(self, other) # type: ignore [no-any-return]
def __ne__(self, other):
return not self.__eq__(other)

@DeinAlptraum
Copy link
Contributor Author

@Endilll I don't see the __ne__ operator being addded in the diff
If you are referring to the "Fix formatting again, again" commit, that just readds the operator after accidentally removing it in the "Fix formatting" commit (I had some issues with my editor yesterday)

@Endilll
Copy link
Contributor

Endilll commented May 20, 2025

Indeed, I was looking at that commit instead of all changes.

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we no longer need # type: ignore [no-any-return]?

@DeinAlptraum
Copy link
Contributor Author

DeinAlptraum commented May 20, 2025

The # type: ignore [no-any-return] are added in cases where the return type cannot be inferred. In our case, when the libclang library functions are called on the conf.lib object, since they are added dynamically so the type checker can only assume they return Any. By combining the library call with the isinstance check, even without knowing the type of the library functions it becomes obvious that the result of a boolean expression is returned, so the type-ignore becomes unnecessary in those spots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:as-a-library libclang and C++ API clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants