Skip to content

Commit 6e196cd

Browse files
authored
Merge pull request #1365 from datajoint/fix/fetch-attrs-array
fix: fetch('column') returns array instead of list of dicts
2 parents 71b63db + d81af9b commit 6e196cd

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/datajoint/expression.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,12 @@ def fetch(
639639

640640
# Handle specific attributes requested
641641
if attrs:
642-
if as_dict or as_dict is None:
643-
# fetch('col1', 'col2', as_dict=True) or fetch('col1', 'col2')
642+
if as_dict is True:
643+
# fetch('col1', 'col2', as_dict=True) -> list of dicts
644644
return self.proj(*attrs).to_dicts(order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)
645645
else:
646-
# fetch('col1', 'col2', as_dict=False) -> tuple of arrays
646+
# fetch('col1', 'col2') or fetch('col1', 'col2', as_dict=False) -> tuple of arrays
647+
# This matches DJ 1.x behavior where fetch('col') returns array(['alpha', 'beta'])
647648
return self.to_arrays(*attrs, order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)
648649

649650
# Handle as_dict=True -> to_dicts()

tests/unit/test_fetch_compat.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,23 @@ def test_fetch_as_dict_true(self, mock_expression):
5353

5454
mock_expression.to_dicts.assert_called_once_with(order_by=None, limit=None, offset=None, squeeze=False)
5555

56-
def test_fetch_with_attrs_returns_dicts(self, mock_expression):
57-
"""fetch('col1', 'col2') should call proj().to_dicts()."""
56+
def test_fetch_with_attrs_returns_arrays(self, mock_expression):
57+
"""fetch('col1', 'col2') should call to_arrays() - matches DJ 1.x behavior."""
5858
with warnings.catch_warnings():
5959
warnings.simplefilter("ignore", DeprecationWarning)
6060
mock_expression.fetch("col1", "col2")
6161

62+
# DJ 1.x: fetch('col') returns array(['alpha', 'beta']), not list of dicts
63+
mock_expression.to_arrays.assert_called_once_with(
64+
"col1", "col2", order_by=None, limit=None, offset=None, squeeze=False
65+
)
66+
67+
def test_fetch_with_attrs_as_dict_true(self, mock_expression):
68+
"""fetch('col1', 'col2', as_dict=True) should call proj().to_dicts()."""
69+
with warnings.catch_warnings():
70+
warnings.simplefilter("ignore", DeprecationWarning)
71+
mock_expression.fetch("col1", "col2", as_dict=True)
72+
6273
mock_expression.proj.assert_called_once_with("col1", "col2")
6374
mock_expression.to_dicts.assert_called_once()
6475

0 commit comments

Comments
 (0)