From c2a46a34e663bce469b91e933cd6a2c379484ca2 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:20:45 +0100 Subject: [PATCH 1/2] Fix ruff/pyupgrade issues (UP030) UP030 Use implicit references for positional format fields https://docs.astral.sh/ruff/rules/format-literals/ --- capsul/in_context/freesurfer.py | 8 ++--- .../test/test_complex_pipeline_activations.py | 4 +-- capsul/process/nipype_process.py | 34 ++++++++++++------- capsul/process/node.py | 7 ++-- capsul/process/process.py | 14 ++++---- capsul/process/test/test_runprocess.py | 2 +- .../qt_gui/widgets/pipeline_developer_view.py | 2 +- ruff.toml | 1 - 8 files changed, 35 insertions(+), 37 deletions(-) diff --git a/capsul/in_context/freesurfer.py b/capsul/in_context/freesurfer.py index bbd711df..d008b4e5 100644 --- a/capsul/in_context/freesurfer.py +++ b/capsul/in_context/freesurfer.py @@ -76,9 +76,7 @@ def freesurfer_command_with_environment( cmd = [ shell, "-c", - 'setenv FREESURFER_HOME "{0}"; source {1}; exec {2} '.format( - freesurfer_dir, freesurfer_script, command[0] - ) + f'setenv FREESURFER_HOME "{freesurfer_dir}"; source {freesurfer_script}; exec {command[0]} ' + " ".join("'%s'" % i.replace("'", "\\'") for i in command[1:]), ] @@ -88,9 +86,7 @@ def freesurfer_command_with_environment( cmd = [ shell, "-c", - 'export FREESURFER_HOME="{0}"; source {1}; exec {2} '.format( - freesurfer_dir, freesurfer_script, command[0] - ) + f'export FREESURFER_HOME="{freesurfer_dir}"; source {freesurfer_script}; exec {command[0]} ' + " ".join("'%s'" % i.replace("'", "\\'") for i in command[1:]), ] diff --git a/capsul/pipeline/test/test_complex_pipeline_activations.py b/capsul/pipeline/test/test_complex_pipeline_activations.py index 09cc31f4..88062801 100644 --- a/capsul/pipeline/test/test_complex_pipeline_activations.py +++ b/capsul/pipeline/test/test_complex_pipeline_activations.py @@ -862,14 +862,14 @@ def test_complex_activations(self): f"Pipeline {node_pipeline.pipeline} has no node named {node_name}" ) from e try: - what = "activation of node {0}".format( + what = "activation of node {}".format( full_node_name or "main pipeline node" ) expected = node_activations.get("_activated") if expected is not None: got = node.activated self.assertEqual(expected, got) - what = "enabled for node {0}".format( + what = "enabled for node {}".format( full_node_name or "main pipeline node" ) expected = node_activations.get("_enabled") diff --git a/capsul/process/nipype_process.py b/capsul/process/nipype_process.py index 94b20f4f..78b7e8db 100644 --- a/capsul/process/nipype_process.py +++ b/capsul/process/nipype_process.py @@ -11,6 +11,7 @@ import logging import os import sys +import textwrap import types import soma.controller as sc @@ -207,11 +208,12 @@ def sync_process_output_traits(process_instance): traceback.print_exc() ex_type, ex, tb = sys.exc_info() logging.debug( - "Something wrong in the nipype output trait " - "synchronization:\n\n\tError: {0} - {1}\n" - "\tTraceback:\n{2}".format( - ex_type, ex, "".join(traceback.format_tb(tb)) - ) + textwrap.dedent(f"""\ + Something wrong in the nipype output trait synchronization: + + \tError: {ex_type} - {ex} + \tTraceback: + {''.join(traceback.format_tb(tb))}""") ) nipype_outputs = {} @@ -247,10 +249,13 @@ def sync_process_output_traits(process_instance): traceback.print_exc() ex_type, ex, tb = sys.exc_info() logging.debug( - "Something wrong in the nipype output trait " - "synchronization:\n\n\tError: {0} - {1}\n" - "\tTraceback:\n{2}".format( - ex_type, ex, "".join(traceback.format_tb(tb)) + textwrap.dedent( + f"""\ + Something wrong in the nipype output trait synchronization: + + \tError: {ex_type} - {ex} + \tTraceback: + {''.join(traceback.format_tb(tb))}""" ) ) @@ -278,10 +283,13 @@ def sync_process_output_traits(process_instance): traceback.print_exc() ex_type, ex, tb = sys.exc_info() logging.debug( - "Something wrong in the nipype output trait " - "synchronization:\n\n\tError: {0} - {1}\n" - "\tTraceback:\n{2}".format( - ex_type, ex, "".join(traceback.format_tb(tb)) + textwrap.dedent( + f"""\ + Something wrong in the nipype output trait synchronization: + + \tError: {ex_type} - {ex} + \tTraceback: + {"".join(traceback.format_tb(tb))}""" ) ) diff --git a/capsul/process/node.py b/capsul/process/node.py index e8fd991e..37e79707 100644 --- a/capsul/process/node.py +++ b/capsul/process/node.py @@ -254,8 +254,7 @@ def __init__( # as specified in the docstring if "name" not in parameter: raise Exception( - "Can't create parameter with unknown" - "identifier and parameter {0}".format(parameter) + f"Can't create parameter with unknown identifier and parameter {parameter}" ) parameter = parameter.copy() # force the parameter type @@ -264,9 +263,7 @@ def __init__( self._add_plug(parameter) else: raise Exception( - "Can't create Node. Expect a dict structure " - "to initialize the Node, " - "got {0}: {1}".format(type(parameter), parameter) + f"Can't create Node. Expect a dict structure to initialize the Node, got {type(parameter)}: {parameter}" ) def __del__(self): diff --git a/capsul/process/process.py b/capsul/process/process.py index d46b1dbb..5c455c6c 100644 --- a/capsul/process/process.py +++ b/capsul/process/process.py @@ -290,7 +290,7 @@ def get_help(self, returnhelp=False, use_labels=False): # Update the documentation with a description of the pipeline # when the xml to pipeline wrapper has been used if returnhelp and hasattr(self, "_pipeline_desc"): - str_desc = "".join([" {0}".format(line) for line in self._pipeline_desc]) + str_desc = "".join(f" {line}" for line in self._pipeline_desc) doctring += [ ".. hidden-code-block:: python", " :starthidden: True", @@ -307,26 +307,24 @@ def get_help(self, returnhelp=False, use_labels=False): # when the function to process wrapper has been used if hasattr(self, "_func_name") and hasattr(self, "_func_module"): doctring += [ - "This process has been wrapped from {0}.{1}.".format( - self._func_module, self._func_name - ), + f"This process has been wrapped from {self._func_module}.{self._func_name}.", "", ] if returnhelp: doctring += [ - ".. currentmodule:: {0}".format(self._func_module), + f".. currentmodule:: {self._func_module}", "", ".. autosummary::", " :toctree: ./", "", - " {0}".format(self._func_name), + f" {self._func_name}", "", ] # Append the input and output fields help if use_labels: - in_label = [".. _%s.%s_inputs:\n\n" % (self.__module__, self.name)] - out_label = [".. _%s.%s_outputs:\n\n" % (self.__module__, self.name)] + in_label = [f".. _{self.__module__}.{self.name}_inputs:\n\n"] + out_label = [f".. _{self.__module__}.{self.name}_outputs:\n\n"] else: in_label = [] out_label = [] diff --git a/capsul/process/test/test_runprocess.py b/capsul/process/test/test_runprocess.py index 8a84d918..e09736fa 100644 --- a/capsul/process/test/test_runprocess.py +++ b/capsul/process/test/test_runprocess.py @@ -14,7 +14,7 @@ class DummyProcess(Process): f: field(type_=float, doc="help for parameter f") def execute(self, context=None): - print("DummyProcess exec, f={0}".format(self.f)) + print(f"DummyProcess exec, f={self.f}") class TestRunProcess(unittest.TestCase): diff --git a/capsul/qt_gui/widgets/pipeline_developer_view.py b/capsul/qt_gui/widgets/pipeline_developer_view.py index 2dd442de..57788472 100644 --- a/capsul/qt_gui/widgets/pipeline_developer_view.py +++ b/capsul/qt_gui/widgets/pipeline_developer_view.py @@ -617,7 +617,7 @@ def _colored_text_item(self, label, text=None, margin=2): label2 = QtGui.QLabel(text) label2.setObjectName("label") label2.setStyleSheet( - "background: rgba({0}, {1}, {2}, 255); " + "background: rgba({}, {}, {}, 255); " "border-radius: 7px; border: 0px solid; " "padding: 1px;".format(*color) ) diff --git a/ruff.toml b/ruff.toml index c5ff7255..8a4c23f2 100644 --- a/ruff.toml +++ b/ruff.toml @@ -19,7 +19,6 @@ ignore = [ "E741", "E501", "UP015", - "UP030", "UP031", "UP032", # https://docs.astral.sh/ruff/rules/redundant-open-modes/ From f22e86453e2b08592513f75bee773ff46e641318 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:24:21 +0100 Subject: [PATCH 2/2] Fix ruff/pyupgrade issues (UP032) UP032 Use f-string instead of `format` call https://docs.astral.sh/ruff/rules/format-literals/ --- capsul/test/__main__.py | 8 ++++---- ruff.toml | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/capsul/test/__main__.py b/capsul/test/__main__.py index 1d86d865..759fead9 100644 --- a/capsul/test/__main__.py +++ b/capsul/test/__main__.py @@ -63,13 +63,13 @@ capsul_src = Path(__file__).parent.parent if args.html: Path(args.html).mkdir(exist_ok=True) - pytest_command += ["--cov=capsul", "--html={}/tests.html".format(args.html)] + pytest_command += ["--cov=capsul", f"--html={args.html}/tests.html"] coverage_command = [sys.executable, "-m", "coverage", "html", "-d", args.html] env = os.environ.copy() - print(" ".join("'{}'".format(i) for i in pytest_command)) + print(" ".join(f"'{i}'" for i in pytest_command)) subprocess.check_call(pytest_command, env=env, cwd=capsul_src) - print(" ".join("'{}'".format(i) for i in coverage_command)) + print(" ".join(f"'{i}'" for i in coverage_command)) subprocess.check_call(coverage_command) else: - print(" ".join("'{}'".format(i) for i in pytest_command)) + print(" ".join(f"'{i}'" for i in pytest_command)) subprocess.check_call(pytest_command, cwd=capsul_src) diff --git a/ruff.toml b/ruff.toml index 8a4c23f2..07b9adf3 100644 --- a/ruff.toml +++ b/ruff.toml @@ -20,7 +20,6 @@ ignore = [ "E501", "UP015", "UP031", - "UP032", # https://docs.astral.sh/ruff/rules/redundant-open-modes/ # we prefer explicit to implicit open modes "UP015",