Repository: scipy/scipy
File: scipy/_lib/_util.py (Python)

Code before:
    try:
        np.array([1]).__array__(copy=None)  # type: ignore[call-overload]
        copy_if_needed = None
    except TypeError:
        copy_if_needed = False


# Wrapped function for inspect.signature for compatibility with Python 3.14+
# See gh-23913
#
# PEP 649/749 allows for underfined annotations at runtime, and added the
# `annotation_format` parameter to handle these cases.
# `annotationlib.Format.FORWARDREF` is the closest to previous behavior,
# returning ForwardRef objects fornew undefined annotations cases.
#
# Consider dropping this wrapper when support for Python 3.13 is dropped.
if sys.version_info >= (3, 14):
    import annotationlib
    def wrapped_inspect_signature(callable):
        """Get a signature object for the passed callable."""
        return inspect.signature(callable,
                                 annotation_format=annotationlib.Format.FORWARDREF)
else:
    def wrapped_inspect_signature(callable):
        """Get a signature object for the passed callable."""
        return inspect.signature(callable)



_RNG: TypeAlias = np.random.Generator | np.random.RandomState
SeedType: TypeAlias = IntNumber | _RNG | None

GeneratorType = TypeVar("GeneratorType", bound=_RNG)


def _lazyselect(condlist, choicelist, arrays, default=0):
    """
    Mimic `np.select(condlist, choicelist)`.

    Notice, it assumes that all `arrays` are of the same shape or can be
    broadcasted together.

    All functions in `choicelist` must accept array arguments in the order
    given in `arrays` and must return an array of the same shape as broadcasted
    `arrays`.

    Examples
    --------
    >>> import numpy as np
    >>> x = np.arange(6)
    >>> np.select([x <3, x > 3], [x**2, x**3], default=0)

Review comment (suggestion):
For speed / clarity, I would suggest

```suggestion
    wrapped_inspect_signature = inspect.signature
```

Code after:
    try:
        np.array([1]).__array__(copy=None)  # type: ignore[call-overload]
        copy_if_needed = None
    except TypeError:
        copy_if_needed = False


# Wrapped function for inspect.signature for compatibility with Python 3.14+
# See gh-23913
#
# PEP 649/749 allows for underfined annotations at runtime, and added the
# `annotation_format` parameter to handle these cases.
# `annotationlib.Format.FORWARDREF` is the closest to previous behavior,
# returning ForwardRef objects fornew undefined annotations cases.
#
# Consider dropping this wrapper when support for Python 3.13 is dropped.
if sys.version_info >= (3, 14):
    import annotationlib
    def wrapped_inspect_signature(callable):
        """Get a signature object for the passed callable."""
        return inspect.signature(callable,
                                 annotation_format=annotationlib.Format.FORWARDREF)
else:
    wrapped_inspect_signature = inspect.signature


_RNG: TypeAlias = np.random.Generator | np.random.RandomState
SeedType: TypeAlias = IntNumber | _RNG | None

GeneratorType = TypeVar("GeneratorType", bound=_RNG)


def _lazyselect(condlist, choicelist, arrays, default=0):
    """
    Mimic `np.select(condlist, choicelist)`.

    Notice, it assumes that all `arrays` are of the same shape or can be
    broadcasted together.

    All functions in `choicelist` must accept array arguments in the order
    given in `arrays` and must return an array of the same shape as broadcasted
    `arrays`.

    Examples
    --------
    >>> import numpy as np
    >>> x = np.arange(6)
    >>> np.select([x <3, x > 3], [x**2, x**3], default=0)
    array([  0,   1,   4,   0,  64, 125])

    >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,))