From 527dfa9e322cdffbe146980cc533ea1ab643f5d9 Mon Sep 17 00:00:00 2001 From: Ivan Ruiz Manuel <72193617+irm-codebase@users.noreply.github.com> Date: Mon, 30 Sep 2024 19:35:50 +0200 Subject: [PATCH] fix: PR CI ruff autofixes --- docs/examples/loading_tabular_data.py | 3 +- docs/examples/national_scale/notebook.py | 3 +- docs/examples/piecewise_constraints.py | 3 +- docs/examples/urban_scale/notebook.py | 3 +- docs/hooks/generate_math_docs.py | 3 +- docs/hooks/generate_plots.py | 3 +- docs/hooks/generate_readable_schema.py | 3 +- tests/common/util.py | 3 +- tests/conftest.py | 3 +- tests/test_backend_expression_parser.py | 47 ++++++++++++------------ tests/test_backend_general.py | 9 ++--- tests/test_backend_gurobi.py | 9 ++--- tests/test_backend_helper_functions.py | 5 +-- tests/test_backend_latex_backend.py | 1 - tests/test_backend_module.py | 1 - tests/test_backend_parsing.py | 47 ++++++++++++------------ tests/test_backend_pyomo.py | 27 +++++++------- tests/test_backend_pyomo_objective.py | 3 +- tests/test_backend_where_parser.py | 29 +++++++-------- tests/test_cli.py | 5 +-- tests/test_constraint_results.py | 13 +++---- tests/test_core_attrdict.py | 9 ++--- tests/test_core_model.py | 5 +-- tests/test_core_preprocess.py | 5 +-- tests/test_core_util.py | 9 ++--- tests/test_example_models.py | 17 ++++----- tests/test_io.py | 5 +-- tests/test_math.py | 5 +-- tests/test_preprocess_data_sources.py | 3 +- tests/test_preprocess_model_data.py | 15 ++++---- tests/test_preprocess_time.py | 1 - 31 files changed, 133 insertions(+), 164 deletions(-) diff --git a/docs/examples/loading_tabular_data.py b/docs/examples/loading_tabular_data.py index edaaf819..8dcd3327 100644 --- a/docs/examples/loading_tabular_data.py +++ b/docs/examples/loading_tabular_data.py @@ -22,9 +22,8 @@ # %% from pathlib import Path -import pandas as pd - import calliope +import pandas as pd calliope.set_log_verbosity("INFO", include_solver_output=False) diff --git a/docs/examples/national_scale/notebook.py b/docs/examples/national_scale/notebook.py index 70e6d114..e6de74a4 100644 --- a/docs/examples/national_scale/notebook.py +++ b/docs/examples/national_scale/notebook.py @@ -18,11 +18,10 @@ # This notebook will show you how to load, build, solve, and examine the results of the national scale example model. # %% +import calliope import pandas as pd import plotly.express as px -import calliope - # We increase logging verbosity calliope.set_log_verbosity("INFO", include_solver_output=False) diff --git a/docs/examples/piecewise_constraints.py b/docs/examples/piecewise_constraints.py index 064be53d..67dc8c6a 100644 --- a/docs/examples/piecewise_constraints.py +++ b/docs/examples/piecewise_constraints.py @@ -21,11 +21,10 @@ # %% +import calliope import numpy as np import plotly.express as px -import calliope - calliope.set_log_verbosity("INFO", include_solver_output=False) # %% [markdown] diff --git a/docs/examples/urban_scale/notebook.py b/docs/examples/urban_scale/notebook.py index 4374fce9..9cc45947 100644 --- a/docs/examples/urban_scale/notebook.py +++ b/docs/examples/urban_scale/notebook.py @@ -18,11 +18,10 @@ # This notebook will show you how to load, build, solve, and examine the results of the urban scale example model. # %% +import calliope import pandas as pd import plotly.express as px -import calliope - # We increase logging verbosity calliope.set_log_verbosity("INFO", include_solver_output=False) diff --git a/docs/hooks/generate_math_docs.py b/docs/hooks/generate_math_docs.py index 257d7e29..607766d9 100644 --- a/docs/hooks/generate_math_docs.py +++ b/docs/hooks/generate_math_docs.py @@ -8,10 +8,9 @@ import textwrap from pathlib import Path -from mkdocs.structure.files import File - import calliope from calliope.postprocess.math_documentation import MathDocumentation +from mkdocs.structure.files import File logger = logging.getLogger("mkdocs") diff --git a/docs/hooks/generate_plots.py b/docs/hooks/generate_plots.py index 256085f9..9a606adc 100644 --- a/docs/hooks/generate_plots.py +++ b/docs/hooks/generate_plots.py @@ -5,13 +5,12 @@ import tempfile from pathlib import Path +import calliope import pandas as pd import plotly.graph_objects as go import xarray as xr from mkdocs.structure.files import File -import calliope - TEMPDIR = tempfile.TemporaryDirectory() diff --git a/docs/hooks/generate_readable_schema.py b/docs/hooks/generate_readable_schema.py index 296242e2..8b104351 100644 --- a/docs/hooks/generate_readable_schema.py +++ b/docs/hooks/generate_readable_schema.py @@ -12,9 +12,8 @@ from pathlib import Path import jsonschema2md -from mkdocs.structure.files import File - from calliope.util import schema +from mkdocs.structure.files import File TEMPDIR = tempfile.TemporaryDirectory() diff --git a/tests/common/util.py b/tests/common/util.py index c888a5f9..eb75d2e7 100644 --- a/tests/common/util.py +++ b/tests/common/util.py @@ -2,11 +2,10 @@ from pathlib import Path from typing import Literal -import xarray as xr - import calliope import calliope.backend import calliope.preprocess +import xarray as xr def build_test_model( diff --git a/tests/conftest.py b/tests/conftest.py index 31f64a75..53bbf4bb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,6 @@ import numpy as np import pytest import xarray as xr - from calliope.attrdict import AttrDict from calliope.backend import latex_backend_model, pyomo_backend_model from calliope.preprocess import CalliopeMath @@ -54,7 +53,7 @@ def simple_supply(): return m -@pytest.fixture +@pytest.fixture() def simple_supply_build_func(): m = build_model({}, "simple_supply,two_hours,investment_costs") m.build() diff --git a/tests/test_backend_expression_parser.py b/tests/test_backend_expression_parser.py index ded1feb9..e68f24f0 100644 --- a/tests/test_backend_expression_parser.py +++ b/tests/test_backend_expression_parser.py @@ -6,7 +6,6 @@ import pyparsing as pp import pytest import xarray as xr - from calliope import exceptions from calliope.backend import expression_parser, helper_functions @@ -37,40 +36,40 @@ def as_array(self, x, y): return x * 10 + y -@pytest.fixture +@pytest.fixture() def valid_component_names(): return ["foo", "with_inf", "only_techs", "no_dims", "multi_dim_var", "no_dim_var"] -@pytest.fixture +@pytest.fixture() def base_parser_elements(): number, identifier = expression_parser.setup_base_parser_elements() return number, identifier -@pytest.fixture +@pytest.fixture() def number(base_parser_elements): return base_parser_elements[0] -@pytest.fixture +@pytest.fixture() def identifier(base_parser_elements): return base_parser_elements[1] -@pytest.fixture +@pytest.fixture() def evaluatable_identifier(identifier, valid_component_names): return expression_parser.evaluatable_identifier_parser( identifier, valid_component_names ) -@pytest.fixture +@pytest.fixture() def id_list(number, evaluatable_identifier): return expression_parser.list_parser(number, evaluatable_identifier) -@pytest.fixture +@pytest.fixture() def unsliced_param(): def _unsliced_param(valid_component_names): return expression_parser.unsliced_object_parser(valid_component_names) @@ -78,12 +77,12 @@ def _unsliced_param(valid_component_names): return _unsliced_param -@pytest.fixture +@pytest.fixture() def unsliced_param_with_obj_names(unsliced_param, valid_component_names): return unsliced_param(valid_component_names) -@pytest.fixture +@pytest.fixture() def sliced_param( number, identifier, evaluatable_identifier, unsliced_param_with_obj_names ): @@ -92,12 +91,12 @@ def sliced_param( ) -@pytest.fixture +@pytest.fixture() def sub_expression(identifier): return expression_parser.sub_expression_parser(identifier) -@pytest.fixture +@pytest.fixture() def helper_function( number, sliced_param, @@ -117,7 +116,7 @@ def helper_function( ) -@pytest.fixture +@pytest.fixture() def helper_function_no_nesting( number, sliced_param, @@ -158,7 +157,7 @@ def helper_function_one_parser_in_args(identifier, request): ) -@pytest.fixture +@pytest.fixture() def eval_kwargs(dummy_pyomo_backend_model): return { "helper_functions": helper_functions._registry["expression"], @@ -173,7 +172,7 @@ def eval_kwargs(dummy_pyomo_backend_model): } -@pytest.fixture +@pytest.fixture() def arithmetic( helper_function, number, sliced_param, sub_expression, unsliced_param_with_obj_names ): @@ -186,7 +185,7 @@ def arithmetic( ) -@pytest.fixture +@pytest.fixture() def helper_function_allow_arithmetic( number, sliced_param, @@ -210,22 +209,22 @@ def helper_function_allow_arithmetic( ) -@pytest.fixture +@pytest.fixture() def equation_comparison(arithmetic): return expression_parser.equation_comparison_parser(arithmetic) -@pytest.fixture +@pytest.fixture() def generate_equation(valid_component_names): return expression_parser.generate_equation_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def generate_slice(valid_component_names): return expression_parser.generate_slice_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def generate_sub_expression(valid_component_names): return expression_parser.generate_sub_expression_parser(valid_component_names) @@ -759,11 +758,11 @@ def var_left(self, request): def var_right(self, request): return request.param - @pytest.fixture + @pytest.fixture() def expected_left(self, var_left): return self.EXPR_PARAMS_AND_EXPECTED_EVAL[var_left] - @pytest.fixture + @pytest.fixture() def expected_right(self, var_right): return self.EXPR_PARAMS_AND_EXPECTED_EVAL[var_right] @@ -771,7 +770,7 @@ def expected_right(self, var_right): def operator(self, request): return request.param - @pytest.fixture + @pytest.fixture() def single_equation_simple(self, var_left, var_right, operator): return f"{var_left} {operator} {var_right}" @@ -850,7 +849,7 @@ def test_repr(self, equation_comparison): class TestAsMathString: - @pytest.fixture + @pytest.fixture() def latex_eval_kwargs(self, dummy_latex_backend_model): return { "helper_functions": helper_functions._registry["expression"], diff --git a/tests/test_backend_general.py b/tests/test_backend_general.py index f61443f5..4a406007 100644 --- a/tests/test_backend_general.py +++ b/tests/test_backend_general.py @@ -1,12 +1,11 @@ import logging +import calliope import numpy as np import pandas as pd import pytest # noqa: F401 import xarray as xr -import calliope - from .common.util import build_test_model as build_model from .common.util import check_error_or_warning @@ -26,7 +25,7 @@ def built_model_cls_longnames(backend) -> calliope.Model: return m -@pytest.fixture +@pytest.fixture() def built_model_func_longnames(backend) -> calliope.Model: m = build_model({}, "simple_supply,two_hours,investment_costs") m.build(backend=backend, pre_validate_math_strings=False) @@ -34,7 +33,7 @@ def built_model_func_longnames(backend) -> calliope.Model: return m -@pytest.fixture +@pytest.fixture() def solved_model_func(backend) -> calliope.Model: m = build_model({}, "simple_supply,two_hours,investment_costs") m.build(backend=backend, pre_validate_math_strings=False) @@ -66,7 +65,7 @@ def solved_model_cls(backend) -> calliope.Model: return m -@pytest.fixture +@pytest.fixture() def built_model_func_updated_cost_flow_cap(backend, dummy_int: int) -> calliope.Model: m = build_model({}, "simple_supply,two_hours,investment_costs") m.build(backend=backend, pre_validate_math_strings=False) diff --git a/tests/test_backend_gurobi.py b/tests/test_backend_gurobi.py index 95821860..ab934f2f 100755 --- a/tests/test_backend_gurobi.py +++ b/tests/test_backend_gurobi.py @@ -1,9 +1,8 @@ +import calliope.exceptions as exceptions import gurobipy import pytest # noqa: F401 import xarray as xr -import calliope.exceptions as exceptions - from .common.util import build_test_model as build_model from .common.util import check_error_or_warning @@ -25,7 +24,7 @@ def simple_supply_gurobi(self): m.solve() return m - @pytest.fixture + @pytest.fixture() def simple_supply_gurobi_func(self): m = build_model({}, "simple_supply,two_hours,investment_costs") m.build(backend="gurobi", pre_validate_math_strings=False) @@ -261,13 +260,13 @@ def test_to_lp_wrong_file_extension(self, simple_supply_gurobi, tmp_path): class TestShadowPrices: - @pytest.fixture + @pytest.fixture() def simple_supply(self): m = build_model({}, "simple_supply,two_hours,investment_costs") m.build(backend="gurobi") return m - @pytest.fixture + @pytest.fixture() def supply_milp(self): m = build_model({}, "supply_milp,two_hours,investment_costs") m.build(backend="gurobi") diff --git a/tests/test_backend_helper_functions.py b/tests/test_backend_helper_functions.py index 9f696927..59541a06 100644 --- a/tests/test_backend_helper_functions.py +++ b/tests/test_backend_helper_functions.py @@ -1,7 +1,6 @@ import numpy as np import pytest import xarray as xr - from calliope import exceptions from calliope.backend import helper_functions @@ -72,7 +71,7 @@ def parsing_kwargs(self, dummy_model_data): "return_type": "array", } - @pytest.fixture + @pytest.fixture() def is_defined_any(self, dummy_model_data): def _is_defined(drop_dims, dims): return ( @@ -83,7 +82,7 @@ def _is_defined(drop_dims, dims): return _is_defined - @pytest.fixture + @pytest.fixture() def is_defined_all(self, dummy_model_data): def _is_defined(drop_dims, dims): return ( diff --git a/tests/test_backend_latex_backend.py b/tests/test_backend_latex_backend.py index 446716c3..ef446bdd 100644 --- a/tests/test_backend_latex_backend.py +++ b/tests/test_backend_latex_backend.py @@ -2,7 +2,6 @@ import pytest import xarray as xr - from calliope import exceptions from calliope.backend import latex_backend_model diff --git a/tests/test_backend_module.py b/tests/test_backend_module.py index f220a3b9..7301a5a3 100644 --- a/tests/test_backend_module.py +++ b/tests/test_backend_module.py @@ -1,7 +1,6 @@ """Test backend module functionality (`__init__.py`).""" import pytest - from calliope import backend from calliope.backend.backend_model import BackendModel from calliope.exceptions import BackendError diff --git a/tests/test_backend_parsing.py b/tests/test_backend_parsing.py index 8847738c..e54ca203 100644 --- a/tests/test_backend_parsing.py +++ b/tests/test_backend_parsing.py @@ -2,12 +2,11 @@ from io import StringIO from unittest.mock import patch +import calliope import pyparsing as pp import pytest import ruamel.yaml as yaml import xarray as xr - -import calliope from calliope.backend import backend_model, expression_parser, parsing, where_parser from .common.util import check_error_or_warning @@ -20,7 +19,7 @@ def string_to_dict(yaml_string): return yaml_loader.load(StringIO(yaml_string)) -@pytest.fixture +@pytest.fixture() def component_obj(): setup_string = """ foreach: [A, A1] @@ -32,43 +31,43 @@ def component_obj(): return parsing.ParsedBackendComponent("constraints", "foo", variable_data) -@pytest.fixture +@pytest.fixture() def exists_array(component_obj, dummy_model_data): component_obj.sets = ["nodes", "techs"] return component_obj.combine_definition_matrix_and_foreach(dummy_model_data) -@pytest.fixture +@pytest.fixture() def valid_component_names(dummy_model_data): return ["foo", "bar", "baz", "foobar", *dummy_model_data.data_vars.keys()] -@pytest.fixture +@pytest.fixture() def expression_string_parser(valid_component_names): return expression_parser.generate_equation_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def arithmetic_string_parser(valid_component_names): return expression_parser.generate_arithmetic_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def slice_parser(valid_component_names): return expression_parser.generate_slice_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def sub_expression_parser(valid_component_names): return expression_parser.generate_sub_expression_parser(valid_component_names) -@pytest.fixture +@pytest.fixture() def where_string_parser(): return where_parser.generate_where_string_parser() -@pytest.fixture +@pytest.fixture() def expression_generator(): def _expression_generator(parse_string, where_string=None): expression_dict = {"expression": parse_string} @@ -79,7 +78,7 @@ def _expression_generator(parse_string, where_string=None): return _expression_generator -@pytest.fixture +@pytest.fixture() def generate_expression_list(component_obj, expression_string_parser): def _generate_expression_list(expression_list, **kwargs): return component_obj.generate_expression_list( @@ -100,7 +99,7 @@ def parse_sub_expressions_and_slices( } -@pytest.fixture +@pytest.fixture() def parsed_sub_expression_dict(component_obj, sub_expression_parser): def _parsed_sub_expression_dict(n_foo, n_bar): foos = ", ".join( @@ -123,7 +122,7 @@ def _parsed_sub_expression_dict(n_foo, n_bar): return _parsed_sub_expression_dict -@pytest.fixture +@pytest.fixture() def parsed_slice_dict(component_obj, slice_parser): def _parsed_slice_dict(n_tech1, n_tech2): techs1 = ", ".join(["{where: techs, expression: foo}" for i in range(n_tech1)]) @@ -142,7 +141,7 @@ def _parsed_slice_dict(n_tech1, n_tech2): return _parsed_slice_dict -@pytest.fixture +@pytest.fixture() def obj_with_sub_expressions_and_slices(): def _obj_with_sub_expressions_and_slices(equation_string): if isinstance(equation_string, str): @@ -182,7 +181,7 @@ def _obj_with_sub_expressions_and_slices(equation_string): return _obj_with_sub_expressions_and_slices -@pytest.fixture +@pytest.fixture() def equation_obj(expression_string_parser, where_string_parser): return parsing.ParsedBackendEquation( equation_name="foo", @@ -192,7 +191,7 @@ def equation_obj(expression_string_parser, where_string_parser): ) -@pytest.fixture +@pytest.fixture() def equation_sub_expression_obj(sub_expression_parser, where_string_parser): def _equation_sub_expression_obj(name): return parsing.ParsedBackendEquation( @@ -205,7 +204,7 @@ def _equation_sub_expression_obj(name): return _equation_sub_expression_obj -@pytest.fixture +@pytest.fixture() def equation_slice_obj(slice_parser, where_string_parser): def _equation_slice_obj(name): return parsing.ParsedBackendEquation( @@ -218,7 +217,7 @@ def _equation_slice_obj(name): return _equation_slice_obj -@pytest.fixture +@pytest.fixture() def dummy_backend_interface(dummy_model_data, dummy_model_math): # ignore the need to define the abstract methods from backend_model.BackendModel with patch.multiple(backend_model.BackendModel, __abstractmethods__=set()): @@ -240,7 +239,7 @@ def __init__(self): return DummyBackendModel() -@pytest.fixture +@pytest.fixture() def evaluatable_component_obj(valid_component_names): def _evaluatable_component_obj(equation_expressions): setup_string = f""" @@ -292,7 +291,7 @@ def evaluate_component_where( return component_obj, equation_where_aligned, request.param[1] -@pytest.fixture +@pytest.fixture() def evaluate_component_expression(evaluate_component_where, dummy_backend_interface): component_obj, equation_where, n_true = evaluate_component_where @@ -993,7 +992,7 @@ def test_evaluate_expression(self, evaluate_component_expression): class TestParsedConstraint: - @pytest.fixture + @pytest.fixture() def constraint_obj(self): dict_ = { "foreach": ["techs"], @@ -1045,7 +1044,7 @@ def test_parse_constraint_dict_evaluate_eq2( class TestParsedVariable: - @pytest.fixture + @pytest.fixture() def variable_obj(self): dict_ = {"foreach": ["techs"], "where": "False"} @@ -1067,7 +1066,7 @@ def test_parse_variable_dict_empty_eq1( class TestParsedObjective: - @pytest.fixture + @pytest.fixture() def objective_obj(self): dict_ = { "equations": [ diff --git a/tests/test_backend_pyomo.py b/tests/test_backend_pyomo.py index b42cca63..5f1cb7c4 100755 --- a/tests/test_backend_pyomo.py +++ b/tests/test_backend_pyomo.py @@ -1,18 +1,17 @@ import logging from itertools import product +import calliope +import calliope.backend +import calliope.exceptions as exceptions +import calliope.preprocess import numpy as np import pyomo.core as po import pyomo.kernel as pmo import pytest # noqa: F401 import xarray as xr -from pyomo.core.kernel.piecewise_library.transforms import piecewise_sos2 - -import calliope -import calliope.backend -import calliope.exceptions as exceptions -import calliope.preprocess from calliope.backend import PyomoBackendModel +from pyomo.core.kernel.piecewise_library.transforms import piecewise_sos2 from .common.util import build_test_model as build_model from .common.util import check_error_or_warning, check_variable_exists @@ -1621,7 +1620,7 @@ def simple_supply_updated_cost_flow_cap( simple_supply.backend.update_parameter("cost_flow_cap", dummy_int) return simple_supply - @pytest.fixture + @pytest.fixture() def temp_path(self, tmpdir_factory): return tmpdir_factory.mktemp("custom_math") @@ -2112,31 +2111,31 @@ def test_fails_on_not_reaching_bounds( class TestShadowPrices: - @pytest.fixture + @pytest.fixture() def simple_supply(self): m = build_model({}, "simple_supply,two_hours,investment_costs") m.build() return m - @pytest.fixture + @pytest.fixture() def supply_milp(self): m = build_model({}, "supply_milp,two_hours,investment_costs") m.build() return m - @pytest.fixture + @pytest.fixture() def simple_supply_with_yaml_shadow_prices(self): m = build_model({}, "simple_supply,two_hours,investment_costs,shadow_prices") m.build() return m - @pytest.fixture + @pytest.fixture() def simple_supply_yaml(self): m = build_model({}, "simple_supply,two_hours,investment_costs,shadow_prices") m.build() return m - @pytest.fixture + @pytest.fixture() def simple_supply_yaml_invalid(self): m = build_model( {}, @@ -2145,7 +2144,7 @@ def simple_supply_yaml_invalid(self): m.build() return m - @pytest.fixture + @pytest.fixture() def supply_milp_yaml(self): m = build_model({}, "supply_milp,two_hours,investment_costs,shadow_prices") m.build() @@ -2234,7 +2233,7 @@ def test_yaml_with_invalid_constraint(self, simple_supply_yaml_invalid): class TestValidateMathDict: LOGGER = "calliope.backend.backend_model" - @pytest.fixture + @pytest.fixture() def validate_math(self): def _validate_math(math_dict: dict): m = build_model({}, "simple_supply,investment_costs") diff --git a/tests/test_backend_pyomo_objective.py b/tests/test_backend_pyomo_objective.py index 2c9ffe58..2427daa1 100644 --- a/tests/test_backend_pyomo_objective.py +++ b/tests/test_backend_pyomo_objective.py @@ -1,8 +1,7 @@ +import calliope import pyomo.core as po import pytest -import calliope - from .common.util import build_test_model as build_model approx = pytest.approx diff --git a/tests/test_backend_where_parser.py b/tests/test_backend_where_parser.py index 69620155..a64caead 100644 --- a/tests/test_backend_where_parser.py +++ b/tests/test_backend_where_parser.py @@ -2,7 +2,6 @@ import pyparsing import pytest import xarray as xr - from calliope.attrdict import AttrDict from calliope.backend import expression_parser, helper_functions, where_parser from calliope.exceptions import BackendError @@ -18,50 +17,50 @@ def parse_yaml(yaml_string): return AttrDict.from_yaml_string(yaml_string) -@pytest.fixture +@pytest.fixture() def base_parser_elements(): number, identifier = expression_parser.setup_base_parser_elements() return number, identifier -@pytest.fixture +@pytest.fixture() def number(base_parser_elements): return base_parser_elements[0] -@pytest.fixture +@pytest.fixture() def identifier(base_parser_elements): return base_parser_elements[1] -@pytest.fixture +@pytest.fixture() def data_var(identifier): return where_parser.data_var_parser(identifier) -@pytest.fixture +@pytest.fixture() def config_option(identifier): return where_parser.config_option_parser(identifier) -@pytest.fixture +@pytest.fixture() def bool_operand(): return where_parser.bool_parser() -@pytest.fixture +@pytest.fixture() def evaluatable_string(identifier): return where_parser.evaluatable_string_parser(identifier) -@pytest.fixture +@pytest.fixture() def helper_function(number, identifier, evaluatable_string): return expression_parser.helper_function_parser( evaluatable_string, number, generic_identifier=identifier ) -@pytest.fixture +@pytest.fixture() def comparison( evaluatable_string, number, helper_function, bool_operand, config_option, data_var ): @@ -75,19 +74,19 @@ def comparison( ) -@pytest.fixture +@pytest.fixture() def subset(identifier, evaluatable_string, number): return where_parser.subset_parser(identifier, evaluatable_string, number) -@pytest.fixture +@pytest.fixture() def where(bool_operand, helper_function, data_var, comparison, subset): return where_parser.where_parser( bool_operand, helper_function, data_var, comparison, subset ) -@pytest.fixture +@pytest.fixture() def eval_kwargs(dummy_pyomo_backend_model): return { "input_data": dummy_pyomo_backend_model.inputs, @@ -99,7 +98,7 @@ def eval_kwargs(dummy_pyomo_backend_model): } -@pytest.fixture +@pytest.fixture() def parse_where_string(eval_kwargs, where): def _parse_where_string(where_string): parsed_ = where.parse_string(where_string, parse_all=True) @@ -564,7 +563,7 @@ def test_where_malformed(self, where, instring): class TestAsMathString: - @pytest.fixture + @pytest.fixture() def latex_eval_kwargs(self, eval_kwargs, dummy_latex_backend_model): eval_kwargs["return_type"] = "math_string" eval_kwargs["backend_interface"] = dummy_latex_backend_model diff --git a/tests/test_cli.py b/tests/test_cli.py index 7a3e3374..67cc97ee 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,12 +2,11 @@ import tempfile from pathlib import Path +import calliope import importlib_resources import pytest # noqa: F401 -from click.testing import CliRunner - -import calliope from calliope import AttrDict, cli +from click.testing import CliRunner _MODEL_NATIONAL = ( importlib_resources.files("calliope") diff --git a/tests/test_constraint_results.py b/tests/test_constraint_results.py index 06f3e633..4b7afd19 100644 --- a/tests/test_constraint_results.py +++ b/tests/test_constraint_results.py @@ -1,6 +1,5 @@ -import pytest - import calliope +import pytest from .common.util import build_test_model as build_model @@ -104,7 +103,7 @@ def _get_flow(model, flow): @pytest.mark.skip(reason="to be reimplemented by comparison to LP files") class TestModelSettings: - @pytest.fixture + @pytest.fixture() def run_model(self): def _run_model(feasibility, cap_val): override_dict = { @@ -132,15 +131,15 @@ def _run_model(feasibility, cap_val): return _run_model - @pytest.fixture + @pytest.fixture() def model_no_unmet(self, run_model): return run_model(True, 10) - @pytest.fixture + @pytest.fixture() def model_unmet_demand(self, run_model): return run_model(True, 5) - @pytest.fixture + @pytest.fixture() def model_unused_supply(self, run_model): return run_model(True, 15) @@ -192,7 +191,7 @@ def test_expected_infeasible_result(self, override, run_model): @pytest.mark.skip(reason="to be reimplemented by comparison to LP files") class TestEnergyCapacityPerStorageCapacity: - @pytest.fixture + @pytest.fixture() def model_file(self): return "flow_cap_per_storage_cap.yaml" diff --git a/tests/test_core_attrdict.py b/tests/test_core_attrdict.py index c65ab18e..c4723197 100644 --- a/tests/test_core_attrdict.py +++ b/tests/test_core_attrdict.py @@ -5,14 +5,13 @@ import numpy as np import pytest import ruamel.yaml as ruamel_yaml - from calliope.attrdict import _MISSING, AttrDict from .common.util import check_error_or_warning class TestAttrDict: - @pytest.fixture + @pytest.fixture() def regular_dict(self): d = { "a": 1, @@ -38,16 +37,16 @@ def regular_dict(self): d: """ - @pytest.fixture + @pytest.fixture() def yaml_filepath(self): this_path = Path(__file__).parent return this_path / "common" / "yaml_file.yaml" - @pytest.fixture + @pytest.fixture() def yaml_string(self): return self.setup_string - @pytest.fixture + @pytest.fixture() def attr_dict(self, regular_dict): d = regular_dict return AttrDict(d) diff --git a/tests/test_core_model.py b/tests/test_core_model.py index e16ebfa4..4d8f91fb 100644 --- a/tests/test_core_model.py +++ b/tests/test_core_model.py @@ -1,12 +1,11 @@ import logging from contextlib import contextmanager -import pandas as pd -import pytest - import calliope import calliope.backend import calliope.preprocess +import pandas as pd +import pytest from .common.util import build_test_model as build_model from .common.util import check_error_or_warning diff --git a/tests/test_core_preprocess.py b/tests/test_core_preprocess.py index b449b73f..086b9323 100644 --- a/tests/test_core_preprocess.py +++ b/tests/test_core_preprocess.py @@ -1,10 +1,9 @@ import warnings -import pandas as pd -import pytest - import calliope import calliope.exceptions as exceptions +import pandas as pd +import pytest from calliope.attrdict import AttrDict from .common.util import build_test_model as build_model diff --git a/tests/test_core_util.py b/tests/test_core_util.py index 8e9175ba..09b66090 100644 --- a/tests/test_core_util.py +++ b/tests/test_core_util.py @@ -3,13 +3,12 @@ import logging from pathlib import Path +import calliope import importlib_resources import jsonschema import numpy as np import pandas as pd import pytest - -import calliope from calliope.util import schema from calliope.util.generate_runs import generate_runs from calliope.util.logging import log_time @@ -181,7 +180,7 @@ def test_invalid_dict(self, to_validate, expected_path): ], ) - @pytest.fixture + @pytest.fixture() def base_math(self): return calliope.AttrDict.from_yaml( Path(calliope.__file__).parent / "math" / "plan.yaml" @@ -322,7 +321,7 @@ def sample_model_def_schema(self): """ return calliope.AttrDict.from_yaml_string(schema_string) - @pytest.fixture + @pytest.fixture() def expected_config_defaults(self): return pd.Series( { @@ -332,7 +331,7 @@ def expected_config_defaults(self): } ).sort_index() - @pytest.fixture + @pytest.fixture() def expected_model_def_defaults(self): return pd.Series( { diff --git a/tests/test_example_models.py b/tests/test_example_models.py index 6f19464e..9708e12e 100755 --- a/tests/test_example_models.py +++ b/tests/test_example_models.py @@ -1,11 +1,10 @@ import shutil from pathlib import Path +import calliope import numpy as np import pandas as pd import pytest - -import calliope from calliope import exceptions from .common.util import check_error_or_warning @@ -17,7 +16,7 @@ class TestModelPreproccessing: def test_preprocess_national_scale(self): calliope.examples.national_scale() - @pytest.mark.time_intensive + @pytest.mark.time_intensive() def test_preprocess_time_clustering(self): calliope.examples.time_clustering() @@ -113,7 +112,7 @@ def _example_tester(solver="cbc", solver_io=None): def test_nationalscale_example_results_cbc(self, example_tester): example_tester() - @pytest.mark.needs_gurobi_license + @pytest.mark.needs_gurobi_license() def test_nationalscale_example_results_gurobi(self, example_tester): pytest.importorskip("gurobipy") example_tester(solver="gurobi", solver_io="python") @@ -215,7 +214,7 @@ def example_tester(self, solver="cbc", solver_io=None): def test_nationalscale_example_results_cbc(self): self.example_tester() - @pytest.mark.needs_gurobi_license + @pytest.mark.needs_gurobi_license() @pytest.mark.filterwarnings( "ignore:(?s).*`gurobi_persistent`.*:calliope.exceptions.ModelWarning" ) @@ -231,7 +230,7 @@ def test_nationalscale_example_results_gurobi(self): assert np.allclose(gurobi_data.flow_cap, gurobi_persistent_data.flow_cap) assert np.allclose(gurobi_data.cost, gurobi_persistent_data.cost) - @pytest.fixture + @pytest.fixture() def base_model_data(self): model = calliope.examples.national_scale( time_subset=["2005-01-01", "2005-01-03"], scenario="spores" @@ -270,7 +269,7 @@ def test_fail_with_spores_as_input_dim(self, base_model_data): excinfo, "Cannot run SPORES with a SPORES dimension in any input" ) - @pytest.fixture + @pytest.fixture() def spores_with_override(self): def _spores_with_override(override_dict): result_without_override = self.example_tester() @@ -442,7 +441,7 @@ def example_tester(self, source_unit, solver="cbc", solver_io=None): def test_urban_example_results_area(self): self.example_tester("per_area") - @pytest.mark.needs_gurobi_license + @pytest.mark.needs_gurobi_license() def test_urban_example_results_area_gurobi(self): pytest.importorskip("gurobipy") self.example_tester("per_area", solver="gurobi", solver_io="python") @@ -450,7 +449,7 @@ def test_urban_example_results_area_gurobi(self): def test_urban_example_results_cap(self): self.example_tester("per_cap") - @pytest.mark.needs_gurobi_license + @pytest.mark.needs_gurobi_license() def test_urban_example_results_cap_gurobi(self): pytest.importorskip("gurobipy") self.example_tester("per_cap", solver="gurobi", solver_io="python") diff --git a/tests/test_io.py b/tests/test_io.py index b496db6b..2d5b95b0 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1,11 +1,10 @@ import os import tempfile -import pytest # noqa: F401 -import xarray as xr - import calliope import calliope.io +import pytest # noqa: F401 +import xarray as xr from calliope import exceptions from .common.util import check_error_or_warning diff --git a/tests/test_math.py b/tests/test_math.py index c25aed51..11aa7ef6 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -4,9 +4,8 @@ import numpy as np import pytest -from pyomo.repn.tests import lp_diff - from calliope import AttrDict +from pyomo.repn.tests import lp_diff from .common.util import build_lp, build_test_model @@ -234,7 +233,7 @@ def abs_filepath(self): def custom_math(self): return AttrDict.from_yaml(self.CUSTOM_MATH_DIR / self.YAML_FILEPATH) - @pytest.fixture + @pytest.fixture() def build_and_compare(self, abs_filepath, compare_lps): def _build_and_compare( filename: str, diff --git a/tests/test_preprocess_data_sources.py b/tests/test_preprocess_data_sources.py index 818eb760..af0817e9 100644 --- a/tests/test_preprocess_data_sources.py +++ b/tests/test_preprocess_data_sources.py @@ -1,9 +1,8 @@ import logging +import calliope import pandas as pd import pytest - -import calliope from calliope.preprocess import data_sources from calliope.util.schema import CONFIG_SCHEMA, extract_from_schema diff --git a/tests/test_preprocess_model_data.py b/tests/test_preprocess_model_data.py index 1cb32c0d..dfa84451 100644 --- a/tests/test_preprocess_model_data.py +++ b/tests/test_preprocess_model_data.py @@ -5,7 +5,6 @@ import pandas as pd import pytest import xarray as xr - from calliope import exceptions from calliope.attrdict import AttrDict from calliope.preprocess import data_sources, scenarios @@ -15,7 +14,7 @@ from .common.util import check_error_or_warning -@pytest.fixture +@pytest.fixture() def model_def(): model_def_path = Path(__file__).parent / "common" / "test_model" / "model.yaml" model_dict = AttrDict.from_yaml(model_def_path) @@ -25,7 +24,7 @@ def model_def(): return model_def_override, model_def_path -@pytest.fixture +@pytest.fixture() def data_source_list(model_def, init_config): model_def_dict, model_def_path = model_def return [ @@ -36,14 +35,14 @@ def data_source_list(model_def, init_config): ] -@pytest.fixture +@pytest.fixture() def init_config(config_defaults, model_def): model_def_dict, _ = model_def config_defaults.union(model_def_dict.pop("config"), allow_override=True) return config_defaults["init"] -@pytest.fixture +@pytest.fixture() def model_data_factory(model_def, init_config, model_defaults): model_def_dict, _ = model_def return ModelDataFactory( @@ -51,13 +50,13 @@ def model_data_factory(model_def, init_config, model_defaults): ) -@pytest.fixture +@pytest.fixture() def model_data_factory_w_params(model_data_factory: ModelDataFactory): model_data_factory.add_node_tech_data() return model_data_factory -@pytest.fixture +@pytest.fixture() def my_caplog(caplog): caplog.set_level(logging.DEBUG, logger="calliope.preprocess") return caplog @@ -856,7 +855,7 @@ def test_raise_error_on_transmission_tech_in_node( class TestTopLevelParams: - @pytest.fixture + @pytest.fixture() def run_and_test(self, model_data_factory_w_params): def _run_and_test(in_dict, out_dict, dims): model_data_factory_w_params.model_definition["parameters"] = { diff --git a/tests/test_preprocess_time.py b/tests/test_preprocess_time.py index 0402c623..2ee93c6f 100644 --- a/tests/test_preprocess_time.py +++ b/tests/test_preprocess_time.py @@ -1,6 +1,5 @@ import pandas as pd import pytest # noqa: F401 - from calliope import AttrDict, exceptions from .common.util import build_test_model