[clang] [clang][Python] Use fstrings instead of string concatenations (PR #173861)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 11 14:15:31 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] 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:
More information about the cfe-commits
mailing list