From bbd72c5e02189359de9eb315f961ed4294455cbe Mon Sep 17 00:00:00 2001 From: andrewtarzia Date: Tue, 13 Feb 2024 12:49:18 +0100 Subject: [PATCH] Typehint tests/molecules/building_blocks. --- .../molecules/building_block/case_data.py | 66 ++++++----- .../molecules/building_block/conftest.py | 23 ++-- .../building_block/fixtures/init_from_file.py | 108 +++++++++--------- .../building_block/test_get_core_atom_ids.py | 18 +-- .../test_get_functional_groups.py | 40 ++++--- .../test_get_num_functional_groups.py | 20 ++-- .../building_block/test_get_placer_ids.py | 40 ++++--- .../molecules/building_block/test_repr.py | 39 ++++--- .../test_with_functional_groups.py | 46 ++++---- 9 files changed, 206 insertions(+), 194 deletions(-) diff --git a/tests/molecular/molecules/building_block/case_data.py b/tests/molecular/molecules/building_block/case_data.py index 0fb4a3a44..41e3ae15f 100644 --- a/tests/molecular/molecules/building_block/case_data.py +++ b/tests/molecular/molecules/building_block/case_data.py @@ -1,54 +1,58 @@ +import stk + + class CaseData: """ A test case. - Attributes - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Attributes: + building_block: + The building block, which will be written to a file, so + that it can be initialized from it. - functional_groups : :class:`tuple` of :class:`.FunctionalGroup` - The functional groups :attr:`.building_block` should be - holding. + functional_groups: + The functional groups :attr:`.building_block` should be + holding. - known_repr : str - The representation of the building block. + known_repr: + The representation of the building block. - core_atom_ids : :class:`tuple` of :class:`int` - The correct core atom ids for :attr:`.building_block`. + core_atom_ids: + The correct core atom ids for :attr:`.building_block`. - placer_ids : :class:`tuple` of :class:`int` - The correct *placer* ids for :attr:`.building_block`. + placer_ids: + The correct *placer* ids for :attr:`.building_block`. """ def __init__( self, - building_block, - functional_groups, - known_repr, - core_atom_ids, - placer_ids, - ): + building_block: stk.BuildingBlock, + functional_groups: tuple, + known_repr: str, + core_atom_ids: tuple[int, ...], + placer_ids: tuple[int, ...], + ) -> None: """ Initialize a :class:`.CaseData` instance. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block, which will be written to a file, so + that it can be initialized from it. - functional_groups : :class:`tuple` of :class:`.FunctionalGroup` - The functional groups `building_block` should be holding. + functional_groups: + The functional groups :attr:`.building_block` should be + holding. - known_repr : str - The representation of the building block. + known_repr: + The representation of the building block. - core_atom_ids : :class:`tuple` of :class:`int` - The correct core atom ids for `building_block`. + core_atom_ids: + The correct core atom ids for :attr:`.building_block`. - placer_ids : :class:`tuple` of :class:`int` - The correct *placer* ids for `building_block`. + placer_ids: + The correct *placer* ids for :attr:`.building_block`. """ diff --git a/tests/molecular/molecules/building_block/conftest.py b/tests/molecular/molecules/building_block/conftest.py index bd387e265..a2b6c894d 100644 --- a/tests/molecular/molecules/building_block/conftest.py +++ b/tests/molecular/molecules/building_block/conftest.py @@ -7,6 +7,8 @@ # Fixtures need be visible for lazy_fixture() calls. from .fixtures import * # noqa +from .case_data import CaseData + @pytest.fixture( params=( @@ -17,7 +19,7 @@ lazy_fixture("init_from_rdkit_mol"), ), ) -def case_data(request): +def case_data(request) -> CaseData: """ A :class:`.CaseData` instance. @@ -27,7 +29,7 @@ def case_data(request): @pytest.fixture -def building_block(case_data): +def building_block(case_data: CaseData) -> stk.BuildingBlock: """ A :class:`.BuildingBlock` instance. @@ -48,18 +50,17 @@ def building_block(case_data): ), ) ) -def get_functional_groups(request): +def get_functional_groups( + request: pytest.FixtureRequest, +) -> stk.FunctionalGroup: """ - Yield the functional groups of a `molecule`. + Get the functional groups of a `molecule`. - Parameters - ---------- - molecule : :class:`.Molecule` - The molecule whose functional groups should be gotten. + Parameters: + molecule: + The molecule whose functional groups should be gotten. - Yields - ------ - :class:`.FunctionalGroup` + Returns: A functional group of `molecule`. """ diff --git a/tests/molecular/molecules/building_block/fixtures/init_from_file.py b/tests/molecular/molecules/building_block/fixtures/init_from_file.py index 606d0be86..f15cf53ba 100644 --- a/tests/molecular/molecules/building_block/fixtures/init_from_file.py +++ b/tests/molecular/molecules/building_block/fixtures/init_from_file.py @@ -1,13 +1,11 @@ import pytest import stk - +from collections.abc import Iterable from ..case_data import CaseData @pytest.fixture( - params=[ - "building_block.mol", - ], + params=["building_block.mol"], ) def path(tmpdir, request): return tmpdir / request.param @@ -17,78 +15,76 @@ class InitFromFileData: """ Stores data for the :meth:`.BuildingBlock.init_from_file`. - Attributes - ---------- - building_block : :class:`.BuildingBlock` - The building block, which will be written to a file, so that it - can be initialized from it. + Attributes: + building_block: + The building block, which will be written to a file, so + that it can be initialized from it. - init_functional_groups : :class:`iterable` - Passed to the `functional_groups` parameter of - :meth:`.BuildingBlock.init_from_file`. + init_functional_groups: + Passed to the `functional_groups` parameter of + :meth:`.BuildingBlock.init_from_file`. - init_placer_ids : :class:`tuple` or :class:`NoneType` - Passed to the `placer_ids` parameter of - :meth:`.BuildingBlock.init_from_file`. + init_placer_ids: + Passed to the `placer_ids` parameter of + :meth:`.BuildingBlock.init_from_file`. - case_data_functional_groups : :class:`tuple` - The functional groups the initialized building block should - have. + case_data_functional_groups: + The functional groups the initialized building block should + have. - case_data_core_atom_ids : :class:`tuple` of :class:`int` - The ids of core atoms the initialized building block should - have. + case_data_core_atom_ids: + The ids of core atoms the initialized building block should + have. - case_data_placer_ids : :class:`tuple` of :class:`int` - The ids of *placer* atoms the initialized building block should - have. + case_data_placer_ids: : :class:`tuple` of :class:`int` + The ids of *placer* atoms the initialized building block + should have. - known_repr : str - The representation of the building block. + known_repr: + The representation of the building block. """ def __init__( self, - building_block, - init_functional_groups, - init_placer_ids, - case_data_functional_groups, - case_data_core_atom_ids, - case_data_placer_ids, - known_repr, - ): + building_block: stk.BuildingBlock, + init_functional_groups: Iterable, + init_placer_ids: tuple | None, + case_data_functional_groups: tuple, + case_data_core_atom_ids: tuple[int, ...], + case_data_placer_ids: tuple[int, ...], + known_repr: str, + ) -> None: """ Initialize a :class:`.InitFromData` instance. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block, which will be written to a file, so - that it can be initialized from it. + Parameters: + building_block: + The building block, which will be written to a file, so + that it can be initialized from it. - init_functional_groups : :class:`iterable` - Passed to the `functional_groups` parameter of - :meth:`.BuildingBlock.init_from_file`. + init_functional_groups: + Passed to the `functional_groups` parameter of + :meth:`.BuildingBlock.init_from_file`. - init_placer_ids : :class:`tuple` or :class:`NoneType` - Passed to the `placer_ids` parameter of - :meth:`.BuildingBlock.init_from_file`. + init_placer_ids: + Passed to the `placer_ids` parameter of + :meth:`.BuildingBlock.init_from_file`. - case_data_functional_groups : :class:`tuple` - The functional groups the initialized building block should - have. + case_data_functional_groups: + The functional groups the initialized building block should + have. - case_data_core_atom_ids : :class:`tuple` of :class:`int` - The ids of core atoms the initialized building block should - have. + case_data_core_atom_ids: + The ids of core atoms the initialized building block should + have. - case_data_placer_ids : :class:`tuple` of :class:`int` - The ids of *placer* atoms the initialized building block - should have. + case_data_placer_ids: : :class:`tuple` of :class:`int` + The ids of *placer* atoms the initialized building block + should have. - known_repr : str - The representation of the building block. + known_repr: + The representation of the building block. """ diff --git a/tests/molecular/molecules/building_block/test_get_core_atom_ids.py b/tests/molecular/molecules/building_block/test_get_core_atom_ids.py index c8b28cb64..76ff857be 100644 --- a/tests/molecular/molecules/building_block/test_get_core_atom_ids.py +++ b/tests/molecular/molecules/building_block/test_get_core_atom_ids.py @@ -1,19 +1,19 @@ import itertools as it +from .case_data import CaseData -def test_get_core_atom_ids(case_data): + +def test_get_core_atom_ids(case_data: CaseData) -> None: """ Test :meth:`.BuildingBlock.get_core_atom_ids`. - Parameters - ---------- - case_data : :class:`.CaseData` - A test case. Holds the building block to test and the correct - core atom ids. + Parameters: + case_data: + A test case. Holds the building block to test and the correct + core atom ids. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ diff --git a/tests/molecular/molecules/building_block/test_get_functional_groups.py b/tests/molecular/molecules/building_block/test_get_functional_groups.py index c6dd4e86e..b429fc754 100644 --- a/tests/molecular/molecules/building_block/test_get_functional_groups.py +++ b/tests/molecular/molecules/building_block/test_get_functional_groups.py @@ -2,20 +2,21 @@ from ..utilities import is_equivalent_functional_group +import stk +from .case_data import CaseData -def test_get_functional_groups(case_data): + +def test_get_functional_groups(case_data: CaseData) -> None: """ Test :meth:`.BuildingBlock.get_functional_groups`. - Parameters - ---------- - case_data : :class:`.CaseData` - A test case. Holds the building block to test and the correct - functional groups. + Parameters: + case_data: + A test case. Holds the building block to test and the correct + functional groups. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ @@ -25,21 +26,22 @@ def test_get_functional_groups(case_data): ) -def _test_get_functional_groups(building_block, functional_groups): +def _test_get_functional_groups( + building_block: stk.BuildingBlock, + functional_groups: tuple[stk.FunctionalGroup], +) -> None: """ Test :meth:`.BuildingBlock.get_functional_groups`. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block to test. - functional_groups : :class:`tuple` of :class:`.FunctionalGroup` - The correct functional groups. + functional_groups: + The correct functional groups. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ diff --git a/tests/molecular/molecules/building_block/test_get_num_functional_groups.py b/tests/molecular/molecules/building_block/test_get_num_functional_groups.py index 22d5db98b..9981ab7cf 100644 --- a/tests/molecular/molecules/building_block/test_get_num_functional_groups.py +++ b/tests/molecular/molecules/building_block/test_get_num_functional_groups.py @@ -1,16 +1,18 @@ -def test_get_num_functional_groups(case_data): +from .case_data import CaseData + + +def test_get_num_functional_groups(case_data: CaseData) -> None: """ Test :meth:`.BuildingBlock.get_num_functional_groups`. - Parameters - ---------- - case_data : :class:`.CaseData` - A test case. Holds the building block to test and the - correct number of functional groups. + Parameters: + case_data: + A test case. Holds the building block to test and the correct + functional groups. + + Returns: + :class:`NoneType` - Returns - ------- - None : :class:`NoneType` """ diff --git a/tests/molecular/molecules/building_block/test_get_placer_ids.py b/tests/molecular/molecules/building_block/test_get_placer_ids.py index 7ac1d2b8a..d703a88b6 100644 --- a/tests/molecular/molecules/building_block/test_get_placer_ids.py +++ b/tests/molecular/molecules/building_block/test_get_placer_ids.py @@ -1,19 +1,20 @@ import itertools as it +from .case_data import CaseData +import stk -def test_get_placer_ids(case_data): + +def test_get_placer_ids(case_data: CaseData) -> None: """ Test :meth:`.BuildingBlock.get_placer_ids`. - Parameters - ---------- - case_data : :class:`.CaseData` - A test case. Holds the building block to test and the - correct placer ids. + Parameters: + case_data: + A test case. Holds the building block to test and the + correct placer ids. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ @@ -23,21 +24,22 @@ def test_get_placer_ids(case_data): ) -def _test_get_placer_ids(building_block, placer_ids): +def _test_get_placer_ids( + building_block: stk.BuildingBlock, + placer_ids: tuple[int, ...], +) -> None: """ Test :meth:`.BuildingBlock.get_placer_ids`. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block to test. - placer_ids : :class:`tuple` of :class:`int` - The correct placer ids. + placer_ids: + The correct placer ids. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ diff --git a/tests/molecular/molecules/building_block/test_repr.py b/tests/molecular/molecules/building_block/test_repr.py index ae8735aa5..d65c2f7a2 100644 --- a/tests/molecular/molecules/building_block/test_repr.py +++ b/tests/molecular/molecules/building_block/test_repr.py @@ -1,16 +1,18 @@ -def test_repr(case_data): +from .case_data import CaseData + +import stk + + +def test_repr(case_data: CaseData) -> None: """ Test :meth:`.BuildingBlock.__repr__`. - Parameters - ---------- - case_data : :class:`.CaseData` - A test case. Holds the building block to test and the correct - functional groups. + Parameters: + case_data: + A test case. Holds the building block to test. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ @@ -20,22 +22,21 @@ def test_repr(case_data): ) -def _test_repr(building_block, known_repr): +def _test_repr(building_block: stk.BuildingBlock, known_repr: str) -> None: """ Test :meth:`.BuildingBlock.get_functional_groups`. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block to test. - functional_groups : :class:`tuple` of :class:`.FunctionalGroup` - The correct functional groups. + known_repr: + The correct representation. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ + print(building_block.__repr__(), known_repr) assert building_block.__repr__() == known_repr diff --git a/tests/molecular/molecules/building_block/test_with_functional_groups.py b/tests/molecular/molecules/building_block/test_with_functional_groups.py index f52ff9100..86c712066 100644 --- a/tests/molecular/molecules/building_block/test_with_functional_groups.py +++ b/tests/molecular/molecules/building_block/test_with_functional_groups.py @@ -4,24 +4,27 @@ is_equivalent_building_block, is_equivalent_molecule, ) +import stk +from collections import abc -def test_with_functional_groups(building_block, get_functional_groups): +def test_with_functional_groups( + building_block: stk.BuildingBlock, + get_functional_groups: abc.Callable, +) -> None: """ Test :meth:`.BuildingBlock.with_functional_groups`. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block to test. - get_functional_groups : :class:`callable` - Takes a single parameter, `building_block` and returns the - `functional_groups` parameter to use for this test. + get_functional_groups: + Takes a single parameter, `building_block` and returns the + `functional_groups` parameter to use for this test. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """ @@ -35,21 +38,22 @@ def test_with_functional_groups(building_block, get_functional_groups): has_same_structure(building_block, clone) -def _test_with_functional_groups(building_block, functional_groups): +def _test_with_functional_groups( + building_block: stk.BuildingBlock, + functional_groups: tuple[stk.FunctionalGroup], +) -> None: """ Test :meth:`.BuildingBlock.with_functional_groups`. - Parameters - ---------- - building_block : :class:`.BuildingBlock` - The building block to test. + Parameters: + building_block: + The building block to test. - functional_groups : :class:`tuple` of :class:`.FunctionalGroup` - The functional groups the new building block should hold. + functional_groups: + The functional groups the new building block should hold. - Returns - ------- - None : :class:`NoneType` + Returns: + :class:`NoneType` """