diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 004e41b..656a26f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: - id: mixed-line-ending - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.0 + rev: v0.15.4 hooks: # Run the linter. - id: ruff-check @@ -40,7 +40,7 @@ repos: - id: ruff-format types_or: [ python, pyi ] - repo: https://github.com/compilerla/conventional-pre-commit - rev: v4.3.0 + rev: v4.4.0 hooks: - id: conventional-pre-commit # stages: [commit-msg] diff --git a/docs/source/conf.py b/docs/source/conf.py index f2aa4a3..ae52158 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -22,7 +22,7 @@ # -- Project information ----------------------------------------------------- project = "ReLeSO" -copyright = "2023, Clemens Fricke" # noqa: A001 +copyright = "2023, Clemens Fricke" author = "Clemens Fricke" # The full version, including alpha/beta/rc tags diff --git a/releso/callback.py b/releso/callback.py index 4cd50a4..737e1b2 100644 --- a/releso/callback.py +++ b/releso/callback.py @@ -205,12 +205,10 @@ def __init__( ["stdout", "csv"], ) self._logger.info( - ( - "The StepLogCallback should only be used for debugging purposes" - " and only for use-cases with a smallish observation space." - " Otherwise it will create a lot of data and slow down the" - " training process. Use with caution!" - ) + "The StepLogCallback should only be used for debugging purposes" + " and only for use-cases with a smallish observation space." + " Otherwise it will create a lot of data and slow down the" + " training process. Use with caution!" ) self._reset_internal_storage() diff --git a/releso/geometry.py b/releso/geometry.py index 2162b5c..791f279 100644 --- a/releso/geometry.py +++ b/releso/geometry.py @@ -247,7 +247,7 @@ def _parse_string_to_int(string: str) -> int: seed = _parse_string_to_int(str(seed)) if seed is not None else seed self.get_logger().debug( f"A random action is applied during reset with the following " - f"seed {str(seed)}" + f"seed {seed!s}" ) rng_gen = np.random.default_rng(seed) if self.discrete_actions: diff --git a/releso/mesh.py b/releso/mesh.py index 583eefb..b0e3b37 100644 --- a/releso/mesh.py +++ b/releso/mesh.py @@ -266,7 +266,7 @@ def validate_mxyz_mien_path(cls, values: Dict[str, Any]) -> Dict[str, Any]: "Mesh files are defined by mxyz_path and mien_path." ) return values - elif "path" in values.keys() and values["path"]: + elif values.get("path"): get_parser_logger().debug( "Trying to find mxyz_path and mien_path from path variable." ) diff --git a/releso/spline.py b/releso/spline.py index 7bcafc3..b178f71 100644 --- a/releso/spline.py +++ b/releso/spline.py @@ -62,9 +62,9 @@ def validate_knot_vector( float: value of the validated value. """ if ( - "number_of_points" in values.keys() - and "degree" in values.keys() - and "name" in values.keys() + "number_of_points" in values + and "degree" in values + and "name" in values ): n_knots = values["number_of_points"] + values["degree"] + 1 else: @@ -165,12 +165,12 @@ def make_default_control_point_grid( List[List[VariableLocation]]: Definition of the control_points """ if not values.get("control_points"): - if "space_dimensions" not in values.keys(): + if "space_dimensions" not in values: raise ParserException( "SplineDefinition", "control_point_variables", "During validation the prerequisite variable " - f"space_dimensions was not present. {str(values)}", + f"space_dimensions was not present. {values!s}", ) spline_dimensions = values["space_dimensions"] n_points_in_dim = [ diff --git a/releso/spor.py b/releso/spor.py index ed21f4f..26b014b 100644 --- a/releso/spor.py +++ b/releso/spor.py @@ -222,7 +222,7 @@ def validate_additional_observations( # TODO: change name to something more meaningful for example # using the function name values["name"] v = { - "name": f"unnamed_{str(uuid4())}", + "name": f"unnamed_{uuid4()!s}", "value_min": -1, "value_max": 1, "observation_shape": [int(v)], @@ -423,10 +423,7 @@ def validate_add_step_information_only_true_if_also_spor_com_is_true( str: value """ if v: - if ( - "use_communication_interface" in values - and values["use_communication_interface"] - ): + if values.get("use_communication_interface"): pass else: raise ParserException( @@ -1016,7 +1013,7 @@ def run( self.save_location / self.python_file_path.name, ) self.get_logger().debug( - f"Successfully copied the python file {str(ret_path)}" + f"Successfully copied the python file {ret_path!s}" ) # trying to internalize the function @@ -1026,7 +1023,7 @@ def run( except ModuleNotFoundError as err: raise RuntimeError( f"Could not load the python file at " - f"{str(self.python_file_path)}" + f"{self.python_file_path!s}" ) from err else: try: @@ -1034,7 +1031,7 @@ def run( except AttributeError as err: raise RuntimeError( f"Could not get main function from python file" - f" {str(self.python_file_path)}." + f" {self.python_file_path!s}." ) from err else: self._run_logger = set_up_logger( diff --git a/releso/util/util_funcs.py b/releso/util/util_funcs.py index cf07eda..1e16a91 100644 --- a/releso/util/util_funcs.py +++ b/releso/util/util_funcs.py @@ -40,7 +40,7 @@ def default(self, o: Any) -> Any: try: return json.JSONEncoder.default(self, o) except TypeError: - print(type(o), o) # noqa: T201 + print(type(o), o) return json.JSONEncoder.default(self, "") @@ -91,7 +91,7 @@ def call_commandline(command, folder, logger=None): output = subprocess.check_output( command, shell=True, - cwd=folder, # noqa: S602 + cwd=folder, ) exitcode = 0 except subprocess.CalledProcessError as exc: diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py index 1a3ea44..d820491 100644 --- a/tests/test_base_parser.py +++ b/tests/test_base_parser.py @@ -1,5 +1,5 @@ -from typing import Any, Dict, List, Union import pathlib +from typing import Any, Dict, List, Union import pytest @@ -17,9 +17,7 @@ def recursive_remove_save_location( if "save_location" in dict_to_clean: del dict_to_clean["save_location"] for value in dict_to_clean.values(): - if isinstance(value, dict): - recursive_remove_save_location(value) - elif isinstance(value, list): + if isinstance(value, dict) or isinstance(value, list): recursive_remove_save_location(value) diff --git a/tests/test_mesh.py b/tests/test_mesh.py index c6a7ec7..871adc1 100644 --- a/tests/test_mesh.py +++ b/tests/test_mesh.py @@ -322,7 +322,7 @@ def test_mixd_mesh_export_and_get( ): mesh_exporter = MeshExporter( format="mixd", - export_path=f"{str(dir_save_location)}/export/" + "{}/test.xns", + export_path=f"{dir_save_location!s}/export/" + "{}/test.xns", save_location=dir_save_location, ) mesh = MeshIOMesh( @@ -333,7 +333,7 @@ def test_mixd_mesh_export_and_get( ) mesh.adapt_export_path("123") mesh.export.export_mesh(mesh.get_mesh()) - exported_file = pathlib.Path(f"{str(dir_save_location)}/export") + exported_file = pathlib.Path(f"{dir_save_location!s}/export") mesh_exporter.mesh_format = "other" with pytest.raises(RuntimeError) as err: mesh_exporter.export_mesh(None) diff --git a/tests/test_parser_environment.py b/tests/test_parser_environment.py index 6c63f32..565170b 100644 --- a/tests/test_parser_environment.py +++ b/tests/test_parser_environment.py @@ -600,7 +600,7 @@ def test_parser_environment_non_geometry_observations( "reward_on_error": -1, "working_directory": str(dir_save_location), "python_file_path": ( - f"{str(file_path)}/samples" + f"{file_path!s}/samples" "/spor_python_scripts_tests/file_exists_has_main.py" ), "use_communication_interface": True, @@ -643,7 +643,7 @@ def test_parser_environment_validation( "reward_on_error": -1, "working_directory": str(dir_save_location), "python_file_path": ( - f"{str(file_path)}/samples" + f"{file_path!s}/samples" "/spor_python_scripts_tests/file_exists_has_main.py" ), "use_communication_interface": True, diff --git a/tests/test_reward_helpers.py b/tests/test_reward_helpers.py index 22fef76..57c1ae6 100644 --- a/tests/test_reward_helpers.py +++ b/tests/test_reward_helpers.py @@ -266,9 +266,9 @@ def test_parse_communication_interface_json( ): args = [] args.extend([ - f"-r{str(uuid.uuid4())}", - f"-e{str(uuid.uuid4())}", - f"-l{str(dir_save_location)}", + f"-r{uuid.uuid4()!s}", + f"-e{uuid.uuid4()!s}", + f"-l{dir_save_location!s}", ]) if j: args.extend(["-j", j]) @@ -285,11 +285,7 @@ def test_parse_communication_interface_json( assert error_j in err return argparse_ret = spor_com_parse_arguments(args) - if json_object: - assert argparse_ret.json_object == {"test": 12, "other": "test"} - elif additional_values: - assert argparse_ret.json_object == {"test": 12, "other": "test"} - elif j: + if json_object or additional_values or j: assert argparse_ret.json_object == {"test": 12, "other": "test"} else: assert argparse_ret.json_object is None diff --git a/tests/test_shape_parameterization.py b/tests/test_shape_parameterization.py index dc99e53..a68d93d 100644 --- a/tests/test_shape_parameterization.py +++ b/tests/test_shape_parameterization.py @@ -94,12 +94,12 @@ def test_variable_location_min_max_location( def test_variable_location_is_action( current_position, min_value, max_value, is_action, dir_save_location ): - variable_location = VariableLocation(**{ - "current_position": current_position, - "min_value": min_value, - "max_value": max_value, - "save_location": dir_save_location, - }) + variable_location = VariableLocation( + current_position=current_position, + min_value=min_value, + max_value=max_value, + save_location=dir_save_location, + ) assert variable_location.is_action() is is_action @@ -294,14 +294,14 @@ def test_variable_location_discrete_action( expect_position, dir_save_location, ): - variable_location = VariableLocation(**{ - "current_position": current_position, - "min_value": min_value, - "max_value": max_value, - "n_steps": n_steps, - "step": step, - "save_location": dir_save_location, - }) + variable_location = VariableLocation( + current_position=current_position, + min_value=min_value, + max_value=max_value, + n_steps=n_steps, + step=step, + save_location=dir_save_location, + ) last_current_position = current_position for iteration in iterations: last_current_position = variable_location.apply_discrete_action( @@ -363,14 +363,14 @@ def test_variable_location_continuous_action( expect_position, dir_save_location, ): - variable_location = VariableLocation(**{ - "current_position": current_position, - "min_value": min_value, - "max_value": max_value, - "n_steps": n_steps, - "step": step, - "save_location": dir_save_location, - }) + variable_location = VariableLocation( + current_position=current_position, + min_value=min_value, + max_value=max_value, + n_steps=n_steps, + step=step, + save_location=dir_save_location, + ) last_current_position = current_position for iteration in iterations: last_current_position = variable_location.apply_continuous_action( @@ -580,10 +580,9 @@ def test_shape_definition_get_actions( point["save_location"] = dir_save_location new_cps.append(VariableLocation(**point)) new_control_points.append(new_cps) - shape = ShapeDefinition(**{ - "control_points": new_control_points, - "save_location": dir_save_location, - }) + shape = ShapeDefinition( + control_points=new_control_points, save_location=dir_save_location + ) actions = shape.get_actions() assert len(actions) == n_actions diff --git a/tests/test_spor.py b/tests/test_spor.py index 7c7f27e..714095d 100644 --- a/tests/test_spor.py +++ b/tests/test_spor.py @@ -499,7 +499,7 @@ def test_spor_object_executor_spor_com_interface( else: assert "--reset" not in interface_string if validation_id is not None: - assert f"--validation_value {str(validation_id)}" in interface_string + assert f"--validation_value {validation_id!s}" in interface_string else: assert "--validation_value" not in interface_string if add_step_info: diff --git a/tests/test_verbosity.py b/tests/test_verbosity.py index 69d2eb5..cfc9361 100644 --- a/tests/test_verbosity.py +++ b/tests/test_verbosity.py @@ -97,12 +97,12 @@ def test_verbosity_default( with caplog.at_level(VerbosityLevel.DEBUG): verbosity = Verbosity(**calling_dict) assert ( - f"Parser logger has logging level: {str(wanted_parser.value)}" + f"Parser logger has logging level: {wanted_parser.value!s}" in caplog.text ) assert ( f"Environment logger has logging level: " - f"{str(wanted_environment.value)}" + f"{wanted_environment.value!s}" ) in caplog.text else: verbosity = Verbosity(**calling_dict)