From 7ec638d76f3f2d4b32033f7db93f517ea4fbeffd Mon Sep 17 00:00:00 2001 From: Nakul Santhosh Date: Fri, 10 Jul 2026 10:22:13 +0200 Subject: [PATCH 1/5] fix: add type checking for `ncores` and `memory` setters --- src/opi/input/core.py | 14 +++++--- tests/unit/test_input_core.py | 66 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/unit/test_input_core.py diff --git a/src/opi/input/core.py b/src/opi/input/core.py index 188519c3..60ec9543 100644 --- a/src/opi/input/core.py +++ b/src/opi/input/core.py @@ -118,8 +118,11 @@ def ncores(self, value: int | None) -> None: ---------- value : int | None """ - if value is not None and value < 0: - raise ValueError(f"{self.__class__.__name__}.ncores must be a positive integer.") + if value is not None: + if not isinstance(value, int): + raise TypeError(f"{self.__class__.__name__}.ncores must be an integer.") + elif value < 0: + raise ValueError(f"{self.__class__.__name__}.ncores must be a positive integer.") # << END OF IF self._ncores = value @@ -134,8 +137,11 @@ def memory(self, value: int | None) -> None: ---------- value : int | None """ - if value is not None and value < 0: - raise ValueError(f"{self.__class__.__name__}.memory must be a positive integer.") + if value is not None: + if not isinstance(value, int): + raise TypeError(f"{self.__class__.__name__}.memory must be an integer.") + elif value < 0: + raise ValueError(f"{self.__class__.__name__}.memory must be a positive integer.") # << END OF IF self._memory = value diff --git a/tests/unit/test_input_core.py b/tests/unit/test_input_core.py new file mode 100644 index 00000000..a97734ef --- /dev/null +++ b/tests/unit/test_input_core.py @@ -0,0 +1,66 @@ +import pytest + +from opi.core import Calculator + +""" +This module contains tests for the `ncores` and `memory` properties of the `Input` class. +""" + + +@pytest.fixture +def empty_calc(): + """An empty instance of `Calculator`.""" + empty_calc = Calculator("test", version_check=False) + return empty_calc + + +@pytest.mark.unit +@pytest.mark.input +@pytest.mark.parametrize("ncores", [0, 4, None]) +def test_ncores_valid(empty_calc: Calculator, ncores: int | None): + """Test for `Input.ncores` setter with valid values.""" + empty_calc.input.ncores = ncores + assert empty_calc.input.ncores == ncores + + +@pytest.mark.unit +@pytest.mark.input +@pytest.mark.parametrize("ncores", ["4", 4.0, [4]]) +def test_ncores_wrong_type(empty_calc: Calculator, ncores: object): + """Test for `Input.ncores` setter with values of the wrong type.""" + with pytest.raises(TypeError): + empty_calc.input.ncores = ncores + + +@pytest.mark.unit +@pytest.mark.input +def test_ncores_negative(empty_calc: Calculator): + """Test for `Input.ncores` setter with an invalid negative value.""" + with pytest.raises(ValueError): + empty_calc.input.ncores = -1 + + +@pytest.mark.unit +@pytest.mark.input +@pytest.mark.parametrize("memory", [0, 4000, None]) +def test_memory_valid(empty_calc: Calculator, memory: int | None): + """Test for `Input.memory` setter with valid values.""" + empty_calc.input.memory = memory + assert empty_calc.input.memory == memory + + +@pytest.mark.unit +@pytest.mark.input +@pytest.mark.parametrize("memory", ["4000", 4000.0, [4000]]) +def test_memory_wrong_type(empty_calc: Calculator, memory: object): + """Test for `Input.memory` setter with values of the wrong type.""" + with pytest.raises(TypeError): + empty_calc.input.memory = memory + + +@pytest.mark.unit +@pytest.mark.input +def test_memory_negative(empty_calc: Calculator): + """Test for `Input.memory` setter with an invalid negative value.""" + with pytest.raises(ValueError): + empty_calc.input.memory = -1 From a023e4628d90068bcc93514ae4ae501afbffc581 Mon Sep 17 00:00:00 2001 From: Nakul Santhosh Date: Fri, 10 Jul 2026 14:27:03 +0200 Subject: [PATCH 2/5] fix: update docstrings --- src/opi/input/core.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/opi/input/core.py b/src/opi/input/core.py index 60ec9543..4f66a8a6 100644 --- a/src/opi/input/core.py +++ b/src/opi/input/core.py @@ -114,9 +114,19 @@ def ncores(self) -> int | None: @ncores.setter def ncores(self, value: int | None) -> None: """ + Setter for the ncores property. Value given must be a positive integer. + Parameters ---------- value : int | None + + Raises + ------ + TypeError + If the given value is not an integer. + + ValueError + If the given value is negative. """ if value is not None: if not isinstance(value, int): @@ -133,9 +143,19 @@ def memory(self) -> int | None: @memory.setter def memory(self, value: int | None) -> None: """ + Setter for the memory property. Value given must be a positive integer. + Parameters ---------- value : int | None + + Raises + ------ + TypeError + If the given value is not an integer. + + ValueError + If the given value is negative. """ if value is not None: if not isinstance(value, int): From dcf0d4939c282cc1ac7110bf2432289c38486bdd Mon Sep 17 00:00:00 2001 From: Nakul Santhosh Date: Fri, 10 Jul 2026 14:29:31 +0200 Subject: [PATCH 3/5] fix: fix wording of docstring --- src/opi/input/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/opi/input/core.py b/src/opi/input/core.py index 4f66a8a6..fa238c80 100644 --- a/src/opi/input/core.py +++ b/src/opi/input/core.py @@ -114,7 +114,7 @@ def ncores(self) -> int | None: @ncores.setter def ncores(self, value: int | None) -> None: """ - Setter for the ncores property. Value given must be a positive integer. + Setter for the ncores property. Value given must be a non-negative integer. Parameters ---------- @@ -143,7 +143,7 @@ def memory(self) -> int | None: @memory.setter def memory(self, value: int | None) -> None: """ - Setter for the memory property. Value given must be a positive integer. + Setter for the memory property. Value given must be a non-negative integer. Parameters ---------- From 8341dbaaf539118798755a5d499c58c524c74e7b Mon Sep 17 00:00:00 2001 From: Nakul Santhosh Date: Mon, 13 Jul 2026 14:33:37 +0200 Subject: [PATCH 4/5] fix: add to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5e21f08..b37a07cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Added fallback keyword argument for `get_final_energy`, `get_gradient`, and `get_structure` that allows to parse these properties from the `.out` file if no JSON output is available (#237). - Added `get_frequencies`, `get_imaginary_frequencies`, `is_pes_minimum`, and `is_pes_transition_state` to the `Output` class (#247). - `Output.parse()`, `Output.parse_property()`, `Output.parse_gbw()`, and `Output.__init__()` now accept a `strict` parameter (default True). When set to False, output fields that fail Pydantic validation are silently set to None and a UserWarning is emitted instead of raising a ValidationError (#248). +- Add type checks for ncores and memory setters in `Output` and checks for invalid negative values (#261). ### Changed - Refactored methods from Runner into BaseRunner (#193) From 9837156e9f44f08401a789a34b713f803f7b9bad Mon Sep 17 00:00:00 2001 From: Hagen Neugebauer <38649381+haneug@users.noreply.github.com> Date: Tue, 14 Jul 2026 07:19:38 +0200 Subject: [PATCH 5/5] chore: small fix in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ce552c..3ef7c55c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ - Added `rmsd` and `rmsd_kabsch` to calculate RMSD without and with rotational alignment to `Structure` class (#230). - Added `calc_rotational_constants` to calculate molecular rotational constants to `Structure` class (#230). - Added `calc_rotor_type` to classify a molecule's rotor type to `Structure` class (#230). -- Add type checks for ncores and memory setters in `Output` and checks for invalid negative values (#261). +- Add type checks for ncores and memory setters in `Input` and checks for invalid negative values (#261). ### Changed - Refactored methods from Runner into BaseRunner (#193)