[clang] [libclang/python] Simplify __eq__ operators (PR #140540)
Jannick Kremer via cfe-commits
cfe-commits at lists.llvm.org
Mon May 19 06:52:30 PDT 2025
https://github.com/DeinAlptraum updated https://github.com/llvm/llvm-project/pull/140540
>From 51df5f4eb979edb7cc761276fe846626641bd6b4 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Mon, 19 May 2025 22:36:05 +0900
Subject: [PATCH 1/3] [libclang/python] Simplfy __eq__ operators This allows us
to remove a few type: ignores
---
clang/bindings/python/clang/cindex.py | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)
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:
>From 0fd964c7dcc7a1ecb18f62c59a543c0676f4343e Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Mon, 19 May 2025 22:45:53 +0900
Subject: [PATCH 2/3] Fix formatting
---
clang/bindings/python/clang/cindex.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index a9f69309f4f67..fd3bdf5cc26b5 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -395,7 +395,9 @@ def end(self):
def __eq__(self, other):
return isinstance(other, SourceRange) and conf.lib.clang_equalRanges(self, other)
- def __ne__(self, other):
+ return isinstance(other, SourceRange) and conf.lib.clang_equalRanges(
+ self, other
+ )
return not self.__eq__(other)
def __contains__(self, other):
>From deea255a71bb1dd266bce393108530c3eac865e8 Mon Sep 17 00:00:00 2001
From: Jannick Kremer <jannick.kremer at mailbox.org>
Date: Mon, 19 May 2025 22:52:12 +0900
Subject: [PATCH 3/3] Fix formatting again
---
clang/bindings/python/clang/cindex.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index fd3bdf5cc26b5..29904728867db 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -335,7 +335,9 @@ def is_in_system_header(self):
return conf.lib.clang_Location_isInSystemHeader(self) # type: ignore [no-any-return]
def __eq__(self, other):
- return isinstance(other, SourceLocation) and conf.lib.clang_equalLocations(self, other)
+ return isinstance(other, SourceLocation) and conf.lib.clang_equalLocations(
+ self, other
+ )
def __ne__(self, other):
return not self.__eq__(other)
More information about the cfe-commits
mailing list