[clang] [clang][Python] Use fstrings instead of string concatenations (PR #173861)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 13 13:47:20 PST 2026
https://github.com/mikomikotaishi updated https://github.com/llvm/llvm-project/pull/173861
>From 6df59741ab64b39f02c89315dc56c7ab950ad8ce Mon Sep 17 00:00:00 2001
From: Toyosatomimi no Miko <110693261+mikomikotaishi at users.noreply.github.com>
Date: Mon, 29 Dec 2025 06:47:10 -0500
Subject: [PATCH 1/2] Use fstrings instead of string concatenations
---
clang/bindings/python/clang/cindex.py | 60 ++++++++++-----------------
1 file changed, 22 insertions(+), 38 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 2b6ab00c88219..d3c6f419975f0 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -210,13 +210,13 @@ 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
+ 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))
+ Exception.__init__(self, f"Error {enumeration}: {message}")
### Structures and Utility Classes ###
@@ -354,11 +354,7 @@ def __repr__(self):
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):
@@ -410,7 +406,7 @@ def __contains__(self, other):
return self.start <= other <= self.end
def __repr__(self):
- return "<SourceRange start %r, end %r>" % (self.start, self.end)
+ return f"<SourceRange start {self.start!r}, end {self.end!r}>"
class Diagnostic:
@@ -542,11 +538,7 @@ 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 f"<Diagnostic severity {self.severity!r}, location {self.location!r}, spelling {self.spelling!r}>"
def __str__(self):
return self.format()
@@ -567,7 +559,7 @@ def __init__(self, range, value):
self.value = value
def __repr__(self):
- return "<FixIt range %r, value %r>" % (self.range, self.value)
+ return f"<FixIt range {self.range!r}, value {self.value!r}>"
class TokenGroup:
@@ -641,10 +633,7 @@ def from_id(cls, id):
return cls(id)
def __repr__(self):
- return "%s.%s" % (
- self.__class__.__name__,
- self.name,
- )
+ return f"{self.__class__.__name__}.{self.name}"
class TokenKind(BaseEnumeration):
@@ -2726,8 +2715,7 @@ def __getitem__(self, key: int) -> Type:
if key >= len(self):
raise IndexError(
- "Index greater than container length: "
- "%d > %d" % (key, len(self))
+ f"Index greater than container length: {key} > {len(self)}"
)
result = Type.from_result(
@@ -3060,7 +3048,7 @@ def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
- return "<ChunkKind: %s>" % self
+ return f"<ChunkKind: {self}>"
def __init__(self, completionString: CObjP, key: int):
self.cs = completionString
@@ -3068,7 +3056,7 @@ def __init__(self, completionString: CObjP, key: int):
self.__kindNumberCache = -1
def __repr__(self) -> str:
- return "{'" + self.spelling + "', " + str(self.kind) + "}"
+ return f"{{'{self.spelling}', {self.kind}}}"
@CachedProperty
def spelling(self) -> str:
@@ -3151,7 +3139,7 @@ def __str__(self):
return self.name
def __repr__(self):
- return "<Availability: %s>" % self
+ return f"<Availability: {self}>"
def __len__(self) -> int:
return self.num_chunks
@@ -3187,13 +3175,10 @@ def briefComment(self) -> str:
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)
+ f"{' | '.join(str(a) for a in self)}"
+ f" || Priority: {self.priority}"
+ f" || Availability: {self.availability}"
+ f" || Brief comment: {self.briefComment}"
)
@@ -3604,7 +3589,7 @@ def reparse(self, unsaved_files=None, options=0):
)
)
if result != 0:
- msg = "Error reparsing translation unit. Error code: " + str(result)
+ msg = f"Error reparsing translation unit. Error code: {result}"
raise TranslationUnitLoadError(msg)
def save(self, filename):
@@ -3722,7 +3707,7 @@ def __str__(self):
return self.name
def __repr__(self):
- return "<File: %s>" % (self.name)
+ return f"<File: {self.name}>"
def __eq__(self, other) -> bool:
return isinstance(other, File) and bool(
@@ -3782,13 +3767,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
+ f"Encountered undefined CompilationDatabase error constant: {enumeration}."
+ "Please file a bug to have this value supported."
)
self.cdb_error = enumeration
- Exception.__init__(self, "Error %d: %s" % (enumeration, message))
+ Exception.__init__(self, f"Error {enumeration}: {message}")
class CompileCommand:
>From 325054296e38dc065c889078e964fefe6c798e30 Mon Sep 17 00:00:00 2001
From: Toyosatomimi no Miko <110693261+mikomikotaishi at users.noreply.github.com>
Date: Fri, 13 Feb 2026 16:45:45 -0500
Subject: [PATCH 2/2] Use str.format() instead of fstrings
---
clang/bindings/python/clang/cindex.py | 42 ++++++++++++++-------------
1 file changed, 22 insertions(+), 20 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 8593b7b8c7df3..89ea8475e9c14 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -212,13 +212,13 @@ def __init__(self, enumeration, message):
if enumeration < 1 or enumeration > 3:
raise Exception(
- f"Encountered undefined TranslationUnit save error "
- f"constant: {enumeration}. Please file a bug to have this "
- f"value supported."
+ "Encountered undefined TranslationUnit save error "
+ "constant: {}. Please file a bug to have this "
+ "value supported.".format(enumeration)
)
self.save_error = enumeration
- Exception.__init__(self, f"Error {enumeration}: {message}")
+ Exception.__init__(self, "Error {}: {}".format(enumeration, message))
### Structures and Utility Classes ###
@@ -247,7 +247,7 @@ 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)
@@ -358,7 +358,7 @@ def __repr__(self) -> str:
filename = self.file.name
else:
filename = None
- return f"<SourceLocation file {filename!r}, line {self.line!r}, column {self.column!r}>"
+ return "<SourceLocation file {}, line {}, column {}>".format(filename, self.line, self.column)
class SourceRange(Structure):
@@ -542,7 +542,7 @@ def format(self, options=None):
return _CXString.from_result(conf.lib.clang_formatDiagnostic(self, options))
def __repr__(self):
- return f"<Diagnostic severity {self.severity!r}, location {self.location!r}, spelling {self.spelling!r}>"
+ return "<Diagnostic severity {}, location {}, spelling {}>".format(self.severity, self.location, self.spelling)
def __str__(self):
return self.format()
@@ -563,7 +563,7 @@ def __init__(self, range, value):
self.value = value
def __repr__(self):
- return f"<FixIt range {self.range!r}, value {self.value!r}>"
+ return "<FixIt range {}, value {}>".format(self.range, self.value)
class TokenGroup:
@@ -637,7 +637,7 @@ def from_id(cls, id):
return cls(id)
def __repr__(self):
- return f"{self.__class__.__name__}.{self.name}"
+ return "{}.{}".format(self.__class__.__name__, self.name)
class TokenKind(BaseEnumeration):
@@ -2719,7 +2719,7 @@ def __getitem__(self, key: int) -> Type:
if key >= len(self):
raise IndexError(
- f"Index greater than container length: {key} > {len(self)}"
+ "Index greater than container length: {} > {}".format(key, len(self))
)
result = Type.from_result(
@@ -3123,7 +3123,7 @@ def __init__(self, completionString: CObjP, key: int):
self.key = key
def __repr__(self) -> str:
- return f"{{'{self.spelling}', {self.kind}}}"
+ return "{{'{}', {}}}".format(self.spelling, self.kind)
@CachedProperty
def spelling(self) -> str:
@@ -3284,10 +3284,12 @@ def briefComment(self) -> str:
def __repr__(self) -> str:
return (
- f"{' | '.join(str(a) for a in self)}"
- f" || Priority: {self.priority}"
- f" || Availability: {self.availability}"
- f" || Brief comment: {self.briefComment}"
+ "{} || Priority: {} || Availability: {} || Brief comment: {}".format(
+ " | ".join(str(a) for a in self),
+ self.priority,
+ self.availability,
+ self.briefComment
+ )
)
@@ -3708,7 +3710,7 @@ def reparse(self, unsaved_files=None, options=0):
)
)
if result != 0:
- msg = f"Error reparsing translation unit. Error code: {result}"
+ msg = "Error reparsing translation unit. Error code: {}".format(result)
raise TranslationUnitLoadError(msg)
def save(self, filename):
@@ -3826,7 +3828,7 @@ def __str__(self):
return self.name
def __repr__(self):
- return f"<File: {self.name}>"
+ return "<File: {}>".format(self.name)
def __eq__(self, other) -> bool:
return isinstance(other, File) and bool(
@@ -3886,12 +3888,12 @@ def __init__(self, enumeration, message):
if enumeration > 1:
raise Exception(
- f"Encountered undefined CompilationDatabase error constant: {enumeration}."
- "Please file a bug to have this value supported."
+ "Encountered undefined CompilationDatabase error constant: {}."
+ "Please file a bug to have this value supported.".format(enumeration)
)
self.cdb_error = enumeration
- Exception.__init__(self, f"Error {enumeration}: {message}")
+ Exception.__init__(self, "Error {}: {}".format(enumeration, message))
class CompileCommand:
More information about the cfe-commits
mailing list