8000 Merge pull request #28768 from Schefflera-Arboricola/fstrings · numpy/numpy@4ede17d · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 4ede17d

Browse files
authored
Merge pull request #28768 from Schefflera-Arboricola/fstrings
MAINT: getting rid of old `%` and `.format(...)` strings with `f-strings`
2 parents 6c7e63a + df41897 commit 4ede17d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+635
-694
lines changed

benchmarks/benchmarks/bench_ufunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
if len(missing_ufuncs) > 0:
3838
missing_ufunc_names = [f.__name__ for f in missing_ufuncs]
3939
raise NotImplementedError(
40-
"Missing benchmarks for ufuncs %r" % missing_ufunc_names)
40+
f"Missing benchmarks for ufuncs {missing_ufunc_names!r}")
4141

4242

4343
class ArrayFunctionDispatcher(Benchmark):

doc/neps/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"show_prev_next": False,
9898
}
9999

100-
html_title = "%s" % (project)
100+
html_title = f"{project}"
101101
html_static_path = ['../source/_static']
102102
html_last_updated_fmt = '%b %d, %Y'
103103

doc/source/conf.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class PyTypeObject(ctypes.Structure):
127127
version = re.sub(r'(\.dev\d+).*?$', r'\1', version)
128128
# The full version, including alpha/beta/rc tags.
129129
release = numpy.__version__
130-
print("%s %s" % (version, release))
130+
print(f"{version} {release}")
131131

132132
# There are two options for replacing |today|: either, you set today to some
133133
# non-false value, then it is used:
@@ -271,7 +271,7 @@ def setup(app):
271271
"show_version_warning_banner": True,
272272
}
273273

274-
html_title = "%s v%s Manual" % (project, version)
274+
html_title = f"{project} v{version} Manual"
275275
html_static_path = ['_static']
276276
html_last_updated_fmt = '%b %d, %Y'
277277
html_css_files = ["numpy.css"]
@@ -575,8 +575,7 @@ def linkcode_resolve(domain, info):
575575
linespec = ""
576576

577577
if 'dev' in numpy.__version__:
578-
return "https://github.com/numpy/numpy/blob/main/numpy/%s%s" % (
579-
fn, linespec)
578+
return f"https://github.com/numpy/numpy/blob/main/numpy/{fn}{linespec}"
580579
else:
581580
return "https://github.com/numpy/numpy/blob/v%s/numpy/%s%s" % (
582581
numpy.__version__, fn, linespec)

numpy/_core/_dtype.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def _scalar_str(dtype, short):
121121

122122
elif dtype.type == np.str_:
123123
if _isunsized(dtype):
124-
return "'%sU'" % byteorder
124+
return f"'{byteorder}U'"
125125
else:
126126
return "'%sU%d'" % (byteorder, dtype.itemsize / 4)
127127

@@ -140,10 +140,10 @@ def _scalar_str(dtype, short):
140140
return "'V%d'" % dtype.itemsize
141141

142142
elif dtype.type == np.datetime64:
143-
return "'%sM8%s'" % (byteorder, _datetime_metadata_str(dtype))
143+
return f"'{byteorder}M8{_datetime_metadata_str(dtype)}'"
144144

145145
elif dtype.type == np.timedelta64:
146-
return "'%sm8%s'" % (byteorder, _datetime_metadata_str(dtype))
146+
return f"'{byteorder}m8{_datetime_metadata_str(dtype)}'"
147147

148148
elif dtype.isbuiltin == 2:
149149
return dtype.type.__name__
@@ -217,17 +217,17 @@ def _struct_dict_str(dtype, includealignedflag):
217217
ret += fieldsep.join(repr(name) for name in names)
218218

219219
# Second, the formats
220-
ret += "], 'formats'%s[" % colon
220+
ret += f"], 'formats'{colon}["
221221
ret += fieldsep.join(
222222
_construction_repr(fld_dtype, short=True) for fld_dtype in fld_dtypes)
223223

224224
# Third, the offsets
225-
ret += "], 'offsets'%s[" % colon
225+
ret += f"], 'offsets'{colon}["
226226
ret += fieldsep.join("%d" % offset for offset in offsets)
227227

228228
# Fourth, the titles
229229
if any(title is not None for title in titles):
230-
ret += "], 'titles'%s[" % colon
230+
ret += f"], 'titles'{colon}["
231231
ret += fieldsep.join(repr(title) for title in titles)
232232

233233
# Fifth, the itemsize

numpy/_core/_internal.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ def _commastring(astr):
183183
order2 = _convorder.get(order2, order2)
184184
if (order1 != order2):
185185
raise ValueError(
186-
'inconsistent byte-order specification %s and %s' %
187-
(order1, order2))
186+
f'inconsistent byte-order specification {order1} and {order2}')
188187
order = order1
189188

190189
if order in ('|', '=', _nbo):
@@ -742,7 +741,7 @@ def __dtype_from_pep3118(stream, is_subdtype):
742741
f"Unrepresentable PEP 3118 data type {stream.next!r} ({desc})")
743742
else:
744743
raise ValueError(
745-
"Unknown PEP 3118 data type specifier %r" % stream.s
744+
f"Unknown PEP 3118 data type specifier {stream.s!r}"
746745
)
747746

748747
#

numpy/_core/_machar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class MachAr:
103103

