Skip to content

Commit 3380048

Browse files
committed
Replace .format() with f-strings in error messages.
1 parent ce4ad76 commit 3380048

File tree

3 files changed

+58
-82
lines changed

3 files changed

+58
-82
lines changed

amaranth_soc/event.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def add(self, src):
167167
if self._frozen:
168168
raise ValueError("Event map has been frozen. Cannot add source")
169169
if not isinstance(src, Source):
170-
raise TypeError("Event source must be an instance of event.Source, not {!r}"
171-
.format(src))
170+
raise TypeError(f"Event source must be an instance of event.Source, not {src!r}")
172171
if id(src) not in self._sources:
173172
self._sources[id(src)] = src, self.size
174173

@@ -189,8 +188,7 @@ def index(self, src):
189188
Raises :exn:`KeyError` if the source is not found.
190189
"""
191190
if not isinstance(src, Source):
192-
raise TypeError("Event source must be an instance of event.Source, not {!r}"
193-
.format(src))
191+
raise TypeError(f"Event source must be an instance of event.Source, not {src!r}")
194192
_, index = self._sources[id(src)]
195193
return index
196194

amaranth_soc/memory.py

+40-56
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,12 @@ def __init__(self, resource, path, start, end, width):
131131
all(name and isinstance(name, str) for name in flattened_path)):
132132
raise TypeError(f"Path must be a non-empty tuple of non-empty strings, not {path!r}")
133133
if not isinstance(start, int) or start < 0:
134-
raise TypeError("Start address must be a non-negative integer, not {!r}"
135-
.format(start))
134+
raise TypeError(f"Start address must be a non-negative integer, not {start!r}")
136135
if not isinstance(end, int) or end <= start:
137-
raise TypeError("End address must be an integer greater than the start address, "
138-
"not {!r}".format(end))
136+
raise TypeError(f"End address must be an integer greater than the start address, "
137+
f"not {end!r}")
139138
if not isinstance(width, int) or width < 0:
140-
raise TypeError("Width must be a non-negative integer, not {!r}"
141-
.format(width))
139+
raise TypeError(f"Width must be a non-negative integer, not {width!r}")
142140

143141
self._resource = resource
144142
self._path = tuple(path)
@@ -199,16 +197,13 @@ class MemoryMap:
199197
"""
200198
def __init__(self, *, addr_width, data_width, alignment=0, name=None):
201199
if not isinstance(addr_width, int) or addr_width <= 0:
202-
raise ValueError("Address width must be a positive integer, not {!r}"
203-
.format(addr_width))
200+
raise ValueError(f"Address width must be a positive integer, not {addr_width!r}")
204201
if not isinstance(data_width, int) or data_width <= 0:
205-
raise ValueError("Data width must be a positive integer, not {!r}"
206-
.format(data_width))
202+
raise ValueError(f"Data width must be a positive integer, not {data_width!r}")
207203
if not isinstance(alignment, int) or alignment < 0:
208-
raise ValueError("Alignment must be a non-negative integer, not {!r}"
209-
.format(alignment))
204+
raise ValueError(f"Alignment must be a non-negative integer, not {alignment!r}")
210205
if name is not None and not (isinstance(name, str) and name):
211-
raise ValueError("Name must be a non-empty string, not {!r}".format(name))
206+
raise ValueError(f"Name must be a non-empty string, not {name!r}")
212207

