71
71
from typing import (
72
72
Any ,
73
73
Callable ,
74
+ cast as Tcast ,
74
75
Generic ,
75
76
Optional ,
77
+ Sequence ,
76
78
Type as TType ,
77
79
TypeVar ,
78
80
TYPE_CHECKING ,
@@ -314,6 +316,8 @@ def is_in_system_header(self):
314
316
return conf .lib .clang_Location_isInSystemHeader (self ) # type: ignore [no-any-return]
315
317
316
318
def __eq__ (self , other ):
319
+ if not isinstance (other , SourceLocation ):
320
+ return False
317
321
return conf .lib .clang_equalLocations (self , other ) # type: ignore [no-any-return]
318
322
319
323
def __ne__ (self , other ):
@@ -372,6 +376,8 @@ def end(self):
372
376
return conf .lib .clang_getRangeEnd (self ) # type: ignore [no-any-return]
373
377
374
378
def __eq__ (self , other ):
379
+ if not isinstance (other , SourceRange ):
380
+ return False
375
381
return conf .lib .clang_equalRanges (self , other ) # type: ignore [no-any-return]
376
382
377
383
def __ne__ (self , other ):
@@ -1556,6 +1562,8 @@ def from_location(tu, location):
1556
1562
return cursor
1557
1563
1558
1564
def __eq__ (self , other ):
1565
+ if not isinstance (other , Cursor ):
1566
+ return False
1559
1567
return conf .lib .clang_equalCursors (self , other ) # type: ignore [no-any-return]
1560
1568
1561
1569
def __ne__ (self , other ):
@@ -1746,7 +1754,7 @@ def get_definition(self):
1746
1754
1747
1755
def get_usr (self ):
1748
1756
"""Return the Unified Symbol Resolution (USR) for the entity referenced
1749
- by the given cursor (or None) .
1757
+ by the given cursor.
1750
1758
1751
1759
A Unified Symbol Resolution (USR) is a string that identifies a
1752
1760
particular entity (function, class, variable, etc.) within a
@@ -2776,7 +2784,7 @@ def pretty_printed(self, policy):
2776
2784
return _CXString .from_result (conf .lib .clang_getTypePrettyPrinted (self , policy ))
2777
2785
2778
2786
def __eq__ (self , other ):
2779
- if type (other ) != type ( self ):
2787
+ if not isinstance (other , Type ):
2780
2788
return False
2781
2789
2782
2790
return conf .lib .clang_equalTypes (self , other ) # type: ignore [no-any-return]
@@ -2886,10 +2894,9 @@ def kind(self):
2886
2894
def string (self ):
2887
2895
res = conf .lib .clang_getCompletionChunkCompletionString (self .cs , self .key )
2888
2896
2889
- if res :
2890
- return CompletionString (res )
2891
- else :
2892
- None
2897
+ if not res :
2898
+ return None
2899
+ return CompletionString (res )
2893
2900
2894
2901
def isKindOptional (self ):
2895
2902
return self .__kindNumber == 0
@@ -2955,6 +2962,13 @@ def __getitem__(self, key):
2955
2962
raise IndexError
2956
2963
return CompletionChunk (self .obj , key )
2957
2964
2965
+ if TYPE_CHECKING :
2966
+ # Defining __getitem__ and __len__ is enough to make an iterable
2967
+ # but the typechecker doesn't understand that.
2968
+ def __iter__ (self ):
2969
+ for i in range (len (self )):
2970
+ yield self [i ]
2971
+
2958
2972
@property
2959
2973
def priority (self ):
2960
2974
return conf .lib .clang_getCompletionPriority (self .obj ) # type: ignore [no-any-return]
@@ -2970,7 +2984,7 @@ def briefComment(self):
2970
2984
return _CXString .from_result (
2971
2985
conf .lib .clang_getCompletionBriefComment (self .obj )
2972
2986
)
2973
- return _CXString ()
2987
+ return ""
2974
2988
2975
2989
def __repr__ (self ):
2976
2990
return (
@@ -3155,8 +3169,8 @@ def from_source(
3155
3169
a list via args. These can be used to specify include paths, warnings,
3156
3170
etc. e.g. ["-Wall", "-I/path/to/include"].
3157
3171
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
3172
+ In-memory file content can be provided via unsaved_files. This is a
3173
+ list of 2-tuples. The first element is the filename (str or
3160
3174
PathLike). The second element defines the content. Content can be
3161
3175
provided as str source code or as file objects (anything with a read()
3162
3176
method). If a file object is being used, content will be read until EOF
@@ -3328,13 +3342,15 @@ def get_extent(self, filename, locations):
3328
3342
start_location , end_location = locations
3329
3343
3330
3344
if hasattr (start_location , "__len__" ):
3345
+ start_location = Tcast (Sequence [int ], start_location )
3331
3346
start_location = SourceLocation .from_position (
3332
3347
self , f , start_location [0 ], start_location [1 ]
3333
3348
)
3334
3349
elif isinstance (start_location , int ):
3335
3350
start_location = SourceLocation .from_offset (self , f , start_location )
3336
3351
3337
3352
if hasattr (end_location , "__len__" ):
3353
+ end_location = Tcast (Sequence [int ], end_location )
3338
3354
end_location = SourceLocation .from_position (
3339
3355
self , f , end_location [0 ], end_location [1 ]
3340
3356
)
@@ -3464,6 +3480,8 @@ def get_tokens(self, locations=None, extent=None):
3464
3480
2-tuple of SourceLocation or as a SourceRange. If both are defined,
3465
3481
behavior is undefined.
3466
3482
"""
3483
+ if locations is None and extent is None :
3484
+ raise TypeError ("get_tokens() requires at least one argument" )
3467
3485
if locations is not None :
3468
3486
extent = SourceRange (start = locations [0 ], end = locations [1 ])
3469
3487
@@ -3510,11 +3528,11 @@ def __ne__(self, other) -> bool:
3510
3528
@staticmethod
3511
3529
def from_result (res , arg ):
3512
3530
assert isinstance (res , c_object_p )
3513
- res = File (res )
3531
+ file = File (res )
3514
3532
3515
3533
# Copy a reference to the TranslationUnit to prevent premature GC.
3516
- res ._tu = arg ._tu
3517
- return res
3534
+ file ._tu = arg ._tu
3535
+ return file
3518
3536
3519
3537
3520
3538
class FileInclusion :
@@ -3593,7 +3611,7 @@ def filename(self):
3593
3611
def arguments (self ):
3594
3612
"""
3595
3613
Get an iterable object providing each argument in the
3596
- command line for the compiler invocation as a _CXString .
3614
+ command line for the compiler invocation as a string .
3597
3615
3598
3616
Invariant : the first argument is the compiler executable
3599
3617
"""
0 commit comments