Skip to content

Commit

Permalink
Introducing mypy.
Browse files Browse the repository at this point in the history
Running it now produces no errors.
  • Loading branch information
ioannis-vm committed May 19, 2024
1 parent f1ad619 commit 1539f57
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 26 deletions.
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
ignore_missing_imports = True
exclude = "^flycheck\."
5 changes: 3 additions & 2 deletions pelicun/assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@
"""

from __future__ import annotations
from typing import Any
import json
from pelicun import base
from pelicun import file_io
from pelicun import model
from pelicun.__init__ import __version__ as pelicun_version
from pelicun.__init__ import __version__ as pelicun_version # type: ignore


class Assessment:
Expand Down Expand Up @@ -88,7 +89,7 @@ class Assessment:
'loss',
]

def __init__(self, config_options=None):
def __init__(self, config_options: dict[str, Any] | None = None) -> None:
"""
Initializes an Assessment object.
Expand Down
6 changes: 3 additions & 3 deletions pelicun/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
import argparse
import pprint
import numpy as np
from scipy.interpolate import interp1d
from scipy.interpolate import interp1d # type: ignore
import pandas as pd
import colorama
from colorama import Fore
Expand All @@ -90,7 +90,7 @@
pp = pprint.PrettyPrinter(indent=2, width=80 - 24)

pd.options.display.max_rows = 20
pd.options.display.max_columns = None
pd.options.display.max_columns = None # type: ignore
pd.options.display.expand_frame_repr = True
pd.options.display.width = 300

Expand Down Expand Up @@ -1481,7 +1481,7 @@ def convert_units(

def stringterpolation(
arguments: str,
) -> tuple[Callable[np.array, np.array]]:
) -> Callable[[np.ndarray], np.ndarray]:
"""
Turns a string of specially formatted arguments into a multilinear
interpolating funciton.
Expand Down
3 changes: 2 additions & 1 deletion pelicun/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from pathlib import Path
from copy import deepcopy
import numpy as np
from scipy.stats import norm
from scipy.stats import norm # type: ignore
import pandas as pd

from pelicun import base
Expand Down Expand Up @@ -2648,6 +2648,7 @@ def find_class_type(entry: str) -> str | None:

for fragility_id in fragility_data['ID'].to_list():
class_type = find_class_type(fragility_id)
assert class_type is not None

class_type_human_readable = class_types[class_type]

Expand Down
6 changes: 5 additions & 1 deletion pelicun/model/damage_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"""

from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import pandas as pd
from pelicun.model.pelicun_model import PelicunModel
Expand All @@ -60,6 +61,9 @@
from pelicun import uq
from pelicun import file_io

if TYPE_CHECKING:
from pelicun.assessment import Assessment

idx = base.idx


Expand All @@ -71,7 +75,7 @@ class DamageModel(PelicunModel):

__slots__ = ['ds_model', 'missing_components']

def __init__(self, assessment):
def __init__(self, assessment: Assessment) -> None:
super().__init__(assessment)

self.ds_model: DamageModel_DS = DamageModel_DS(assessment)
Expand Down
14 changes: 10 additions & 4 deletions pelicun/model/loss_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"""

from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import pandas as pd
from pelicun.model.pelicun_model import PelicunModel
Expand All @@ -60,6 +61,8 @@
from pelicun import uq
from pelicun import file_io

if TYPE_CHECKING:
from pelicun.assessment import Assessment

idx = base.idx

Expand All @@ -75,17 +78,20 @@ class LossModel(PelicunModel):

__slots__ = ['ds_model', 'lf_model']

# pylint: disable=dangerous-default-value
def __init__(
self, assessment, decision_variables=('Carbon', 'Cost', 'Energy', 'Time')
):
self,
assessment: Assessment,
decision_variables: list[str] = ['Carbon', 'Cost', 'Energy', 'Time'],
) -> None:
"""
Initializes LossModel objects.
Parameters
----------
assessment: pelicun.Assessment
Parent assessment
decision_variables: tuple
decision_variables: list
Defines the decision variables to be included in the loss
calculations. Defaults to those supported, but fewer can be
used if desired. When fewer are used, the loss parameters for
Expand Down Expand Up @@ -1289,7 +1295,7 @@ def _calc_median_consequence(self, eco_qnt):
components and damage states, appropriately grouped to
account for economies of scale.
decision_variables: tuple
decision_variables: list
Defines the decision variables to be included in the loss
calculations. Defaults to those supported, but fewer can be
used if desired. When fewer are used, the loss parameters for
Expand Down
2 changes: 1 addition & 1 deletion pelicun/model/pelicun_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class PelicunModel:

__slots__ = ['_asmnt', 'log']

def __init__(self, assessment):
def __init__(self, assessment: Assessment) -> None:
# link the PelicunModel object to its Assessment object
self._asmnt: Assessment = assessment

Expand Down
2 changes: 1 addition & 1 deletion pelicun/tests/code_repetition_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
Python test files.
"""

from glob2 import glob
from glob2 import glob # type: ignore

# pylint: disable=missing-any-param-doc

Expand Down
4 changes: 2 additions & 2 deletions pelicun/tests/test_uq.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
import warnings
import pytest
import numpy as np
from scipy.stats import norm
from scipy.stats import lognorm
from scipy.stats import norm # type: ignore
from scipy.stats import lognorm # type: ignore
from pelicun import uq
from pelicun.tests.util import import_pickle
from pelicun.tests.util import export_pickle
Expand Down
2 changes: 1 addition & 1 deletion pelicun/tests/validation/1/test_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import pandas as pd
import pelicun
from pelicun import assessment
from scipy.stats import norm
from scipy.stats import norm # type: ignore


def test_validation_ds_probabilities():
Expand Down
4 changes: 2 additions & 2 deletions pelicun/tools/HDF_to_CSV.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ def convert_HDF(HDF_path):
parser = argparse.ArgumentParser()
parser.add_argument('HDF_path')

args = parser.parse_args(args)
parser_args = parser.parse_args(args)

convert_HDF(args.HDF_path)
convert_HDF(parser_args.HDF_path)
2 changes: 1 addition & 1 deletion pelicun/tools/export_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from pathlib import Path
import pandas as pd

from pelicun.db import convert_Series_to_dict
from pelicun.db import convert_Series_to_dict # type: ignore


def export_DB(data_path, target_dir):
Expand Down
14 changes: 7 additions & 7 deletions pelicun/uq.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"""

from abc import ABC, abstractmethod
from scipy.stats import uniform, norm
from scipy.stats import multivariate_normal as mvn
from scipy.stats._mvn import mvndst # pylint: disable=no-name-in-module
from scipy.linalg import cholesky, svd
from scipy.optimize import minimize
from scipy.stats import uniform, norm # type: ignore
from scipy.stats import multivariate_normal as mvn # type: ignore
from scipy.stats._mvn import mvndst # type: ignore # pylint: disable=no-name-in-module # noqa # lol
from scipy.linalg import cholesky, svd # type: ignore
from scipy.optimize import minimize # type: ignore
import numpy as np
import pandas as pd
import colorama
Expand Down Expand Up @@ -1226,7 +1226,7 @@ class RandomVariable(BaseRandomVariable):
Random variable that needs `values` in `inverse_transform`
"""

__slots__ = []
__slots__: list[str] = []

@abstractmethod
def __init__(
Expand Down Expand Up @@ -1296,7 +1296,7 @@ class UtilityRandomVariable(BaseRandomVariable):
Random variable that needs `sample_size` in `inverse_transform`
"""

__slots__ = []
__slots__: list[str] = []

@abstractmethod
def __init__(
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def read(*filenames, **kwargs):
'development': [
'flake8',
'pylint',
'mypy',
'black',
'pytest',
'pytest-cov',
Expand All @@ -63,6 +64,8 @@ def read(*filenames, **kwargs):
'nbsphinx',
'flake8-rst',
'flake8-rst-docstrings',
'pandas-stubs',
'types-colorama',
],
},
classifiers=[
Expand Down

0 comments on commit 1539f57

Please sign in to comment.