213208
self._addr_width = addr_width
214209
self._data_width = data_width
@@ -267,32 +262,28 @@ def align_to(self, alignment):
267262
Implicit next address.
268263
"""
269264
if not isinstance(alignment, int) or alignment < 0:
270-
raise ValueError("Alignment must be a non-negative integer, not {!r}"
271-
.format(alignment))
265+
raise ValueError(f"Alignment must be a non-negative integer, not {alignment!r}")
272266
self._next_addr = self._align_up(self._next_addr, max(alignment, self.alignment))
273267
return self._next_addr
274268

275269
def _compute_addr_range(self, addr, size, step=1, *, alignment):
276270
if addr is not None:
277271
if not isinstance(addr, int) or addr < 0:
278-
raise ValueError("Address must be a non-negative integer, not {!r}"
279-
.format(addr))
272+
raise ValueError(f"Address must be a non-negative integer, not {addr!r}")
280273
if addr % (1 << self.alignment) != 0:
281-
raise ValueError("Explicitly specified address {:#x} must be a multiple of "
282-
"{:#x} bytes"
283-
.format(addr, 1 << alignment))
274+
raise ValueError(f"Explicitly specified address {addr:#x} must be a multiple of "
275+
f"{1 << alignment:#x} bytes")
284276
else:
285277
addr = self._align_up(self._next_addr, alignment)
286278

287279
if not isinstance(size, int) or size < 0:
288-
raise ValueError("Size must be a non-negative integer, not {!r}"
289-
.format(size))
280+
raise ValueError(f"Size must be a non-negative integer, not {size!r}")
290281
size = self._align_up(max(size, 1), alignment)
291282

292283
if addr > (1 << self.addr_width) or addr + size > (1 << self.addr_width):
293-
raise ValueError("Address range {:#x}..{:#x} out of bounds for memory map spanning "
294-
"range {:#x}..{:#x} ({} address bits)"
295-
.format(addr, addr + size, 0, 1 << self.addr_width, self.addr_width))
284+
raise ValueError(f"Address range {addr:#x}..{addr + size:#x} out of bounds for "
285+
f"memory map spanning range {0:#x}..{1 << self.addr_width:#x} "
286+
f"({self.addr_width} address bits)")
296287

297288
addr_range = range(addr, addr + size, step)
298289
overlaps = self._ranges.overlaps(addr_range)
@@ -301,14 +292,14 @@ def _compute_addr_range(self, addr, size, step=1, *, alignment):
301292
for overlap in overlaps:
302293
if id(overlap) in self._resources:
303294
_, _, resource_range = self._resources[id(overlap)]
304-
overlap_descrs.append("resource {!r} at {:#x}..{:#x}"
305-
.format(overlap, resource_range.start, resource_range.stop))
295+
overlap_descrs.append(f"resource {overlap!r} at {resource_range.start:#x}.."
296+
f"{resource_range.stop:#x}")
306297
if id(overlap) in self._windows:
307298
_, window_range = self._windows[id(overlap)]
308-
overlap_descrs.append("window {!r} at {:#x}..{:#x}"
309-
.format(overlap, window_range.start, window_range.stop))
310-
raise ValueError("Address range {:#x}..{:#x} overlaps with {}"
311-
.format(addr, addr + size, ", ".join(overlap_descrs)))
299+
overlap_descrs.append(f"window {overlap!r} at {window_range.start:#x}.."
300+
f"{window_range.stop:#x}")
301+
raise ValueError(f"Address range {addr:#x}..{addr + size:#x} overlaps with " +
302+
", ".join(overlap_descrs))
312303

313304
return addr_range
314305

@@ -355,16 +346,15 @@ def add_resource(self, resource, *, name, size, addr=None, alignment=None):
355346
this memory map.
356347
"""
357348
if self._frozen:
358-
raise ValueError("Memory map has been frozen. Cannot add resource {!r}"
359-
.format(resource))
349+
raise ValueError(f"Memory map has been frozen. Cannot add resource {resource!r}")
360350

361351
if not isinstance(resource, wiring.Component):
362352
raise TypeError(f"Resource must be a wiring.Component, not {resource!r}")
363353

364354
if id(resource) in self._resources:
365355
_, _, addr_range = self._resources[id(resource)]
366-
raise ValueError("Resource {!r} is already added at address range {:#x}..{:#x}"
367-
.format(resource, addr_range.start, addr_range.stop))
356+
raise ValueError(f"Resource {resource!r} is already added at address range "
357+
f"{addr_range.start:#x}..{addr_range.stop:#x}")
368358

369359
if not (name and isinstance(name, tuple) and
370360
all(part and isinstance(part, str) for part in name)):
@@ -380,8 +370,7 @@ def add_resource(self, resource, *, name, size, addr=None, alignment=None):
380370

381371
if alignment is not None:
382372
if not isinstance(alignment, int) or alignment < 0:
383-
raise ValueError("Alignment must be a non-negative integer, not {!r}"
384-
.format(alignment))
373+
raise ValueError(f"Alignment must be a non-negative integer, not {alignment!r}")
385374
alignment = max(alignment, self.alignment)
386375
else:
387376
alignment = self.alignment
@@ -470,33 +459,28 @@ def add_window(self, window, *, addr=None, sparse=None):
470459
conflicts with the name of others present in this memory map.
471460
"""
472461
if not isinstance(window, MemoryMap):
473-
raise TypeError("Window must be a MemoryMap, not {!r}"
474-
.format(window))
462+
raise TypeError(f"Window must be a MemoryMap, not {window!r}")
475463

476464
if self._frozen:
477-
raise ValueError("Memory map has been frozen. Cannot add window {!r}"
478-
.format(window))
465+
raise ValueError(f"Memory map has been frozen. Cannot add window {window!r}")
479466

480467
if id(window) in self._windows:
481468
_, addr_range = self._windows[id(window)]
482-
raise ValueError("Window {!r} is already added at address range {:#x}..{:#x}"
483-
.format(window, addr_range.start, addr_range.stop))
469+
raise ValueError(f"Window {window!r} is already added at address range "
470+
f"{addr_range.start:#x}..{addr_range.stop:#x}")
484471

485472
if window.data_width > self.data_width:
486-
raise ValueError("Window has data width {}, and cannot be added to a memory map "
487-
"with data width {}"
488-
.format(window.data_width, self.data_width))
473+
raise ValueError(f"Window has data width {window.data_width}, and cannot be added to a "
474+
f"memory map with data width {self.data_width}")
489475
if window.data_width != self.data_width:
490476
if sparse is None:
491-
raise ValueError("Address translation mode must be explicitly specified "
492-
"when adding a window with data width {} to a memory map "
493-
"with data width {}"
494-
.format(window.data_width, self.data_width))
477+
raise ValueError(f"Address translation mode must be explicitly specified when "
478+
f"adding a window with data width {window.data_width} to a "
479+
f"memory map with data width {self.data_width}")
495480
if not sparse and self.data_width % window.data_width != 0:
496-
raise ValueError("Dense addressing cannot be used because the memory map "
497-
"data width {} is not an integer multiple of window "
498-
"data width {}"
499-
.format(self.data_width, window.data_width))
481+
raise ValueError(f"Dense addressing cannot be used because the memory map "
482+
f"data width {self.data_width} is not an integer multiple of "
483+
f"window data width {window.data_width}")
500484

501485
queries = window._namespace.names() if window.name is None else ((window.name,),)
502486
reasons = []
@@ -564,10 +548,10 @@ def window_patterns(self):
564548
for window, (window_start, window_stop, window_ratio) in self.windows():
565549
const_bits = self.addr_width - window.addr_width
566550
if const_bits > 0:
567-
const_pat = "{:0{}b}".format(window_start >> window.addr_width, const_bits)
551+
const_pat = f"{window_start >> window.addr_width:0{const_bits}b}"
568552
else:
569553
const_pat = ""
570-
pattern = "{}{}".format(const_pat, "-" * window.addr_width)
554+
pattern = const_pat + "-" * window.addr_width
571555
yield window, (pattern, window_ratio)
572556

573557
@staticmethod

amaranth_soc/periph.py

+16-22
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class ConstantBool(ConstantValue):
2424
"""
2525
def __init__(self, value):
2626
if not isinstance(value, bool):
27-
raise TypeError("Value must be a bool, not {!r}".format(value))
27+
raise TypeError(f"Value must be a bool, not {value!r}")
2828
self._value = value
2929

3030
@property
3131
def value(self):
3232
return self._value
3333

3434
def __repr__(self):
35-
return "ConstantBool({})".format(self.value)
35+
return f"ConstantBool({self.value})"
3636

3737

