Skip to content

Commit 4c90f65

Browse files
committed
[libclang/python] Add some logical typing changes.
1 parent ea0dbee commit 4c90f65

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@
7171
from typing import (
7272
Any,
7373
Callable,
74+
cast as Tcast,
7475
Generic,
7576
Optional,
77+
Sequence,
7678
Type as TType,
7779
TypeVar,
7880
TYPE_CHECKING,
@@ -314,6 +316,8 @@ def is_in_system_header(self):
314316
return conf.lib.clang_Location_isInSystemHeader(self) # type: ignore [no-any-return]
315317

316318
def __eq__(self, other):
319+
if not isinstance(other, SourceLocation):
320+
return False
317321
return conf.lib.clang_equalLocations(self, other) # type: ignore [no-any-return]
318322

319323
def __ne__(self, other):
@@ -372,6 +376,8 @@ def end(self):
372376
return conf.lib.clang_getRangeEnd(self) # type: ignore [no-any-return]
373377

374378
def __eq__(self, other):
379+
if not isinstance(other, SourceRange):
380+
return False
375381
return conf.lib.clang_equalRanges(self, other) # type: ignore [no-any-return]
376382

377383
def __ne__(self, other):
@@ -1556,6 +1562,8 @@ def from_location(tu, location):
15561562
return cursor
15571563

15581564
def __eq__(self, other):
1565+
if not isinstance(other, Cursor):
1566+
return False
15591567
return conf.lib.clang_equalCursors(self, other) # type: ignore [no-any-return]
15601568

15611569
def __ne__(self, other):
@@ -1746,7 +1754,7 @@ def get_definition(self):
17461754

17471755
def get_usr(self):
17481756
"""Return the Unified Symbol Resolution (USR) for the entity referenced
1749-
by the given cursor (or None).
1757+
by the given cursor.
17501758
17511759
A Unified Symbol Resolution (USR) is a string that identifies a
17521760
particular entity (function, class, variable, etc.) within a
@@ -2776,7 +2784,7 @@ def pretty_printed(self, policy):
27762784
return _CXString.from_result(conf.lib.clang_getTypePrettyPrinted(self, policy))
27772785

27782786
def __eq__(self, other):
2779-
if type(other) != type(self):
2787+
if not isinstance(other, Type):
27802788
return False
27812789

27822790
return conf.lib.clang_equalTypes(self, other) # type: ignore [no-any-return]
@@ -2888,8 +2896,7 @@ def string(self):
28882896

28892897
if res:
28902898
return CompletionString(res)
2891-
else:
2892-
None
2899+
return None
28932900

28942901
def isKindOptional(self):
28952902
return self.__kindNumber == 0
@@ -2955,6 +2962,10 @@ def __getitem__(self, key):
29552962
raise IndexError
29562963
return CompletionChunk(self.obj, key)
29572964

2965+
def __iter__(self):
2966+
for i in range(len(self)):
2967+
yield self[i]
2968+
29582969
@property
29592970
def priority(self):
29602971
return conf.lib.clang_getCompletionPriority(self.obj) # type: ignore [no-any-return]
@@ -2970,7 +2981,7 @@ def briefComment(self):
29702981
return _CXString.from_result(
29712982
conf.lib.clang_getCompletionBriefComment(self.obj)
29722983
)
2973-
return _CXString()
2984+
return ""
29742985

29752986
def __repr__(self):
29762987
return (
@@ -3155,8 +3166,8 @@ def from_source(
31553166
a list via args. These can be used to specify include paths, warnings,
31563167
etc. e.g. ["-Wall", "-I/path/to/include"].
31573168
3158-
In-memory file content can be provided via unsaved_files. This is an
3159-
iterable of 2-tuples. The first element is the filename (str or
3169+
In-memory file content can be provided via unsaved_files. This is a
3170+
list of 2-tuples. The first element is the filename (str or
31603171
PathLike). The second element defines the content. Content can be
31613172
provided as str source code or as file objects (anything with a read()
31623173
method). If a file object is being used, content will be read until EOF
@@ -3328,13 +3339,15 @@ def get_extent(self, filename, locations):
33283339
start_location, end_location = locations
33293340

33303341
if hasattr(start_location, "__len__"):
3342+
start_location = Tcast(Sequence[int], start_location)
33313343
start_location = SourceLocation.from_position(
33323344
self, f, start_location[0], start_location[1]
33333345
)
33343346
elif isinstance(start_location, int):
33353347
start_location = SourceLocation.from_offset(self, f, start_location)
33363348

33373349
if hasattr(end_location, "__len__"):
3350+
end_location = Tcast(Sequence[int], end_location)
33383351
end_location = SourceLocation.from_position(
33393352
self, f, end_location[0], end_location[1]
33403353
)
@@ -3466,6 +3479,8 @@ def get_tokens(self, locations=None, extent=None):
34663479
"""
34673480
if locations is not None:
34683481
extent = SourceRange(start=locations[0], end=locations[1])
3482+
if extent is None:
3483+
raise TypeError("get_tokens() requires at least one argument")
34693484

34703485
return TokenGroup.get_tokens(self, extent)
34713486

@@ -3502,11 +3517,11 @@ def __repr__(self):
35023517
@staticmethod
35033518
def from_result(res, arg):
35043519
assert isinstance(res, c_object_p)
3505-
res = File(res)
3520+
file = File(res)
35063521

35073522
# Copy a reference to the TranslationUnit to prevent premature GC.
3508-
res._tu = arg._tu
3509-
return res
3523+
file._tu = arg._tu
3524+
return file
35103525

35113526

35123527
class FileInclusion:
@@ -3585,7 +3600,7 @@ def filename(self):
35853600
def arguments(self):
35863601
"""
35873602
Get an iterable object providing each argument in the
3588-
command line for the compiler invocation as a _CXString.
3603+
command line for the compiler invocation as a string.
35893604
35903605
Invariant : the first argument is the compiler executable
35913606
"""

0 commit comments

Comments
 (0)