llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Miko (mikomikotaishi) <details> <summary>Changes</summary> This PR adds type declarations to Python and applies fstrings. --- Patch is 53.72 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/173845.diff 1 Files Affected: - (modified) clang/bindings/python/clang/cindex.py (+285-286) ``````````diff diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index 2b6ab00c88219..bc2dad9ea19b0 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -84,8 +84,10 @@ import os import sys from enum import Enum +from os import PathLike from typing import ( + IO, Any, Callable, cast as Tcast, @@ -93,6 +95,7 @@ Iterator, Literal, Optional, + Self, Sequence, Type as TType, TypeVar, @@ -142,7 +145,7 @@ def value(self) -> str | None: # type: ignore [override] return val.decode("utf8") @classmethod - def from_param(cls, param: str | bytes | None) -> c_interop_string: + def from_param(cls: TType["c_interop_string"], param: str | bytes | None) -> c_interop_string: if isinstance(param, str): return cls(param) if isinstance(param, bytes): @@ -196,28 +199,26 @@ class TranslationUnitSaveError(Exception): # Indicates that an unknown error occurred. This typically indicates that # I/O failed during save. - ERROR_UNKNOWN = 1 + ERROR_UNKNOWN: int = 1 # Indicates that errors during translation prevented saving. The errors # should be available via the TranslationUnit's diagnostics. - ERROR_TRANSLATION_ERRORS = 2 - + ERROR_TRANSLATION_ERRORS: int = 2 # Indicates that the translation unit was somehow invalid. - ERROR_INVALID_TU = 3 + ERROR_INVALID_TU: int = 3 - def __init__(self, enumeration, message): + def __init__(self, enumeration: int, message: str) -> None: assert isinstance(enumeration, int) if enumeration < 1 or enumeration > 3: raise Exception( - "Encountered undefined TranslationUnit save error " - "constant: %d. Please file a bug to have this " - "value supported." % enumeration + f"Encountered undefined TranslationUnit save error " + f"constant: {enumeration}. Please file a bug to have this " + f"value supported." ) - self.save_error = enumeration - Exception.__init__(self, "Error %d: %s" % (enumeration, message)) - + self.save_error: int = enumeration + Exception.__init__(self, f"Error {enumeration}: {message}") ### Structures and Utility Classes ### @@ -234,9 +235,9 @@ class CachedProperty(Generic[TInstance, TResult]): """ def __init__(self, wrapped: Callable[[TInstance], TResult]): - self.wrapped = wrapped + self.wrapped: Callable[[TInstance], TResult] = wrapped try: - self.__doc__ = wrapped.__doc__ + self.__doc__: str | None = wrapped.__doc__ except: pass @@ -248,7 +249,7 @@ def __get__(self, instance: TInstance, instance_type: Any = None) -> TResult: f"'{property_name}' is not a static attribute of '{class_name}'" ) - value = self.wrapped(instance) + value: TResult = self.wrapped(instance) setattr(instance, self.wrapped.__name__, value) return value @@ -277,9 +278,9 @@ class SourceLocation(Structure): """ _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)] - _data = None + _data: tuple[File | None, int, int, int] | None = None - def _get_instantiation(self): + def _get_instantiation(self) -> tuple[File | None, int, int, int]: if self._data is None: f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint() conf.lib.clang_getInstantiationLocation( @@ -293,7 +294,7 @@ def _get_instantiation(self): return self._data @staticmethod - def from_position(tu, file, line, column): + def from_position(tu: TranslationUnit, file: File, line: int, column: int) -> SourceLocation: """ Retrieve the source location associated with a given file/line/column in a particular translation unit. @@ -301,7 +302,7 @@ def from_position(tu, file, line, column): return conf.lib.clang_getLocation(tu, file, line, column) # type: ignore [no-any-return] @staticmethod - def from_offset(tu, file, offset): + def from_offset(tu: TranslationUnit, file: File, offset: int) -> SourceLocation: """Retrieve a SourceLocation from a given character offset. tu -- TranslationUnit file belongs to @@ -311,36 +312,36 @@ def from_offset(tu, file, offset): return conf.lib.clang_getLocationForOffset(tu, file, offset) # type: ignore [no-any-return] @property - def file(self): + def file(self) -> File | None: """Get the file represented by this source location.""" return self._get_instantiation()[0] @property - def line(self): + def line(self) -> int: """Get the line represented by this source location.""" return self._get_instantiation()[1] @property - def column(self): + def column(self) -> int: """Get the column represented by this source location.""" return self._get_instantiation()[2] @property - def offset(self): + def offset(self) -> int: """Get the file offset represented by this source location.""" return self._get_instantiation()[3] @property - def is_in_system_header(self): + def is_in_system_header(self) -> bool: """Returns true if the given source location is in a system header.""" return bool(conf.lib.clang_Location_isInSystemHeader(self)) - def __eq__(self, other): + def __eq__(self, other: object) -> bool: return isinstance(other, SourceLocation) and bool( conf.lib.clang_equalLocations(self, other) ) - def __ne__(self, other): + def __ne__(self, other: object) -> bool: return not self.__eq__(other) def __lt__(self, other: SourceLocation) -> bool: @@ -349,16 +350,12 @@ def __lt__(self, other: SourceLocation) -> bool: def __le__(self, other: SourceLocation) -> bool: return self < other or self == other - def __repr__(self): + def __repr__(self) -> str: if self.file: filename = self.file.name else: filename = None - return "<SourceLocation file %r, line %r, column %r>" % ( - filename, - self.line, - self.column, - ) + return f"<SourceLocation file {filename!r}, line {self.line!r}, column {self.column!r}>" class SourceRange(Structure): @@ -376,11 +373,11 @@ class SourceRange(Structure): # FIXME: Eliminate this and make normal constructor? Requires hiding ctypes # object. @staticmethod - def from_locations(start, end): + def from_locations(start: SourceLocation, end: SourceLocation) -> SourceRange: return conf.lib.clang_getRange(start, end) # type: ignore [no-any-return] @property - def start(self): + def start(self) -> SourceLocation: """ Return a SourceLocation representing the first character within a source range. @@ -388,29 +385,29 @@ def start(self): return conf.lib.clang_getRangeStart(self) # type: ignore [no-any-return] @property - def end(self): + def end(self) -> SourceLocation: """ Return a SourceLocation representing the last character within a source range. """ return conf.lib.clang_getRangeEnd(self) # type: ignore [no-any-return] - def __eq__(self, other): + def __eq__(self, other: object) -> bool: return isinstance(other, SourceRange) and bool( conf.lib.clang_equalRanges(self, other) ) - def __ne__(self, other): + def __ne__(self, other: object) -> bool: return not self.__eq__(other) - def __contains__(self, other): + def __contains__(self, other: object) -> bool: """Useful to detect the Token/Lexer bug""" if not isinstance(other, SourceLocation): return False return self.start <= other <= self.end - def __repr__(self): - return "<SourceRange start %r, end %r>" % (self.start, self.end) + def __repr__(self) -> str: + return f"<SourceRange start {self.start!r}, end {self.end!r}>" class Diagnostic: @@ -420,43 +417,43 @@ class Diagnostic: well as additional source ranges and associated fix-it hints. """ - Ignored = 0 - Note = 1 - Warning = 2 - Error = 3 - Fatal = 4 + Ignored: int = 0 + Note: int = 1 + Warning: int = 2 + Error: int = 3 + Fatal: int = 4 - DisplaySourceLocation = 0x01 - DisplayColumn = 0x02 - DisplaySourceRanges = 0x04 - DisplayOption = 0x08 - DisplayCategoryId = 0x10 - DisplayCategoryName = 0x20 - _FormatOptionsMask = 0x3F + DisplaySourceLocation: int = 0x01 + DisplayColumn: int = 0x02 + DisplaySourceRanges: int = 0x04 + DisplayOption: int = 0x08 + DisplayCategoryId: int = 0x10 + DisplayCategoryName: int = 0x20 + _FormatOptionsMask: int = 0x3F - def __init__(self, ptr): - self.ptr = ptr + def __init__(self, ptr: c_void_p) -> None: + self.ptr: c_void_p = ptr - def __del__(self): + def __del__(self) -> None: conf.lib.clang_disposeDiagnostic(self) @property - def severity(self): + def severity(self) -> int: return conf.lib.clang_getDiagnosticSeverity(self) # type: ignore [no-any-return] @property - def location(self): + def location(self) -> SourceLocation: return conf.lib.clang_getDiagnosticLocation(self) # type: ignore [no-any-return] @property - def spelling(self): + def spelling(self) -> str: return _CXString.from_result(conf.lib.clang_getDiagnosticSpelling(self)) @property def ranges(self) -> NoSliceSequence[SourceRange]: class RangeIterator: - def __init__(self, diag: Diagnostic): - self.diag = diag + def __init__(self, diag: Diagnostic) -> None: + self.diag: Diagnostic = diag def __len__(self) -> int: return int(conf.lib.clang_getDiagnosticNumRanges(self.diag)) @@ -471,15 +468,15 @@ def __getitem__(self, key: int) -> SourceRange: @property def fixits(self) -> NoSliceSequence[FixIt]: class FixItIterator: - def __init__(self, diag: Diagnostic): - self.diag = diag + def __init__(self, diag: Diagnostic) -> None: + self.diag: Diagnostic = diag def __len__(self) -> int: return int(conf.lib.clang_getDiagnosticNumFixIts(self.diag)) def __getitem__(self, key: int) -> FixIt: - range = SourceRange() - value = _CXString.from_result( + range: SourceRange = SourceRange() + value: str = _CXString.from_result( conf.lib.clang_getDiagnosticFixIt(self.diag, key, byref(range)) ) if len(value) == 0: @@ -492,14 +489,14 @@ def __getitem__(self, key: int) -> FixIt: @property def children(self) -> NoSliceSequence[Diagnostic]: class ChildDiagnosticsIterator: - def __init__(self, diag: Diagnostic): - self.diag_set = conf.lib.clang_getChildDiagnostics(diag) + def __init__(self, diag: Diagnostic) -> None: + self.diag_set: c_void_p = conf.lib.clang_getChildDiagnostics(diag) def __len__(self) -> int: return int(conf.lib.clang_getNumDiagnosticsInSet(self.diag_set)) def __getitem__(self, key: int) -> Diagnostic: - diag = conf.lib.clang_getDiagnosticInSet(self.diag_set, key) + diag: Any = conf.lib.clang_getDiagnosticInSet(self.diag_set, key) if not diag: raise IndexError return Diagnostic(diag) @@ -507,28 +504,28 @@ def __getitem__(self, key: int) -> Diagnostic: return ChildDiagnosticsIterator(self) @property - def category_number(self): + def category_number(self) -> int: """The category number for this diagnostic or 0 if unavailable.""" return conf.lib.clang_getDiagnosticCategory(self) # type: ignore [no-any-return] @property - def category_name(self): + def category_name(self) -> str: """The string name of the category for this diagnostic.""" return _CXString.from_result(conf.lib.clang_getDiagnosticCategoryText(self)) @property - def option(self): + def option(self) -> str: """The command-line option that enables this diagnostic.""" return _CXString.from_result(conf.lib.clang_getDiagnosticOption(self, None)) @property - def disable_option(self): + def disable_option(self) -> str: """The command-line option that disables this diagnostic.""" - disable = _CXString() + disable: _CXString = _CXString() conf.lib.clang_getDiagnosticOption(self, byref(disable)) return _CXString.from_result(disable) - def format(self, options=None): + def format(self, options: int | None = None) -> str: """ Format this diagnostic for display. The options argument takes Diagnostic.Display* flags, which can be combined using bitwise OR. If @@ -537,21 +534,17 @@ def format(self, options=None): """ if options is None: options = conf.lib.clang_defaultDiagnosticDisplayOptions() - if options & ~Diagnostic._FormatOptionsMask: + if options and options & ~Diagnostic._FormatOptionsMask: raise ValueError("Invalid format options") return _CXString.from_result(conf.lib.clang_formatDiagnostic(self, options)) - def __repr__(self): - return "<Diagnostic severity %r, location %r, spelling %r>" % ( - self.severity, - self.location, - self.spelling, - ) + def __repr__(self) -> str: + return f"<Diagnostic severity {self.severity!r}, location {self.location!r}, spelling {self.spelling!r}>" - def __str__(self): + def __str__(self) -> str: return self.format() - def from_param(self): + def from_param(self) -> c_void_p: return self.ptr @@ -562,12 +555,12 @@ class FixIt: with the given value. """ - def __init__(self, range, value): - self.range = range - self.value = value + def __init__(self, range: SourceRange, value: str) -> None: + self.range: SourceRange = range + self.value: str = value - def __repr__(self): - return "<FixIt range %r, value %r>" % (self.range, self.value) + def __repr__(self) -> str: + return f"<FixIt range {self.range!r}, value {self.value!r}>" class TokenGroup: @@ -585,39 +578,39 @@ class TokenGroup: You should not instantiate this class outside of this module. """ - def __init__(self, tu, memory, count): - self._tu = tu - self._memory = memory - self._count = count + def __init__(self, tu: TranslationUnit, memory: Any, count: c_uint) -> None: + self._tu: TranslationUnit = tu + self._memory: Any = memory + self._count: c_uint = count - def __del__(self): + def __del__(self) -> None: conf.lib.clang_disposeTokens(self._tu, self._memory, self._count) @staticmethod - def get_tokens(tu, extent): + def get_tokens(tu: TranslationUnit, extent: Any) -> Iterator[Token]: """Helper method to return all tokens in an extent. This functionality is needed multiple places in this module. We define it here because it seems like a logical place. """ tokens_memory = POINTER(Token)() - tokens_count = c_uint() + tokens_count: c_uint = c_uint() conf.lib.clang_tokenize(tu, extent, byref(tokens_memory), byref(tokens_count)) - count = int(tokens_count.value) + count: int = int(tokens_count.value) # If we get no tokens, no memory was allocated. Be sure not to return # anything and potentially call a destructor on nothing. if count < 1: return - tokens_array = cast(tokens_memory, POINTER(Token * count)).contents + tokens_array: Array[Token] = cast(tokens_memory, POINTER(Token * count)).contents - token_group = TokenGroup(tu, tokens_memory, tokens_count) + token_group: TokenGroup = TokenGroup(tu, tokens_memory, tokens_count) for i in range(0, count): - token = Token() + token: Token = Token() token.int_data = tokens_array[i].int_data token.ptr_data = tokens_array[i].ptr_data token._tu = tu @@ -633,25 +626,21 @@ class BaseEnumeration(Enum): """ - def from_param(self): + def from_param(self) -> int: return self.value @classmethod - def from_id(cls, id): + def from_id(cls: type[Self], id: int) -> Self: return cls(id) - def __repr__(self): - return "%s.%s" % ( - self.__class__.__name__, - self.name, - ) - + def __repr__(self) -> str: + return f"{self.__class__.__name__}.{self.name}" class TokenKind(BaseEnumeration): """Describes a specific type of a Token.""" @classmethod - def from_value(cls, value): + def from_value(cls: type[Self], value: int) -> Self: """Obtain a registered TokenKind instance from its value.""" return cls.from_id(value) @@ -668,43 +657,43 @@ class CursorKind(BaseEnumeration): """ @staticmethod - def get_all_kinds(): + def get_all_kinds() -> list[CursorKind]: """Return all CursorKind enumeration instances.""" return list(CursorKind) - def is_declaration(self): + def is_declaration(self) -> bool: """Test if this is a declaration kind.""" return bool(conf.lib.clang_isDeclaration(self)) - def is_reference(self): + def is_reference(self) -> bool: """Test if this is a reference kind.""" return bool(conf.lib.clang_isReference(self)) - def is_expression(self): + def is_expression(self) -> bool: """Test if this is an expression kind.""" return bool(conf.lib.clang_isExpression(self)) - def is_statement(self): + def is_statement(self) -> bool: """Test if this is a statement kind.""" return bool(conf.lib.clang_isStatement(self)) - def is_attribute(self): + def is_attribute(self) -> bool: """Test if this is an attribute kind.""" return bool(conf.lib.clang_isAttribute(self)) - def is_invalid(self): + def is_invalid(self) -> bool: """Test if this is an invalid kind.""" return bool(conf.lib.clang_isInvalid(self)) - def is_translation_unit(self): + def is_translation_unit(self) -> bool: """Test if this is a translation unit kind.""" return bool(conf.lib.clang_isTranslationUnit(self)) - def is_preprocessing(self): + def is_preprocessing(self) -> bool: """Test if this is a preprocessing kind.""" return bool(conf.lib.clang_isPreprocessing(self)) - def is_unexposed(self): + def is_unexposed(self) -> bool: """Test if this is an unexposed kind.""" return bool(conf.lib.clang_isUnexposed(self)) @@ -1624,7 +1613,7 @@ def cursor_null_guard(func): calling its `is_null` method. """ - def inner(self, *args, **kwargs): + def inner(self, *args: tuple[Any, ...], **kwargs: dict[str, Any]) -> Any: if self.is_null(): raise Exception("Tried calling method on a null-cursor.") return func(self, *args, **kwargs) @@ -2386,7 +2375,7 @@ def from_result(res: Cursor, arg: Cursor | TranslationUnit | Type) -> Cursor | N # Store a reference to the TU in the Python object so it won't get GC'd # before the Cursor. - tu = None + tu: TranslationUnit | None = None if isinstance(arg, TranslationUnit): tu = arg elif hasattr(arg, "translation_unit"): @@ -2420,12 +2409,12 @@ class BinaryOperator(BaseEnumeration): Describes the BinaryOperator of a declaration """ - def __nonzero__(self): + def __nonzero__(self) -> bool: ... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/173845 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
