Skip to content

Commit 5c9f081

Browse files
Check type hints with mypy (#67)
* Check type hints with mypy * fix type hints * fix type hints * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a317235 commit 5c9f081

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

.github/workflows/pipeline.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ jobs:
8585
pip install . --no-deps --no-build-isolation
8686
python -m unittest discover tests
8787
88+
mypy:
89+
needs: [black]
90+
runs-on: ubuntu-latest
91+
steps:
92+
- name: Setup Python
93+
uses: actions/setup-python@v5
94+
with:
95+
python-version: "3.13"
96+
architecture: x64
97+
- name: Checkout
98+
uses: actions/checkout@v4
99+
- name: Install mypy
100+
run: pip install mypy
101+
- name: Test
102+
run: mypy --ignore-missing-imports ${{ github.event.repository.name }}
103+
88104
pyiron_atomistics_tests:
89105
needs: [black]
90106
runs-on: ubuntu-latest

pyiron_dataclasses/v1/converter.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable
1+
from typing import Union
22

33
from pint import UnitRegistry
44

@@ -73,14 +73,22 @@
7373
)
7474

7575

76-
def get_dataclass(job_dict: dict) -> Callable:
77-
funct_dict = {
78-
"<class 'pyiron_atomistics.lammps.lammps.Lammps'>": _convert_lammps_job_dict,
79-
"<class 'pyiron_atomistics.sphinx.sphinx.Sphinx'>": _convert_sphinx_job_dict,
80-
"<class 'pyiron_atomistics.vasp.vasp.Vasp'>": _convert_vasp_job_dict,
81-
"<class 'pyiron_atomistics.atomistics.master.murnaghan.Murnaghan'>": _convert_murnaghan_job_dict,
82-
}
83-
return funct_dict[job_dict["TYPE"]](job_dict=job_dict)
76+
def get_dataclass(job_dict: dict) -> Union[LammpsJob, VaspJob, SphinxJob, MurnaghanJob]:
77+
if job_dict["TYPE"] == "<class 'pyiron_atomistics.lammps.lammps.Lammps'>":
78+
return _convert_lammps_job_dict(job_dict=job_dict)
79+
elif job_dict["TYPE"] == "<class 'pyiron_atomistics.sphinx.sphinx.Sphinx'>":
80+
return _convert_sphinx_job_dict(job_dict=job_dict)
81+
elif job_dict["TYPE"] == "<class 'pyiron_atomistics.vasp.vasp.Vasp'>":
82+
return _convert_vasp_job_dict(job_dict=job_dict)
83+
elif (
84+
job_dict["TYPE"]
85+
== "<class 'pyiron_atomistics.atomistics.master.murnaghan.Murnaghan'>"
86+
):
87+
return _convert_murnaghan_job_dict(job_dict=job_dict)
88+
else:
89+
raise KeyError(
90+
"JobType {} is not supported by pyiron_dataclass.".format(job_dict["TYPE"])
91+
)
8492

8593

8694
def _convert_sphinx_job_dict(job_dict: dict) -> SphinxJob:
@@ -625,7 +633,7 @@ def _convert_lammps_job_dict(job_dict: dict) -> LammpsJob:
625633
)
626634

627635

628-
def _convert_vasp_job_dict(job_dict):
636+
def _convert_vasp_job_dict(job_dict: dict) -> VaspJob:
629637
ureg = UnitRegistry()
630638
generic_input_dict = convert_generic_parameters_to_dictionary(
631639
generic_parameter_dict=job_dict["input"]["generic"],
@@ -931,7 +939,7 @@ def _convert_vasp_job_dict(job_dict):
931939
)
932940

933941

934-
def _convert_murnaghan_job_dict(job_dict):
942+
def _convert_murnaghan_job_dict(job_dict: dict) -> MurnaghanJob:
935943
input_dict = convert_generic_parameters_to_dictionary(
936944
generic_parameter_dict=job_dict["input"]["parameters"]
937945
)

tests/test_pyiron_atomistics_static.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ def test_all(self):
4141
)
4242
else:
4343
raise ValueError()
44+
45+
def test_not_supported(self):
46+
with self.assertRaises(KeyError):
47+
get_dataclass_v1(job_dict={"TYPE": "unknown"})

0 commit comments

Comments
 (0)