Skip to content

Commit 06c5acd

Browse files
ArmavicaricardoV94
authored andcommitted
Fix UP031 with ruff --unsafe-fixes
1 parent cada5ad commit 06c5acd

36 files changed

+2148
-2141
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ disable = ["C0330", "C0326"]
130130

131131
[tool.ruff]
132132
select = ["C", "E", "F", "I", "UP", "W"]
133-
ignore = ["C408", "C901", "E501", "E741", "UP031"]
133+
ignore = ["C408", "C901", "E501", "E741"]
134134
exclude = ["doc/", "pytensor/_version.py", "bin/pytensor_cache.py"]
135135

136136

pytensor/compile/builders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def __hash__(self):
465465
def __str__(self):
466466
name = self.__class__.__name__ if self.name is None else self.name
467467
is_inline = self.is_inline
468-
return "%(name)s{inline=%(is_inline)s}" % locals()
468+
return "{name}{{inline={is_inline}}}".format(**locals())
469469

470470
@config.change_flags(compute_test_value="off")
471471
def _recompute_lop_op(self):

pytensor/link/c/basic.py

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -250,50 +250,46 @@ def struct_gen(args, struct_builders, blocks, sub):
250250
# that holds the type, the value and the traceback. After storing
251251
# the error, we return the failure code so we know which code
252252
# block failed.
253-
do_return = (
254-
"""
255-
if (%(failure_var)s) {
253+
do_return = """
254+
if ({failure_var}) {{
256255
// When there is a failure, this code puts the exception
257256
// in __ERROR.
258257
PyObject* err_type = NULL;
259258
PyObject* err_msg = NULL;
260259
PyObject* err_traceback = NULL;
261260
PyErr_Fetch(&err_type, &err_msg, &err_traceback);
262-
if (!err_type) {err_type = Py_None;Py_INCREF(Py_None);}
263-
if (!err_msg) {err_msg = Py_None; Py_INCREF(Py_None);}
264-
if (!err_traceback) {err_traceback = Py_None; Py_INCREF(Py_None);}
261+
if (!err_type) {{err_type = Py_None;Py_INCREF(Py_None);}}
262+
if (!err_msg) {{err_msg = Py_None; Py_INCREF(Py_None);}}
263+
if (!err_traceback) {{err_traceback = Py_None; Py_INCREF(Py_None);}}
265264
PyObject* old_err_type = PyList_GET_ITEM(__ERROR, 0);
266265
PyObject* old_err_msg = PyList_GET_ITEM(__ERROR, 1);
267266
PyObject* old_err_traceback = PyList_GET_ITEM(__ERROR, 2);
268267
PyList_SET_ITEM(__ERROR, 0, err_type);
269268
PyList_SET_ITEM(__ERROR, 1, err_msg);
270269
PyList_SET_ITEM(__ERROR, 2, err_traceback);
271-
{Py_XDECREF(old_err_type);}
272-
{Py_XDECREF(old_err_msg);}
273-
{Py_XDECREF(old_err_traceback);}
274-
}
270+
{{Py_XDECREF(old_err_type);}}
271+
{{Py_XDECREF(old_err_msg);}}
272+
{{Py_XDECREF(old_err_traceback);}}
273+
}}
275274
// The failure code is returned to index what code block failed.
276-
return %(failure_var)s;
277-
"""
278-
% sub
279-
)
275+
return {failure_var};
276+
""".format(**sub)
280277

281278
sub = dict(sub)
282279
sub.update(locals())
283280

284281
# TODO: add some error checking to make sure storage_<x> are
285282
# 1-element lists and __ERROR is a 3-elements list.
286283

