From add6842514daa33a9139598d81887889b38dab56 Mon Sep 17 00:00:00 2001 From: Jostein Solaas Date: Tue, 10 Dec 2024 09:29:21 +0100 Subject: [PATCH] chore: mypy fixes --- src/libecalc/common/time_utils.py | 10 ++++---- src/libecalc/common/utils/rates.py | 2 +- src/libecalc/common/variables.py | 2 +- .../train/utils/enthalpy_calculations.py | 2 +- .../core/models/model_input_validation.py | 3 +++ .../core/models/results/compressor.py | 6 ++--- .../legacy/energy_usage_model/common.py | 4 +++- ...yaml_energy_usage_model_consumer_system.py | 14 +++++------ .../yaml/yaml_types/components/yaml_asset.py | 8 ++++--- .../components/yaml_installation.py | 24 ++++++++++--------- 10 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/libecalc/common/time_utils.py b/src/libecalc/common/time_utils.py index 53d7d560b2..c38d72f275 100644 --- a/src/libecalc/common/time_utils.py +++ b/src/libecalc/common/time_utils.py @@ -171,23 +171,23 @@ def get_period(self, period: Period) -> Optional[Period]: return None @property - def all_dates(self): + def all_dates(self) -> list[datetime]: return self.start_dates + [self.end_dates[-1]] @property - def start_dates(self): + def start_dates(self) -> list[datetime]: return [period.start for period in self.periods] @property - def end_dates(self): + def end_dates(self) -> list[datetime]: return [period.end for period in self.periods] @property - def last_date(self): + def last_date(self) -> datetime: return self.end_dates[-1] @property - def first_date(self): + def first_date(self) -> datetime: return self.start_dates[0] def __add__(self, other): diff --git a/src/libecalc/common/utils/rates.py b/src/libecalc/common/utils/rates.py index 031fd3283b..44f9d70d19 100644 --- a/src/libecalc/common/utils/rates.py +++ b/src/libecalc/common/utils/rates.py @@ -547,7 +547,7 @@ def resample( ds_interpolated = ds.reindex(ds.index.union(new_dates)).interpolate("slinear") # New resampled pd.Series - resampled = ds_interpolated.reindex(new_dates).values.tolist() + resampled: list[float] = ds_interpolated.reindex(new_dates).values.tolist() if not include_start_date: dropped_cumulative_volume = resampled[0] diff --git a/src/libecalc/common/variables.py b/src/libecalc/common/variables.py index cb0ac70507..ffd491d9b8 100644 --- a/src/libecalc/common/variables.py +++ b/src/libecalc/common/variables.py @@ -158,7 +158,7 @@ def _evaluate_temporal( class ExpressionEvaluator(Protocol): @abc.abstractmethod - def get_time_vector(self) -> [list[datetime]]: ... + def get_time_vector(self) -> list[datetime]: ... @abc.abstractmethod def get_period(self) -> Period: ... diff --git a/src/libecalc/core/models/compressor/train/utils/enthalpy_calculations.py b/src/libecalc/core/models/compressor/train/utils/enthalpy_calculations.py index 1792514a2a..0115a9a8e6 100644 --- a/src/libecalc/core/models/compressor/train/utils/enthalpy_calculations.py +++ b/src/libecalc/core/models/compressor/train/utils/enthalpy_calculations.py @@ -47,7 +47,7 @@ def calculate_enthalpy_change_head_iteration( """ - pressure_ratios = outlet_pressure / inlet_pressure + pressure_ratios = np.divide(outlet_pressure, inlet_pressure) # Using divide seems to keep the float64 type inlet_kappa = np.asarray([stream.kappa for stream in inlet_streams]) inlet_z = np.asarray([stream.z for stream in inlet_streams]) diff --git a/src/libecalc/core/models/model_input_validation.py b/src/libecalc/core/models/model_input_validation.py index fa901d4223..39cd480abe 100644 --- a/src/libecalc/core/models/model_input_validation.py +++ b/src/libecalc/core/models/model_input_validation.py @@ -177,6 +177,9 @@ def _validate_model_input( if np.ndim(rate) == 2: # check if any of the streams have changed value during validation, streams along axis 0, time along axis 1 invalid_rate_input = np.any(rate != input_rate, axis=0) + + # Ensure invalid_rate_input is an array and not a single bool, this should be the case since using axis=0. + assert isinstance(invalid_rate_input, np.ndarray) else: invalid_rate_input = np.where(rate != input_rate, True, False) diff --git a/src/libecalc/core/models/results/compressor.py b/src/libecalc/core/models/results/compressor.py index aed9e7313f..4a154a6a03 100644 --- a/src/libecalc/core/models/results/compressor.py +++ b/src/libecalc/core/models/results/compressor.py @@ -231,7 +231,7 @@ def is_valid(self) -> list[bool]: if self.stage_results is not None else [not isnan(x) for x in self.energy_usage] ) - return list(np.all([failure_status_are_valid, turbine_are_valid, stage_results_are_valid], axis=0)) + return np.all([failure_status_are_valid, turbine_are_valid, stage_results_are_valid], axis=0).tolist() @property def inlet_stream(self) -> CompressorStreamCondition: @@ -248,7 +248,7 @@ def mass_rate_kg_per_hr(self) -> list[float]: @property def pressure_is_choked(self) -> list[bool]: - return list(np.any([stage.pressure_is_choked for stage in self.stage_results], axis=0)) + return np.any([stage.pressure_is_choked for stage in self.stage_results], axis=0).tolist() @property def recirculation_loss(self) -> list[float]: @@ -256,4 +256,4 @@ def recirculation_loss(self) -> list[float]: @property def rate_exceeds_maximum(self) -> list[bool]: - return list(np.any([stage.rate_exceeds_maximum for stage in self.stage_results], axis=0)) + return np.any([stage.rate_exceeds_maximum for stage in self.stage_results], axis=0).tolist() diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/common.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/common.py index 2f21c10d72..4d7a8d6247 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/common.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/common.py @@ -1,3 +1,5 @@ +from typing import Optional + from pydantic import Field, model_validator from libecalc.presentation.yaml.yaml_types import YamlBase @@ -12,7 +14,7 @@ class EnergyUsageModelCommon(YamlBase): title="CONDITION", description="Logical condition for the consumer to be used.\n\n$ECALC_DOCS_KEYWORDS_URL/CONDITION", ) - conditions: list[YamlExpressionType] = Field( + conditions: Optional[list[YamlExpressionType]] = Field( None, title="CONDITIONS", description="Logical conditions for the consumer to be used.\n\n$ECALC_DOCS_KEYWORDS_URL/CONDITION", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py index 6503cfe98a..4e00dd1774 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py @@ -1,4 +1,4 @@ -from typing import Literal +from typing import Literal, Optional from pydantic import Field @@ -29,27 +29,27 @@ class YamlCompressorSystemCompressor(YamlBase): class YamlCompressorSystemOperationalSetting(YamlBase): - crossover: list[int] = Field( + crossover: Optional[list[int]] = Field( None, title="CROSSOVER", description="Set cross over rules in system operational setting. \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#crossover", ) - rates: list[YamlExpressionType] = Field( + rates: Optional[list[YamlExpressionType]] = Field( None, title="RATES", description="Set rate per consumer in a consumer system operational setting. \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#rates", ) - rate_fractions: list[YamlExpressionType] = Field( + rate_fractions: Optional[list[YamlExpressionType]] = Field( None, title="RATE_FRACTIONS", description="List of expressions defining fractional rate (of total system rate) per consumer. \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#rate-fractions", ) - suction_pressures: list[YamlExpressionType] = Field( + suction_pressures: Optional[list[YamlExpressionType]] = Field( None, title="SUCTION_PRESSURES", description="Set suction pressure per consumer in a consumer system operational setting. \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#suction-pressures", ) - discharge_pressures: list[YamlExpressionType] = Field( + discharge_pressures: Optional[list[YamlExpressionType]] = Field( None, title="DISCHARGE_PRESSURES", description="Set discharge pressure per consumer in a consumer system operational setting. \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#discharge-pressures", @@ -67,7 +67,7 @@ class YamlCompressorSystemOperationalSetting(YamlBase): class YamlPumpSystemOperationalSettings(YamlCompressorSystemOperationalSetting): - fluid_densities: list[YamlExpressionType] = Field( + fluid_densities: Optional[list[YamlExpressionType]] = Field( None, title="FLUID_DENSITIES", description="Set fluid density per consumer in a consumer system operational setting. Will overwrite the systems common fluid density expression \n\n$ECALC_DOCS_KEYWORDS_URL/OPERATIONAL_SETTINGS#fluid-densities", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py index 521892602e..be387493e5 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py @@ -1,3 +1,5 @@ +from typing import Optional + from pydantic import ConfigDict, Field, model_validator from pydantic_core.core_schema import ValidationInfo @@ -29,19 +31,19 @@ class YamlAsset(YamlBase): title="Asset", ) - time_series: list[YamlTimeSeriesCollection] = Field( + time_series: Optional[list[YamlTimeSeriesCollection]] = Field( None, title="TIME_SERIES", description="Defines the inputs for time dependent variables, or 'reservoir variables'." "\n\n$ECALC_DOCS_KEYWORDS_URL/TIME_SERIES", ) - facility_inputs: list[YamlFacilityModel] = Field( + facility_inputs: Optional[list[YamlFacilityModel]] = Field( None, title="FACILITY_INPUTS", description="Defines input files which characterize various facility elements." "\n\n$ECALC_DOCS_KEYWORDS_URL/FACILITY_INPUTS", ) - models: list[YamlConsumerModel] = Field( + models: Optional[list[YamlConsumerModel]] = Field( None, title="MODELS", description="Defines input files which characterize various facility elements." diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_installation.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_installation.py index c4355a4bd4..b4cf913987 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_installation.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_installation.py @@ -1,4 +1,4 @@ -from typing import Annotated, Union +from typing import Annotated, Optional, Union from pydantic import ConfigDict, Field, model_validator @@ -53,20 +53,22 @@ class YamlInstallation(YamlBase): title="REGULARITY", description="Regularity of the installation can be specified by a single number or as an expression. USE WITH CARE.\n\n$ECALC_DOCS_KEYWORDS_URL/REGULARITY", ) - generator_sets: list[YamlGeneratorSet] = Field( + generator_sets: Optional[list[YamlGeneratorSet]] = Field( None, title="GENERATORSETS", description="Defines one or more generator sets.\n\n$ECALC_DOCS_KEYWORDS_URL/GENERATORSETS", alias="GENERATORSETS", ) - fuel_consumers: list[ - Annotated[ - Union[ - YamlFuelConsumer, - YamlConsumerSystem, - ], - Field(discriminator="component_type"), - DiscriminatorWithFallback("TYPE", "FUEL_CONSUMER"), + fuel_consumers: Optional[ + list[ + Annotated[ + Union[ + YamlFuelConsumer, + YamlConsumerSystem, + ], + Field(discriminator="component_type"), + DiscriminatorWithFallback("TYPE", "FUEL_CONSUMER"), + ] ] ] = Field( None, @@ -74,7 +76,7 @@ class YamlInstallation(YamlBase): description="Defines fuel consumers on the installation which are not generators.\n\n$ECALC_DOCS_KEYWORDS_URL/FUELCONSUMERS", alias="FUELCONSUMERS", ) - venting_emitters: list[YamlVentingEmitter] = Field( + venting_emitters: Optional[list[YamlVentingEmitter]] = Field( None, title="VENTING_EMITTERS", description="Covers the direct emissions on the installation that are not consuming energy",