From e813b668031c57eabbe086830a597733674aaaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Sat, 21 Dec 2024 19:55:08 -0500 Subject: [PATCH] ENH: Add `None` to b-vals iterators `size` parameter annotations Add `None` to b-vals iterators `size` parameter annotations. Fixes: ``` src/nifreeze/utils.py:30: error: Incompatible default for argument "size" (default has type "None", argument has type "int") [assignment] src/nifreeze/utils.py:30: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True src/nifreeze/utils.py:30: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase src/nifreeze/utils.py:59: error: Incompatible default for argument "size" (default has type "None", argument has type "int") [assignment] src/nifreeze/utils.py:59: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True src/nifreeze/utils.py:59: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase src/nifreeze/utils.py:108: error: Incompatible default for argument "size" (default has type "None", argument has type "int") [assignment] src/nifreeze/utils.py:108: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True src/nifreeze/utils.py:108: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase src/nifreeze/utils.py:135: error: Incompatible default for argument "size" (default has type "None", argument has type "int") [assignment] src/nifreeze/utils.py:135: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True src/nifreeze/utils.py:135: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase ``` raised for example in: https://github.com/nipreps/nifreeze/actions/runs/12437972140/job/34728973936#step:8:32 --- src/nifreeze/utils.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/nifreeze/utils.py b/src/nifreeze/utils.py index 83886e5..cdb785b 100644 --- a/src/nifreeze/utils.py +++ b/src/nifreeze/utils.py @@ -27,15 +27,16 @@ from typing import Iterator -def linear_iterator(size: int = None, **kwargs) -> Iterator[int]: +def linear_iterator(size: int | None = None, **kwargs) -> Iterator[int]: """ Traverse the dataset volumes in ascending order. Parameters ---------- - size : :obj:`int` - Number of volumes in the dataset - (for instance, the number of orientations in a DWI) + size : :obj:`int` or None, optional + Number of volumes in the dataset (for instance, the number of + orientations in a DWI). If `None`, the size is inferred from the `bvals` + argument in `kwargs`, if present. Returns ------- @@ -56,20 +57,21 @@ def linear_iterator(size: int = None, **kwargs) -> Iterator[int]: return range(size) -def random_iterator(size: int = None, **kwargs) -> Iterator[int]: +def random_iterator(size: int | None = None, **kwargs) -> Iterator[int]: """ Traverse the dataset volumes randomly. + If the `seed` key is present in the keyword arguments, initializes the seed + of Python's `random` pseudo-random number generator library with the given + value. Specifically, if `False`, `None` is used as the seed; it `True`, a + default seed value is used. + Parameters ---------- - size : :obj:`int` - Number of volumes in the dataset - (for instance, the number of orientations in a DWI) - seed : :obj:`int` or :obj:`bool` or :obj:`bool` or ``None`` - If :obj:`int` or :obj:`str` or ``None``, initializes the seed of Python's random generator - with the given value. - If ``False``, the random generator is passed ``None``. - If ``True``, a default seed value is set. + size : :obj:`int` or None, optional + Number of volumes in the dataset (for instance, the number of + orientations in a DWI). If `None`, the size is inferred from the `bvals` + argument in `kwargs`, if present. Returns ------- @@ -105,7 +107,7 @@ def random_iterator(size: int = None, **kwargs) -> Iterator[int]: return (x for x in index_order) -def bvalue_iterator(size: int = None, **kwargs) -> Iterator[int]: +def bvalue_iterator(size: int | None = None, **kwargs) -> Iterator[int]: """ Traverse the volumes in a DWI dataset by growing b-value. @@ -132,15 +134,16 @@ def bvalue_iterator(size: int = None, **kwargs) -> Iterator[int]: return (index[1] for index in indexed_bvals) -def centralsym_iterator(size: int = None, **kwargs) -> Iterator[int]: +def centralsym_iterator(size: int | None = None, **kwargs) -> Iterator[int]: """ Traverse the dataset starting from the center and alternatingly progressing to the sides. Parameters ---------- - size : :obj:`int` - Number of volumes in the dataset - (for instance, the number of orientations in a DWI) + size : :obj:`int` or None, optional + Number of volumes in the dataset (for instance, the number of + orientations in a DWI). If `None`, the size is inferred from the `bvals` + argument in `kwargs`, if present. Returns -------