287-
struct_code = (
288-
"""
289-
namespace {
290-
struct %(name)s {
284+
struct_code = """
285+
namespace {{
286+
struct {name} {{
291287
PyObject* __ERROR;
292288
293-
%(storage_decl)s
294-
%(struct_decl)s
289+
{storage_decl}
290+
{struct_decl}
295291
296-
%(name)s() {
292+
{name}() {{
297293
// This is only somewhat safe because we:
298294
// 1) Are not a virtual class
299295
// 2) Do not use any virtual classes in the members
@@ -306,32 +302,30 @@ def struct_gen(args, struct_builders, blocks, sub):
306302
#ifndef PYTENSOR_DONT_MEMSET_STRUCT
307303
memset(this, 0, sizeof(*this));
308304
#endif
309-
}
310-
~%(name)s(void) {
305+
}}
306+
~{name}(void) {{
311307
cleanup();
312-
}
308+
}}
313309
314-
int init(PyObject* __ERROR, %(args_decl)s) {
315-
%(storage_incref)s
316-
%(storage_set)s
317-
%(struct_init_head)s
310+
int init(PyObject* __ERROR, {args_decl}) {{
311+
{storage_incref}
312+
{storage_set}
313+
{struct_init_head}
318314
this->__ERROR = __ERROR;
319315
return 0;
320-
}
321-
void cleanup(void) {
322-
%(struct_cleanup)s
323-
%(storage_decref)s
324-
}
325-
int run(void) {
326-
int %(failure_var)s = 0;
327-
%(behavior)s
328-
%(do_return)s
329-
}
330-
};
331-
}
332-
"""
333-
% sub
334-
)
316+
}}
317+
void cleanup(void) {{
318+
{struct_cleanup}
319+
{storage_decref}
320+
}}
321+
int run(void) {{
322+
int {failure_var} = 0;
323+
{behavior}
324+
{do_return}
325+
}}
326+
}};
327+
}}
328+
""".format(**sub)
335329

336330
return struct_code
337331

@@ -380,9 +374,9 @@ def get_c_init(fgraph, r, name, sub):
380374
pre = (
381375
""
382376
"""
383-
py_%(name)s = Py_None;
384-
{Py_XINCREF(py_%(name)s);}
385-
""" % locals()
377+
py_{name} = Py_None;
378+
{{Py_XINCREF(py_{name});}}
379+
""".format(**locals())
386380
)
387381
return pre + r.type.c_init(name, sub)
388382

@@ -418,9 +412,9 @@ def get_c_extract(fgraph, r, name, sub):
418412
c_extract = r.type.c_extract(name, sub, False)
419413

420414
pre = """
421-
py_%(name)s = PyList_GET_ITEM(storage_%(name)s, 0);
422-
{Py_XINCREF(py_%(name)s);}
423-
""" % locals()
415+
py_{name} = PyList_GET_ITEM(storage_{name}, 0);
416+
{{Py_XINCREF(py_{name});}}
417+
""".format(**locals())
424418
return pre + c_extract
425419

426420

@@ -447,9 +441,9 @@ def get_c_extract_out(fgraph, r, name, sub):
447441
c_extract = r.type.c_extract_out(name, sub, check_input, check_broadcast=False)
448442

449443
pre = """
450-
py_%(name)s = PyList_GET_ITEM(storage_%(name)s, 0);
451-
{Py_XINCREF(py_%(name)s);}
452-
""" % locals()
444+
py_{name} = PyList_GET_ITEM(storage_{name}, 0);
445+
{{Py_XINCREF(py_{name});}}
446+
""".format(**locals())
453447
return pre + c_extract
454448

455449

@@ -459,8 +453,8 @@ def get_c_cleanup(fgraph, r, name, sub):
459453
460454
"""
461455
post = """
462-
{Py_XDECREF(py_%(name)s);}
463-
""" % locals()
456+
{{Py_XDECREF(py_{name});}}
457+
""".format(**locals())
464458
return r.type.c_cleanup(name, sub) + post
465459

466460

@@ -470,14 +464,14 @@ def get_c_sync(fgraph, r, name, sub):
470464
471465
"""
472466
return """
473-
if (!%(failure_var)s) {
474-
%(sync)s
475-
PyObject* old = PyList_GET_ITEM(storage_%(name)s, 0);
476-
{Py_XINCREF(py_%(name)s);}
477-
PyList_SET_ITEM(storage_%(name)s, 0, py_%(name)s);
478-
{Py_XDECREF(old);}
479-
}
480-
""" % dict(sync=r.type.c_sync(name, sub), name=name, **sub)
467+
if (!{failure_var}) {{
468+
{sync}
469+
PyObject* old = PyList_GET_ITEM(storage_{name}, 0);
470+
{{Py_XINCREF(py_{name});}}
471+
PyList_SET_ITEM(storage_{name}, 0, py_{name});
472+
{{Py_XDECREF(old);}}
473+
}}
474+
""".format(**dict(sync=r.type.c_sync(name, sub), name=name, **sub))
481475

482476

483477
def apply_policy(fgraph, policy, r, name, sub):

pytensor/link/c/cmodule.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,14 +1950,13 @@ def _try_flags(
19501950

19511951
code = (
19521952
"""
1953-
%(preamble)s
1953+
{preamble}
19541954
int main(int argc, char** argv)
1955-
{
1956-
%(body)s
1955+
{{
1956+
{body}
19571957
return 0;
1958-
}
1959-
"""
1960-
% locals()
1958+
}}
1959+
""".format(**locals())
19611960
).encode()
19621961
return cls._try_compile_tmp(
19631962
code,

pytensor/link/c/interface.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,18 +558,20 @@ def c_extract_out(
558558
559559
"""
560560
return """
561-
if (py_%(name)s == Py_None)
562-
{
563-
%(c_init_code)s
564-
}
561+
if (py_{name} == Py_None)
562+
{{
563+
{c_init_code}
564+
}}
565565
else
566-
{
567-
%(c_extract_code)s
568-
}
569-
""" % dict(
570-
name=name,
571-
c_init_code=self.c_init(name, sub),
572-
c_extract_code=self.c_extract(name, sub, check_input),
566+
{{
567+
{c_extract_code}
568+
}}
569+
""".format(
570+
**dict(
571+
name=name,
572+
c_init_code=self.c_init(name, sub),
573+
c_extract_code=self.c_extract(name, sub, check_input),
574+
)
573575
)
574576

575577
def c_cleanup(self, name: str, sub: dict[str, str]) -> str:

pytensor/link/c/op.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -596,20 +596,22 @@ def c_code(self, node, name, inp, out, sub):
596596

597597
# Generate the C code
598598
return """
599-
%(define_macros)s
600-
{
601-
if (%(func_name)s(%(func_args)s%(params)s) != 0) {
602-
%(fail)s
603-
}
604-
}
605-
%(undef_macros)s
606-
""" % dict(
607-
func_name=self.func_name,
608-
fail=sub["fail"],
609-
params=params,
610-
func_args=self.format_c_function_args(inp, out),
611-
define_macros=define_macros,
612-
undef_macros=undef_macros,
599+
{define_macros}
600+
{{
601+
if ({func_name}({func_args}{params}) != 0) {{
602+
{fail}
603+
}}
604+
}}
605+
{undef_macros}
606+
""".format(
607+
**dict(
608+
func_name=self.func_name,
609+
fail=sub["fail"],
610+
params=params,
611+
func_args=self.format_c_function_args(inp, out),
612+
define_macros=define_macros,
613+
undef_macros=undef_macros,
614+
)
613615
)
614616
else:
615617
if "code" in self.code_sections:

0 commit comments

Comments
 (0)