diff --git a/src/datajoint/expression.py b/src/datajoint/expression.py index d90e363eb..a9a7ddfe7 100644 --- a/src/datajoint/expression.py +++ b/src/datajoint/expression.py @@ -639,11 +639,12 @@ def fetch( # Handle specific attributes requested if attrs: - if as_dict or as_dict is None: - # fetch('col1', 'col2', as_dict=True) or fetch('col1', 'col2') + if as_dict is True: + # fetch('col1', 'col2', as_dict=True) -> list of dicts return self.proj(*attrs).to_dicts(order_by=order_by, limit=limit, offset=offset, squeeze=squeeze) else: - # fetch('col1', 'col2', as_dict=False) -> tuple of arrays + # fetch('col1', 'col2') or fetch('col1', 'col2', as_dict=False) -> tuple of arrays + # This matches DJ 1.x behavior where fetch('col') returns array(['alpha', 'beta']) return self.to_arrays(*attrs, order_by=order_by, limit=limit, offset=offset, squeeze=squeeze) # Handle as_dict=True -> to_dicts() diff --git a/tests/unit/test_fetch_compat.py b/tests/unit/test_fetch_compat.py index 05e9621cf..15f5607f0 100644 --- a/tests/unit/test_fetch_compat.py +++ b/tests/unit/test_fetch_compat.py @@ -53,12 +53,23 @@ def test_fetch_as_dict_true(self, mock_expression): mock_expression.to_dicts.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=False) - def test_fetch_with_attrs_returns_dicts(self, mock_expression): - """fetch('col1', 'col2') should call proj().to_dicts().""" + def test_fetch_with_attrs_returns_arrays(self, mock_expression): + """fetch('col1', 'col2') should call to_arrays() - matches DJ 1.x behavior.""" with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) mock_expression.fetch("col1", "col2") + # DJ 1.x: fetch('col') returns array(['alpha', 'beta']), not list of dicts + mock_expression.to_arrays.assert_called_once_with( + "col1", "col2", order_by=None, limit=None, offset=None, squeeze=False + ) + + def test_fetch_with_attrs_as_dict_true(self, mock_expression): + """fetch('col1', 'col2', as_dict=True) should call proj().to_dicts().""" + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + mock_expression.fetch("col1", "col2", as_dict=True) + mock_expression.proj.assert_called_once_with("col1", "col2") mock_expression.to_dicts.assert_called_once()