[clang] 0f7791e - [clang][Python] Use str.format instead of string concatenations (#173861)
Vlad Serebrennikov via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 15 08:59:07 PST 2026
Author: Miko
Date: 2026-02-15T19:57:22+03:00
New Revision: 0f7791e9d82d7d40981eb1a781a61c1e988997fd
URL: https://github.com/llvm/llvm-project/commit/0f7791e9d82d7d40981eb1a781a61c1e988997fd
DIFF: https://github.com/llvm/llvm-project/commit/0f7791e9d82d7d40981eb1a781a61c1e988997fd.diff
LOG: [clang][Python] Use str.format instead of string concatenations (#173861)
This PR replaces string concatenations and the older `%` string
interpolation with `str.format`. Changes along those lines were
originally a part of #173845, but they were moved to a new PR,
where it was decided that `str.format` is preferable over
f-strings.
Changes in this commit are identical to
abf1d0bea04ab5d5ed1be3708ce1cd86707d5c8f, but commit title and
description were changed to correctly reflect the changes.
Added:
Modified:
clang/bindings/python/clang/cindex.py
Removed:
################################################################################
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index f4d7f4fe68966..1896a0a9c1c34 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -93,6 +93,7 @@
Generic,
Iterator,
Literal,
+ NoReturn,
Optional,
Sequence,
Type as TType,
@@ -212,12 +213,12 @@ def __init__(self, enumeration, message):
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
+ "constant: {}. Please file a bug to have this "
+ "value supported.".format(enumeration)
)
self.save_error = enumeration
- Exception.__init__(self, "Error %d: %s" % (enumeration, message))
+ Exception.__init__(self, "Error {}: {}".format(enumeration, message))
### Structures and Utility Classes ###
@@ -246,7 +247,9 @@ def __get__(self, instance: TInstance, instance_type: Any = None) -> TResult:
property_name = self.wrapped.__name__
class_name = instance_type.__name__
raise TypeError(
- f"'{property_name}' is not a static attribute of '{class_name}'"
+ "'{}' is not a static attribute of '{}'".format(
+ property_name, class_name
+ )
)
value = self.wrapped(instance)
@@ -357,10 +360,8 @@ def __repr__(self) -> str:
filename = self.file.name
else:
filename = None
- return "<SourceLocation file %r, line %r, column %r>" % (
- filename,
- self.line,
- self.column,
+ return "<SourceLocation file {}, line {}, column {}>".format(
+ repr(filename), repr(self.line), repr(self.column)
)
@@ -545,10 +546,8 @@ def format(self, options=None):
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,
+ return "<Diagnostic severity {}, location {}, spelling {}>".format(
+ repr(self.severity), repr(self.location), repr(self.spelling)
)
def __str__(self):
@@ -570,7 +569,7 @@ def __init__(self, range, value):
self.value = value
def __repr__(self):
- return "<FixIt range %r, value %r>" % (self.range, self.value)
+ return "<FixIt range {}, value {}>".format(repr(self.range), repr(self.value))
class TokenGroup:
@@ -644,10 +643,7 @@ def from_id(cls, id):
return cls(id)
def __repr__(self):
- return "%s.%s" % (
- self.__class__.__name__,
- self.name,
- )
+ return "{}.{}".format(self.__class__.__name__, self.name)
class TokenKind(BaseEnumeration):
@@ -2729,8 +2725,9 @@ def __getitem__(self, key: int) -> Type:
if key >= len(self):
raise IndexError(
- "Index greater than container length: "
- "%d > %d" % (key, len(self))
+ "Index greater than container length: {} > {}".format(
+ key, len(self)
+ )
)
result = Type.from_result(
@@ -3091,14 +3088,14 @@ class SpellingCacheAlias:
"will be removed in a future release."
)
- def __getattr__(self, _):
+ def __getattr__(self, _: Any) -> NoReturn:
raise AttributeError(self.deprecation_message)
- def __getitem__(self, value: int):
+ def __getitem__(self, value: int) -> str:
warnings.warn(self.deprecation_message, DeprecationWarning)
return CompletionChunk.SPELLING_CACHE[CompletionChunkKind.from_id(value)]
- def __contains__(self, value: int):
+ def __contains__(self, value: int) -> bool:
warnings.warn(self.deprecation_message, DeprecationWarning)
return CompletionChunkKind.from_id(value) in CompletionChunk.SPELLING_CACHE
@@ -3134,7 +3131,7 @@ def __init__(self, completionString: CObjP, key: int):
self.key = key
def __repr__(self) -> str:
- return "{'" + self.spelling + "', " + str(self.kind) + "}"
+ return "{{'{}', {}}}".format(self.spelling, self.kind)
@CachedProperty
def spelling(self) -> str:
@@ -3294,14 +3291,11 @@ def briefComment(self) -> str:
return _CXString.from_result(conf.lib.clang_getCompletionBriefComment(self.obj))
def __repr__(self) -> str:
- return (
- " | ".join([str(a) for a in self])
- + " || Priority: "
- + str(self.priority)
- + " || Availability: "
- + str(self.availability)
- + " || Brief comment: "
- + str(self.briefComment)
+ return "{chunks} || Priority: {priority} || Availability: {availability} || Brief comment: {comment}".format(
+ chunks=" | ".join(str(a) for a in self),
+ priority=self.priority,
+ availability=self.availability,
+ comment=self.briefComment,
)
@@ -3722,7 +3716,7 @@ def reparse(self, unsaved_files=None, options=0):
)
)
if result != 0:
- msg = "Error reparsing translation unit. Error code: " + str(result)
+ msg = "Error reparsing translation unit. Error code: {}".format(result)
raise TranslationUnitLoadError(msg)
def save(self, filename):
@@ -3840,7 +3834,7 @@ def __str__(self):
return self.name
def __repr__(self):
- return "<File: %s>" % (self.name)
+ return "<File: {}>".format(self.name)
def __eq__(self, other) -> bool:
return isinstance(other, File) and bool(
@@ -3900,13 +3894,12 @@ def __init__(self, enumeration, message):
if enumeration > 1:
raise Exception(
- "Encountered undefined CompilationDatabase error "
- "constant: %d. Please file a bug to have this "
- "value supported." % enumeration
+ "Encountered undefined CompilationDatabase error constant: {}."
+ "Please file a bug to have this value supported.".format(enumeration)
)
self.cdb_error = enumeration
- Exception.__init__(self, "Error %d: %s" % (enumeration, message))
+ Exception.__init__(self, "Error {}: {}".format(enumeration, message))
class CompileCommand:
More information about the cfe-commits
mailing list