104104
def __init__(self, float_conv=float, int_conv=int,
105105
float_to_float=float,
106-
float_to_str=lambda v: '%24.16e' % v,
106+
float_to_str=lambda v: f'{v:24.16e}',
107107
title='Python floating point number'):
108108
"""
109109

numpy/_core/arrayprint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,14 +1564,14 @@ def dtype_short_repr(dtype):
15641564
return str(dtype)
15651565
elif issubclass(dtype.type, flexible):
15661566
# handle these separately so they don't give garbage like str256
1567-
return "'%s'" % str(dtype)
1567+
return f"'{str(dtype)}'"
15681568

15691569
typename = dtype.name
15701570
if not dtype.isnative:
15711571
# deal with cases like dtype('<u2') that are identical to an
15721572
# established dtype (in this case uint16)
15731573
# except that they have a different endianness.
1574-
return "'%s'" % str(dtype)
1574+
return f"'{str(dtype)}'"
15751575
# quote typenames which can't be represented as python variable names
15761576
if typename and not (typename[0].isalpha() and typename.isalnum()):
15771577
typename = repr(typename)

numpy/_core/code_generators/genapi.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ def _format_arg(self, typename, name):
154154
def __str__(self):
155155
argstr = ', '.join([self._format_arg(*a) for a in self.args])
156156
if self.doc:
157-
doccomment = '/* %s */\n' % self.doc
157+
doccomment = f'/* {self.doc} */\n'
158158
else:
159159
doccomment = ''
160-
return '%s%s %s(%s)' % (doccomment, self.return_type, self.name, argstr)
160+
return f'{doccomment}{self.return_type} {self.name}({argstr})'
161161

162162
def api_hash(self):
163163
m = hashlib.md5(usedforsecurity=False)
@@ -177,7 +177,7 @@ def __init__(self, filename, lineno, msg):
177177
self.msg = msg
178178

179179
def __str__(self):
180-
return '%s:%s:%s' % (self.filename, self.lineno, self.msg)
180+
return f'{self.filename}:{self.lineno}:{self.msg}'
181181

182182
def skip_brackets(s, lbrac, rbrac):
183183
count = 0
@@ -188,7 +188,7 @@ def skip_brackets(s, lbrac, rbrac):
188188
count -= 1
189189
if count == 0:
190190
return i
191-
raise ValueError("no match '%s' for '%s' (%r)" % (lbrac, rbrac, s))
191+
raise ValueError(f"no match '{lbrac}' for '{rbrac}' ({s!r})")
192192

193193
def split_arguments(argstr):
194194
arguments = []
@@ -344,7 +344,7 @@ def define_from_array_api_string(self):
344344
self.index)
345345

346346
def array_api_define(self):
347-
return " (void *) &%s" % self.name
347+
return f" (void *) &{self.name}"
348348

349349
def internal_define(self):
350350
if self.internal_type is None:
@@ -376,12 +376,11 @@ def define_from_array_api_string(self):
376376
self.index)
377377

378378
def array_api_define(self):
379-
return " (%s *) &%s" % (self.type, self.name)
379+
return f" ({self.type} *) &{self.name}"
380380

381381
def internal_define(self):
382-
astr = """\
383-
extern NPY_NO_EXPORT %(type)s %(name)s;
384-
""" % {'type': self.type, 'name': self.name}
382+
astr = f"""extern NPY_NO_EXPORT {self.type} {self.name};
383+
"""
385384
return astr
386385

387386
# Dummy to be able to consistently use *Api instances for all items in the
@@ -400,7 +399,7 @@ def define_from_array_api_string(self):
400399
self.index)
401400

402401
def array_api_define(self):
403-
return " (void *) &%s" % self.name
402+
return f" (void *) &{self.name}"
404403

405404
def internal_define(self):
406405
astr = """\
@@ -448,15 +447,13 @@ def define_from_array_api_string(self):
448447
return define
449448

450449
def array_api_define(self):
451-
return " (void *) %s" % self.name
450+
return f" (void *) {self.name}"
452451

453452
def internal_define(self):
454453
annstr = [str(a) for a in self.annotations]
455454
annstr = ' '.join(annstr)
456-
astr = """\
457-
NPY_NO_EXPORT %s %s %s \\\n (%s);""" % (annstr, self.return_type,
458-
self.name,
459-
self._argtypes_string())
455+
astr = f"""NPY_NO_EXPORT {annstr} {self.return_type} {self.name} \\
456+
({self._argtypes_string()});"""
460457
return astr
461458

462459
def order_dict(d):
@@ -511,8 +508,7 @@ def check_api_dict(d):
511508
f"{indexes.intersection(removed)}")
512509
if indexes.union(removed) != expected:
513510
diff = expected.symmetric_difference(indexes.union(removed))
514-
msg = "There are some holes in the API indexing: " \
515-
"(symmetric diff is %s)" % diff
511+
msg = f"There are some holes in the API indexing: (symmetric diff is {diff})"
516512
raise ValueError(msg)
517513

518514
def get_api_functions(tagname, api_dict):

numpy/_core/code_generators/generate_numpy_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@
208208
def generate_api(output_dir, force=False):
209209
basename = 'multiarray_api'
210210

211-
h_file = os.path.join(output_dir, '__%s.h' % basename)
212-
c_file = os.path.join(output_dir, '__%s.c' % basename)
211+
h_file = os.path.join(output_dir, f'__{basename}.h')
212+
c_file = os.path.join(output_dir, f'__{basename}.c')
213213
targets = (h_file, c_file)
214214

215215
sources = numpy_api.multiarray_api

numpy/_core/code_generators/generate_ufunc_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@
140140
def generate_api(output_dir, force=False):
141141
basename = 'ufunc_api'
142142

143-
h_file = os.path.join(output_dir, '__%s.h' % basename)
144-
c_file = os.path.join(output_dir, '__%s.c' % basename)
143+
h_file = os.path.join(output_dir, f'__{basename}.h')
144+
c_file = os.path.join(output_dir, f'__{basename}.c')
145145
targets = (h_file, c_file)
146146

147147
sources = ['ufunc_api_order.txt']

0 commit comments

Comments
 (0)
0