Skip to content

Commit 9a6c0ea

Browse files
authored
[libclang/python] Add type annotations for code completion classes (#140539)
This fully annotates the code completion classes (CompletionChunk, CompletionString, CodeCompletionResult, CodeCompletionResults and CCRStructure) resolving 59 strict typing errors as the next step towards #76664
1 parent a3186be commit 9a6c0ea

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"tuple[str, Optional[list[Any]], Any]",
111111
"tuple[str, Optional[list[Any]], Any, Callable[..., Any]]",
112112
]
113+
CObjP: TypeAlias = _Pointer[Any]
113114

114115
TSeq = TypeVar("TSeq", covariant=True)
115116

@@ -2981,25 +2982,25 @@ class _CXUnsavedFile(Structure):
29812982

29822983
class CompletionChunk:
29832984
class Kind:
2984-
def __init__(self, name):
2985+
def __init__(self, name: str):
29852986
self.name = name
29862987

2987-
def __str__(self):
2988+
def __str__(self) -> str:
29882989
return self.name
29892990

2990-
def __repr__(self):
2991+
def __repr__(self) -> str:
29912992
return "<ChunkKind: %s>" % self
29922993

2993-
def __init__(self, completionString, key):
2994+
def __init__(self, completionString: CObjP, key: int):
29942995
self.cs = completionString
29952996
self.key = key
29962997
self.__kindNumberCache = -1
29972998

2998-
def __repr__(self):
2999+
def __repr__(self) -> str:
29993000
return "{'" + self.spelling + "', " + str(self.kind) + "}"
30003001

30013002
@CachedProperty
3002-
def spelling(self):
3003+
def spelling(self) -> str:
30033004
if self.__kindNumber in SPELLING_CACHE:
30043005
return SPELLING_CACHE[self.__kindNumber]
30053006
return _CXString.from_result(
@@ -3010,38 +3011,38 @@ def spelling(self):
30103011
# apparently still significantly faster. Please profile carefully if you
30113012
# would like to add CachedProperty back.
30123013
@property
3013-
def __kindNumber(self):
3014+
def __kindNumber(self) -> int:
30143015
if self.__kindNumberCache == -1:
30153016
self.__kindNumberCache = conf.lib.clang_getCompletionChunkKind(
30163017
self.cs, self.key
30173018
)
30183019
return self.__kindNumberCache
30193020

30203021
@CachedProperty
3021-
def kind(self):
3022+
def kind(self) -> Kind:
30223023
return completionChunkKindMap[self.__kindNumber]
30233024

30243025
@CachedProperty
3025-
def string(self):
3026+
def string(self) -> CompletionString | None:
30263027
res = conf.lib.clang_getCompletionChunkCompletionString(self.cs, self.key)
30273028

30283029
if not res:
30293030
return None
30303031
return CompletionString(res)
30313032

3032-
def isKindOptional(self):
3033+
def isKindOptional(self) -> bool:
30333034
return self.__kindNumber == 0
30343035

3035-
def isKindTypedText(self):
3036+
def isKindTypedText(self) -> bool:
30363037
return self.__kindNumber == 1
30373038

3038-
def isKindPlaceHolder(self):
3039+
def isKindPlaceHolder(self) -> bool:
30393040
return self.__kindNumber == 3
30403041

3041-
def isKindInformative(self):
3042+
def isKindInformative(self) -> bool:
30423043
return self.__kindNumber == 4
30433044

3044-
def isKindResultType(self):
3045+
def isKindResultType(self) -> bool:
30453046
return self.__kindNumber == 15
30463047

30473048

@@ -3081,39 +3082,39 @@ def __str__(self):
30813082
def __repr__(self):
30823083
return "<Availability: %s>" % self
30833084

3084-
def __len__(self):
3085+
def __len__(self) -> int:
30853086
return self.num_chunks
30863087

30873088
@CachedProperty
3088-
def num_chunks(self):
3089+
def num_chunks(self) -> int:
30893090
return conf.lib.clang_getNumCompletionChunks(self.obj) # type: ignore [no-any-return]
30903091

3091-
def __getitem__(self, key):
3092+
def __getitem__(self, key: int) -> CompletionChunk:
30923093
if self.num_chunks <= key:
30933094
raise IndexError
30943095
return CompletionChunk(self.obj, key)
30953096

30963097
if TYPE_CHECKING:
30973098
# Defining __getitem__ and __len__ is enough to make an iterable
30983099
# but the typechecker doesn't understand that.
3099-
def __iter__(self):
3100+
def __iter__(self) -> Iterator[CompletionChunk]:
31003101
for i in range(len(self)):
31013102
yield self[i]
31023103

31033104
@property
3104-
def priority(self):
3105+
def priority(self) -> int:
31053106
return conf.lib.clang_getCompletionPriority(self.obj) # type: ignore [no-any-return]
31063107

31073108
@property
3108-
def availability(self):
3109+
def availability(self) -> CompletionChunk.Kind:
31093110
res = conf.lib.clang_getCompletionAvailability(self.obj)
31103111
return availabilityKinds[res]
31113112

31123113
@property
3113-
def briefComment(self):
3114+
def briefComment(self) -> str:
31143115
return _CXString.from_result(conf.lib.clang_getCompletionBriefComment(self.obj))
31153116

3116-
def __repr__(self):
3117+
def __repr__(self) -> str:
31173118
return (
31183119
" | ".join([str(a) for a in self])
31193120
+ " || Priority: "
@@ -3136,44 +3137,47 @@ def __repr__(self):
31363137
class CodeCompletionResult(Structure):
31373138
_fields_ = [("cursorKind", c_int), ("completionString", c_object_p)]
31383139

3139-
def __repr__(self):
3140+
def __repr__(self) -> str:
31403141
return str(CompletionString(self.completionString))
31413142

31423143
@property
3143-
def kind(self):
3144+
def kind(self) -> CursorKind:
31443145
return CursorKind.from_id(self.cursorKind)
31453146

31463147
@property
3147-
def string(self):
3148+
def string(self) -> CompletionString:
31483149
return CompletionString(self.completionString)
31493150

31503151

31513152
class CCRStructure(Structure):
31523153
_fields_ = [("results", POINTER(CodeCompletionResult)), ("numResults", c_int)]
31533154

3154-
def __len__(self):
3155+
results: NoSliceSequence[CodeCompletionResult]
3156+
numResults: int
3157+
3158+
def __len__(self) -> int:
31553159
return self.numResults
31563160

3157-
def __getitem__(self, key):
3161+
def __getitem__(self, key: int) -> CodeCompletionResult:
31583162
if len(self) <= key:
31593163
raise IndexError
31603164

31613165
return self.results[key]
31623166

31633167

31643168
class CodeCompletionResults(ClangObject):
3165-
def __init__(self, ptr):
3169+
def __init__(self, ptr: _Pointer[CCRStructure]):
31663170
assert isinstance(ptr, POINTER(CCRStructure)) and ptr
31673171
self.ptr = self._as_parameter_ = ptr
31683172

3169-
def from_param(self):
3173+
def from_param(self) -> _Pointer[CCRStructure]:
31703174
return self._as_parameter_
31713175

3172-
def __del__(self):
3176+
def __del__(self) -> None:
31733177
conf.lib.clang_disposeCodeCompleteResults(self)
31743178

31753179
@property
3176-
def results(self):
3180+
def results(self) -> CCRStructure:
31773181
return self.ptr.contents
31783182

31793183
@property

0 commit comments

Comments
 (0)