3838
class ConstantInt(ConstantValue):
@@ -49,25 +49,22 @@ class ConstantInt(ConstantValue):
4949
"""
5050
def __init__(self, value, *, width=None, signed=None):
5151
if not isinstance(value, int):
52-
raise TypeError("Value must be an integer, not {!r}"
53-
.format(value))
52+
raise TypeError(f"Value must be an integer, not {value!r}")
5453
self._value = value
5554

5655
if width is None:
5756
width = bits_for(value)
5857
if not isinstance(width, int):
59-
raise TypeError("Width must be an integer, not {!r}"
60-
.format(width))
58+
raise TypeError(f"Width must be an integer, not {width!r}")
6159
if width < bits_for(value):
62-
raise ValueError("Width must be greater than or equal to the number of bits needed to"
63-
" represent {}".format(value))
60+
raise ValueError(f"Width must be greater than or equal to the number of bits needed "
61+
f"to represent {value}")
6462
self._width = width
6563

6664
if signed is None:
6765
signed = value < 0
6866
if not isinstance(signed, bool):
69-
raise TypeError("Signedness must be a bool, not {!r}"
70-
.format(signed))
67+
raise TypeError(f"Signedness must be a bool, not {signed!r}")
7168
self._signed = signed
7269

7370
@property
@@ -83,7 +80,7 @@ def signed(self):
8380
return self._signed
8481

8582
def __repr__(self):
86-
return "ConstantInt({}, width={}, signed={})".format(self.value, self.width, self.signed)
83+
return f"ConstantInt({self.value}, width={self.width}, signed={self.signed})"
8784

8885

8986
class ConstantMap(Mapping):
@@ -109,8 +106,8 @@ def __init__(self, **constants):
109106
if isinstance(value, int):
110107
value = ConstantInt(value)
111108
if not isinstance(value, ConstantValue):
112-
raise TypeError("Constant value must be an instance of ConstantValue, not {!r}"
113-
.format(value))
109+
raise TypeError(f"Constant value must be an instance of ConstantValue, not "
110+
f"{value!r}")
114111
self._storage[key] = value
115112

116113
def __getitem__(self, key):
@@ -123,7 +120,7 @@ def __len__(self):
123120
return len(self._storage)
124121

125122
def __repr__(self):
126-
return "ConstantMap({})".format(list(self._storage.items()))
123+
return f"ConstantMap({list(self._storage.items())})"
127124

128125

129126
class PeripheralInfo:
@@ -143,21 +140,19 @@ class PeripheralInfo:
143140
"""
144141
def __init__(self, *, memory_map, irq=None, constant_map=None):
145142
if not isinstance(memory_map, MemoryMap):
146-
raise TypeError("Memory map must be an instance of MemoryMap, not {!r}"
147-
.format(memory_map))
143+
raise TypeError(f"Memory map must be an instance of MemoryMap, not {memory_map!r}")
148144
memory_map.freeze()
149145
self._memory_map = memory_map
150146

151147
if irq is not None and not isinstance(irq, event.Source):
152-
raise TypeError("IRQ line must be an instance of event.Source, not {!r}"
153-
.format(irq))
148+
raise TypeError(f"IRQ line must be an instance of event.Source, not {irq!r}")
154149
self._irq = irq
155150

156151
if constant_map is None:
157152
constant_map = ConstantMap()
158153
if not isinstance(constant_map, ConstantMap):
159-
raise TypeError("Constant map must be an instance of ConstantMap, not {!r}"
160-
.format(constant_map))
154+
raise TypeError(f"Constant map must be an instance of ConstantMap, not "
155+
f"{constant_map!r}")
161156
self._constant_map = constant_map
162157

163158
@property
@@ -184,8 +179,7 @@ def irq(self):
184179
Raises :exn:`NotImplementedError` if the peripheral info does not have an IRQ line.
185180
"""
186181
if self._irq is None:
187-
raise NotImplementedError("Peripheral info does not have an IRQ line"
188-
.format(self))
182+
raise NotImplementedError("Peripheral info does not have an IRQ line")
189183
return self._irq
190184

191185
@property

0 commit comments

Comments
 (0)