Repository: scipy/scipy
Issue category: Bug Report

BUG: signal: Confusing error when giving an invalid mode to `correlation_lags`
### Describe your issue.

When correlation_lags is given a mode that is not "valid", "same", or "full", the `lags` array is never defined, and the function fails with `UnboundLocalError: local variable 'lags' referenced before assignment`.
The fix is very simple, I will submit a PR soon.

### Reproducing Code Example

```python
signal.correlation_lags(10, 10, "some_invalid_format")
```


### Error message

```shell
Traceback (most recent call last)

<ipython-input-25-202bff163744> in <cell line: 1>()
----> 1 signal.correlation_lags(10, 10, "some_invalid_format")

/usr/local/lib/python3.10/dist-packages/scipy/signal/_signaltools.py in correlation_lags(in1_len, in2_len, mode)
    384         else:
    385             lags = np.arange(lag_bound, 1)
--> 386     return lags
    387 
    388 

UnboundLocalError: local variable 'lags' referenced before assignment
```


### SciPy/NumPy/Python version and system information

```shell
1.13.1 1.26.4 sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.27
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.27
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  pythran:
    include directory: ../../tmp/pip-build-env-mnl4e8vy/overlay/lib/python3.10/site-packages/pythran
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp310-cp310/bin/python
  version: '3.10'

```
```



Functions to edit:
scipy/signal/_signaltools.py:correlation_lags

Patch:
diff --git a/scipy/signal/_signaltools.py b/scipy/signal/_signaltools.py
index f95b52c0c69f..3006bf014be5 100644
--- a/scipy/signal/_signaltools.py
+++ b/scipy/signal/_signaltools.py
@@ -405,6 +405,8 @@ def correlation_lags(in1_len, in2_len, mode='full'):
             lags = np.arange(lag_bound + 1)
         else:
             lags = np.arange(lag_bound, 1)
+    else:
+        raise ValueError(f"Mode {mode} is invalid")
     return lags
 
 

Test patch: diff --git a/scipy/signal/tests/test_signaltools.py b/scipy/signal/tests/test_signaltools.py
index c5a5028e4624..4f8a573696b1 100644
--- a/scipy/signal/tests/test_signaltools.py
+++ b/scipy/signal/tests/test_signaltools.py
@@ -2135,6 +2135,11 @@ def test_correlation_lags(mode, behind, input_size):
     assert_equal(lags.shape, correlation.shape)
 
 
+def test_correlation_lags_invalid_mode():
+    with pytest.raises(ValueError, match="Mode asdfgh is invalid"):
+        correlation_lags(100, 100, mode="asdfgh")
+
+
 @pytest.mark.parametrize('dt', [np.csingle, np.cdouble,
                                 pytest.param(np.clongdouble, marks=_pmf)])
 class TestCorrelateComplex: