From 2d819d0eeb61522be2044bd25445843fb81dbcf7 Mon Sep 17 00:00:00 2001 From: John Vouvakis Manousakis Date: Sun, 19 May 2024 05:17:06 -0700 Subject: [PATCH] Type-hint all `__init__`s --- pelicun/assessment.py | 2 +- pelicun/auto.py | 1 + pelicun/base.py | 19 ++++- pelicun/db.py | 2 +- pelicun/file_io.py | 1 + pelicun/model/__init__.py | 1 + pelicun/model/asset_model.py | 6 +- pelicun/model/damage_model.py | 6 +- pelicun/model/demand_model.py | 6 +- pelicun/model/loss_model.py | 4 +- pelicun/model/pelicun_model.py | 2 +- pelicun/resources/auto/Hazus_Earthquake_IM.py | 1 + .../resources/auto/Hazus_Earthquake_Story.py | 1 + pelicun/tests/code_repetition_checker.py | 1 + .../PRJ-3411v5/pelicun_Example01.py | 1 + pelicun/tests/model/test_asset_model.py | 1 + pelicun/tests/model/test_damage_model.py | 1 + pelicun/tests/model/test_demand_model.py | 1 + pelicun/tests/model/test_loss_model.py | 1 + pelicun/tests/model/test_model.py | 1 + pelicun/tests/model/test_pelicun_model.py | 1 + pelicun/tests/reset_tests.py | 1 + pelicun/tests/test_assessment.py | 1 + pelicun/tests/test_auto.py | 1 + pelicun/tests/test_base.py | 1 + pelicun/tests/test_file_io.py | 1 + pelicun/tests/test_uq.py | 1 + pelicun/tests/util.py | 1 + .../tests/validation/0/test_loss_function.py | 1 + pelicun/tests/validation/1/test_1.py | 1 + pelicun/tools/DL_calculation.py | 1 + pelicun/tools/HDF_to_CSV.py | 1 + pelicun/tools/export_DB.py | 1 + pelicun/uq.py | 80 ++++++++++--------- 34 files changed, 99 insertions(+), 53 deletions(-) diff --git a/pelicun/assessment.py b/pelicun/assessment.py index 2bddf00d8..6e861378c 100644 --- a/pelicun/assessment.py +++ b/pelicun/assessment.py @@ -89,7 +89,7 @@ class Assessment: 'loss', ] - def __init__(self, config_options: dict[str, Any] | None = None) -> None: + def __init__(self, config_options: dict[str, Any] | None = None): """ Initializes an Assessment object. diff --git a/pelicun/auto.py b/pelicun/auto.py index 4fe4622f6..08597b29b 100644 --- a/pelicun/auto.py +++ b/pelicun/auto.py @@ -48,6 +48,7 @@ """ +from __future__ import annotations import sys import importlib from pathlib import Path diff --git a/pelicun/base.py b/pelicun/base.py index ec09b0136..b847df614 100644 --- a/pelicun/base.py +++ b/pelicun/base.py @@ -67,6 +67,8 @@ """ from __future__ import annotations +from typing import Any +from typing import TYPE_CHECKING from collections.abc import Callable import os import sys @@ -84,6 +86,9 @@ from colorama import Style from pelicun.warnings import PelicunWarning +if TYPE_CHECKING: + from pelicun.assessment import Assessment + colorama.init() # set printing options @@ -164,7 +169,11 @@ class Options: """ - def __init__(self, user_config_options, assessment=None): + def __init__( + self, + user_config_options: dict[str, Any] | None, + assessment: Assessment | None = None, + ): """ Initializes an Options object. @@ -275,7 +284,9 @@ class Logger: """ - def __init__(self, verbose, log_show_ms, log_file, print_log): + def __init__( + self, verbose: bool, log_show_ms: bool, log_file: str | None, print_log: bool + ): """ Initializes a Logger object. @@ -312,8 +323,8 @@ def __init__(self, verbose, log_show_ms, log_file, print_log): raise self.print_log = str2bool(print_log) - self.warning_stack = [] - self.emitted = set() + self.warning_stack: list[str] = [] + self.emitted: set[str] = set() self.reset_log_strings() control_warnings() diff --git a/pelicun/db.py b/pelicun/db.py index d452c2ffe..5d42771cd 100644 --- a/pelicun/db.py +++ b/pelicun/db.py @@ -2107,7 +2107,7 @@ def create_Hazus_HU_fragility_db( 'damage_DB_SimCenter_Hazus_HU_bldg_template.json' ), target_meta_file: str = 'damage_DB_SimCenter_Hazus_HU_bldg.json', -) -> None: +): """ Create a database metadata file for the HAZUS Hurricane fragilities. diff --git a/pelicun/file_io.py b/pelicun/file_io.py index 932417b0a..2bca3c764 100644 --- a/pelicun/file_io.py +++ b/pelicun/file_io.py @@ -54,6 +54,7 @@ """ +from __future__ import annotations from pathlib import Path import numpy as np import pandas as pd diff --git a/pelicun/model/__init__.py b/pelicun/model/__init__.py index 05205ee04..f4734f486 100644 --- a/pelicun/model/__init__.py +++ b/pelicun/model/__init__.py @@ -41,6 +41,7 @@ # flake8: noqa +from __future__ import annotations from pelicun.model.pelicun_model import PelicunModel from pelicun.model.demand_model import DemandModel from pelicun.model.asset_model import AssetModel diff --git a/pelicun/model/asset_model.py b/pelicun/model/asset_model.py index 1f3b26cb3..122596eb6 100644 --- a/pelicun/model/asset_model.py +++ b/pelicun/model/asset_model.py @@ -49,6 +49,8 @@ """ +from __future__ import annotations +from typing import TYPE_CHECKING from itertools import product import numpy as np import pandas as pd @@ -57,6 +59,8 @@ from pelicun import uq from pelicun import file_io +if TYPE_CHECKING: + from pelicun.assessment import Assessment idx = base.idx @@ -72,7 +76,7 @@ class AssetModel(PelicunModel): __slots__ = ['cmp_marginal_params', 'cmp_units', 'cmp_sample', '_cmp_RVs'] - def __init__(self, assessment): + def __init__(self, assessment: Assessment): super().__init__(assessment) self.cmp_marginal_params = None diff --git a/pelicun/model/damage_model.py b/pelicun/model/damage_model.py index 349f25d18..dc350aedc 100644 --- a/pelicun/model/damage_model.py +++ b/pelicun/model/damage_model.py @@ -75,7 +75,7 @@ class DamageModel(PelicunModel): __slots__ = ['ds_model', 'missing_components'] - def __init__(self, assessment: Assessment) -> None: + def __init__(self, assessment: Assessment): super().__init__(assessment) self.ds_model: DamageModel_DS = DamageModel_DS(assessment) @@ -417,7 +417,7 @@ class DamageModel_Base(PelicunModel): __slots__ = ['damage_params', 'sample'] - def __init__(self, assessment): + def __init__(self, assessment: Assessment): super().__init__(assessment) self.damage_params = None @@ -629,7 +629,7 @@ class DamageModel_DS(DamageModel_Base): __slots__ = ['ds_sample'] - def __init__(self, assessment): + def __init__(self, assessment: Assessment): super().__init__(assessment) self.ds_sample = None diff --git a/pelicun/model/demand_model.py b/pelicun/model/demand_model.py index 1dd642e2b..5d3b745b3 100644 --- a/pelicun/model/demand_model.py +++ b/pelicun/model/demand_model.py @@ -49,6 +49,8 @@ """ +from __future__ import annotations +from typing import TYPE_CHECKING import numpy as np import pandas as pd from pelicun.model.pelicun_model import PelicunModel @@ -56,6 +58,8 @@ from pelicun import uq from pelicun import file_io +if TYPE_CHECKING: + from pelicun.assessment import Assessment idx = base.idx @@ -103,7 +107,7 @@ class DemandModel(PelicunModel): 'sample', ] - def __init__(self, assessment): + def __init__(self, assessment: Assessment): super().__init__(assessment) self.marginal_params = None diff --git a/pelicun/model/loss_model.py b/pelicun/model/loss_model.py index 0637249ad..4a7c412dd 100644 --- a/pelicun/model/loss_model.py +++ b/pelicun/model/loss_model.py @@ -83,7 +83,7 @@ def __init__( self, assessment: Assessment, decision_variables: list[str] = ['Carbon', 'Cost', 'Energy', 'Time'], - ) -> None: + ): """ Initializes LossModel objects. @@ -763,7 +763,7 @@ class RepairModel_Base(PelicunModel): __slots__ = ['loss_params', 'sample', 'consequence'] - def __init__(self, assessment): + def __init__(self, assessment: Assessment): """ Initializes RepairModel_Base objects. diff --git a/pelicun/model/pelicun_model.py b/pelicun/model/pelicun_model.py index a24bf309d..4acab8835 100644 --- a/pelicun/model/pelicun_model.py +++ b/pelicun/model/pelicun_model.py @@ -70,7 +70,7 @@ class PelicunModel: __slots__ = ['_asmnt', 'log'] - def __init__(self, assessment: Assessment) -> None: + def __init__(self, assessment: Assessment): # link the PelicunModel object to its Assessment object self._asmnt: Assessment = assessment diff --git a/pelicun/resources/auto/Hazus_Earthquake_IM.py b/pelicun/resources/auto/Hazus_Earthquake_IM.py index 18b4ebb8e..a85729458 100644 --- a/pelicun/resources/auto/Hazus_Earthquake_IM.py +++ b/pelicun/resources/auto/Hazus_Earthquake_IM.py @@ -36,6 +36,7 @@ # # Contributors: # Adam Zsarnóczay +from __future__ import annotations import json import pandas as pd import pelicun diff --git a/pelicun/resources/auto/Hazus_Earthquake_Story.py b/pelicun/resources/auto/Hazus_Earthquake_Story.py index 47db26175..4b29a3feb 100644 --- a/pelicun/resources/auto/Hazus_Earthquake_Story.py +++ b/pelicun/resources/auto/Hazus_Earthquake_Story.py @@ -37,6 +37,7 @@ # Contributors: # Adam Zsarnóczay +from __future__ import annotations import pandas as pd ap_DesignLevel = {1940: 'LC', 1975: 'MC', 2100: 'HC'} diff --git a/pelicun/tests/code_repetition_checker.py b/pelicun/tests/code_repetition_checker.py index 63b24a839..3bf66f4f6 100644 --- a/pelicun/tests/code_repetition_checker.py +++ b/pelicun/tests/code_repetition_checker.py @@ -43,6 +43,7 @@ Python test files. """ +from __future__ import annotations from glob2 import glob # type: ignore # pylint: disable=missing-any-param-doc diff --git a/pelicun/tests/compatibility/PRJ-3411v5/pelicun_Example01.py b/pelicun/tests/compatibility/PRJ-3411v5/pelicun_Example01.py index 3d427adf1..e255eccb1 100644 --- a/pelicun/tests/compatibility/PRJ-3411v5/pelicun_Example01.py +++ b/pelicun/tests/compatibility/PRJ-3411v5/pelicun_Example01.py @@ -9,6 +9,7 @@ """ +from __future__ import annotations import numpy as np import pandas as pd from pelicun.base import convert_to_MultiIndex diff --git a/pelicun/tests/model/test_asset_model.py b/pelicun/tests/model/test_asset_model.py index ca9ead018..584213c14 100644 --- a/pelicun/tests/model/test_asset_model.py +++ b/pelicun/tests/model/test_asset_model.py @@ -42,6 +42,7 @@ These are unit and integration tests on the asset model of pelicun. """ +from __future__ import annotations import tempfile from copy import deepcopy import pytest diff --git a/pelicun/tests/model/test_damage_model.py b/pelicun/tests/model/test_damage_model.py index c9da0aad6..8718109cc 100644 --- a/pelicun/tests/model/test_damage_model.py +++ b/pelicun/tests/model/test_damage_model.py @@ -42,6 +42,7 @@ These are unit and integration tests on the damage model of pelicun. """ +from __future__ import annotations from copy import deepcopy import warnings import pytest diff --git a/pelicun/tests/model/test_demand_model.py b/pelicun/tests/model/test_demand_model.py index 25c4bbef0..fd0837242 100644 --- a/pelicun/tests/model/test_demand_model.py +++ b/pelicun/tests/model/test_demand_model.py @@ -42,6 +42,7 @@ These are unit and integration tests on the demand model of pelicun. """ +from __future__ import annotations import os import tempfile import warnings diff --git a/pelicun/tests/model/test_loss_model.py b/pelicun/tests/model/test_loss_model.py index c1b99598d..352bdcc75 100644 --- a/pelicun/tests/model/test_loss_model.py +++ b/pelicun/tests/model/test_loss_model.py @@ -42,6 +42,7 @@ These are unit and integration tests on the loss model of pelicun. """ +from __future__ import annotations from itertools import product from copy import deepcopy import pytest diff --git a/pelicun/tests/model/test_model.py b/pelicun/tests/model/test_model.py index 94eb7611f..8cbc8d866 100644 --- a/pelicun/tests/model/test_model.py +++ b/pelicun/tests/model/test_model.py @@ -42,6 +42,7 @@ This file defines a clas used by the model unit tests. """ +from __future__ import annotations from copy import deepcopy import pytest from pelicun import assessment diff --git a/pelicun/tests/model/test_pelicun_model.py b/pelicun/tests/model/test_pelicun_model.py index 7b0cb09e5..c2c84cfd0 100644 --- a/pelicun/tests/model/test_pelicun_model.py +++ b/pelicun/tests/model/test_pelicun_model.py @@ -42,6 +42,7 @@ These are unit and integration tests on the PelicunModel class. """ +from __future__ import annotations from copy import deepcopy import pytest import numpy as np diff --git a/pelicun/tests/reset_tests.py b/pelicun/tests/reset_tests.py index 283ea8ba9..6cf8b18b4 100644 --- a/pelicun/tests/reset_tests.py +++ b/pelicun/tests/reset_tests.py @@ -42,6 +42,7 @@ This file is used to reset all expected test result data. """ +from __future__ import annotations import os import re import glob diff --git a/pelicun/tests/test_assessment.py b/pelicun/tests/test_assessment.py index e0be7d787..7b9ebe028 100644 --- a/pelicun/tests/test_assessment.py +++ b/pelicun/tests/test_assessment.py @@ -42,6 +42,7 @@ These are unit and integration tests on the assessment module of pelicun. """ +from __future__ import annotations import pytest from pelicun import assessment diff --git a/pelicun/tests/test_auto.py b/pelicun/tests/test_auto.py index 33bdbc3c6..3a96025d0 100644 --- a/pelicun/tests/test_auto.py +++ b/pelicun/tests/test_auto.py @@ -43,6 +43,7 @@ """ +from __future__ import annotations from unittest.mock import patch from unittest.mock import MagicMock import pytest diff --git a/pelicun/tests/test_base.py b/pelicun/tests/test_base.py index ff517c863..c873d8fd1 100644 --- a/pelicun/tests/test_base.py +++ b/pelicun/tests/test_base.py @@ -42,6 +42,7 @@ These are unit and integration tests on the base module of pelicun. """ +from __future__ import annotations import os import io import re diff --git a/pelicun/tests/test_file_io.py b/pelicun/tests/test_file_io.py index eeaa03360..aa0450259 100644 --- a/pelicun/tests/test_file_io.py +++ b/pelicun/tests/test_file_io.py @@ -42,6 +42,7 @@ These are unit and integration tests on the file_io module of pelicun. """ +from __future__ import annotations import tempfile import os import pytest diff --git a/pelicun/tests/test_uq.py b/pelicun/tests/test_uq.py index 34272d3c4..16b9a93b2 100644 --- a/pelicun/tests/test_uq.py +++ b/pelicun/tests/test_uq.py @@ -46,6 +46,7 @@ reset from the `reset_all_test_data` function in `reset_tests.py`. """ +from __future__ import annotations import warnings import pytest import numpy as np diff --git a/pelicun/tests/util.py b/pelicun/tests/util.py index 9a06a3e7d..68a7c7790 100644 --- a/pelicun/tests/util.py +++ b/pelicun/tests/util.py @@ -42,6 +42,7 @@ These are utility functions for the unit and integration tests. """ +from __future__ import annotations import pickle import os diff --git a/pelicun/tests/validation/0/test_loss_function.py b/pelicun/tests/validation/0/test_loss_function.py index 123eff3e0..d91631f0a 100644 --- a/pelicun/tests/validation/0/test_loss_function.py +++ b/pelicun/tests/validation/0/test_loss_function.py @@ -47,6 +47,7 @@ """ +from __future__ import annotations import numpy as np import pandas as pd import pelicun diff --git a/pelicun/tests/validation/1/test_1.py b/pelicun/tests/validation/1/test_1.py index 65e4f093b..d9e840792 100644 --- a/pelicun/tests/validation/1/test_1.py +++ b/pelicun/tests/validation/1/test_1.py @@ -44,6 +44,7 @@ """ +from __future__ import annotations import tempfile import numpy as np import pandas as pd diff --git a/pelicun/tools/DL_calculation.py b/pelicun/tools/DL_calculation.py index 7b380d3ef..2bc7f1083 100644 --- a/pelicun/tools/DL_calculation.py +++ b/pelicun/tools/DL_calculation.py @@ -37,6 +37,7 @@ # Contributors: # Adam Zsarnóczay +from __future__ import annotations from time import gmtime from time import strftime import sys diff --git a/pelicun/tools/HDF_to_CSV.py b/pelicun/tools/HDF_to_CSV.py index f44b21ef5..21f8d1941 100644 --- a/pelicun/tools/HDF_to_CSV.py +++ b/pelicun/tools/HDF_to_CSV.py @@ -37,6 +37,7 @@ # Contributors: # Adam Zsarnóczay +from __future__ import annotations import pandas as pd import sys import argparse diff --git a/pelicun/tools/export_DB.py b/pelicun/tools/export_DB.py index 327dcac2b..22c79737b 100644 --- a/pelicun/tools/export_DB.py +++ b/pelicun/tools/export_DB.py @@ -37,6 +37,7 @@ # Contributors: # Adam Zsarnóczay +from __future__ import annotations import sys import json import argparse diff --git a/pelicun/uq.py b/pelicun/uq.py index a047d1cd9..26ca2eeb7 100644 --- a/pelicun/uq.py +++ b/pelicun/uq.py @@ -58,6 +58,8 @@ """ +from __future__ import annotations +from collections.abc import Callable from abc import ABC, abstractmethod from scipy.stats import uniform, norm # type: ignore from scipy.stats import multivariate_normal as mvn # type: ignore @@ -1109,9 +1111,9 @@ class BaseRandomVariable(ABC): def __init__( self, - name, - f_map=None, - anchor=None, + name: str, + f_map: Callable | None = None, + anchor: BaseRandomVariable | None = None, ): """ Initializes a RandomVariable object. @@ -1138,12 +1140,12 @@ def __init__( """ self.name = name - self.distribution = None + self.distribution: str | None = None self.f_map = f_map - self._uni_samples = None - self.RV_set = None + self._uni_samples: np.ndarray | None = None + self.RV_set: RandomVariableSet | None = None self._sample_DF = None - self._sample = None + self._sample: np.ndarray | None = None if anchor is None: self.anchor = self else: @@ -1231,11 +1233,11 @@ class RandomVariable(BaseRandomVariable): @abstractmethod def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), - f_map=None, - anchor=None, + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), + f_map: Callable | None = None, + anchor: BaseRandomVariable | None = None, ): """ Instantiates a normal random variable. @@ -1301,9 +1303,9 @@ class UtilityRandomVariable(BaseRandomVariable): @abstractmethod def __init__( self, - name, - f_map=None, - anchor=None, + name: str, + f_map: Callable | None = None, + anchor: BaseRandomVariable | None = None, ): """ Instantiates a normal random variable. @@ -1354,9 +1356,9 @@ class NormalRandomVariable(RandomVariable): def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -1478,8 +1480,8 @@ class LogNormalRandomVariable(RandomVariable): def __init__( self, - name, - theta, + name: str, + theta: np.ndarray, truncation_limits=np.array((np.nan, np.nan)), f_map=None, anchor=None, @@ -1596,9 +1598,9 @@ class UniformRandomVariable(RandomVariable): def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -1687,9 +1689,9 @@ class MultilinearCDFRandomVariable(RandomVariable): def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -1812,9 +1814,9 @@ class EmpiricalRandomVariable(RandomVariable): def __init__( self, - name, - raw_samples, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + raw_samples: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -1869,9 +1871,9 @@ class CoupledEmpiricalRandomVariable(UtilityRandomVariable): def __init__( self, - name, - raw_samples, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + raw_samples: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -1955,9 +1957,9 @@ class DeterministicRandomVariable(UtilityRandomVariable): def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -2033,9 +2035,9 @@ class MultinomialRandomVariable(RandomVariable): def __init__( self, - name, - theta, - truncation_limits=np.array((np.nan, np.nan)), + name: str, + theta: np.ndarray, + truncation_limits: np.ndarray = np.array((np.nan, np.nan)), f_map=None, anchor=None, ): @@ -2114,7 +2116,7 @@ class RandomVariableSet: __slots__ = ['name', '_variables', '_Rho'] - def __init__(self, name, RV_list, Rho): + def __init__(self, name: str, RV_list: list[RandomVariable], Rho: np.ndarray): self.name = name if len(RV_list) > 1: