Skip to content

Bug: fetch in DJ2.0 behaves differently compared to DJ. 0.14 #1381

@arturoptophys

Description

@arturoptophys

Bug Report

Description

Now that fetch has returned for backwards compatibility. I see 3 differences which might still lead to issues:

  1. fetch seems not to be classmethod anymore
  2. fetch defaults to as_dict=True in dj.2.0 but was as_dict=False in dj.0.14
  3. fetch('KEY') doesnt work

Reproducibility

import numpy as np
import datajoint as dj

dj.conn()  # using datajoint.json
schema = dj.Schema("TEST")

@schema
class RandomLookup(dj.Lookup):
    definition = """
    idx: int32
    ---
    values1: float32
    """
    contents = [(0, 0.0), (1, 1.0), (2, 2.0), (3, 3.0), (4, 4.0)]

RandomLookup.fetch('values1') # works in dj.014

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[38], line 1
----> 1 RandomLookup.fetch('values1')

File ~/datajoint-python/src/datajoint/expression.py:654, in QueryExpression.fetch(self, offset, limit, order_by, format, as_dict, squeeze, *attrs)
    651     return self.to_dicts(order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)
    653 # Default: return structured array (legacy behavior)
--> 654 return self.to_arrays(order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)

AttributeError: 'str' object has no attribute 'to_arrays'

print(isinstance(RandomLookup().fetch('values1')[0],dict)) # True

print(isinstance(RandomLookup().fetch('values1',as_dict=False)[0],dict)) # False

RandomNumbers().fetch('KEY')

---------------------------------------------------------------------------
DataJointError                            Traceback (most recent call last)
Cell In[41], line 1
----> 1 RandomNumbers().fetch('KEY')

File ~/datajoint-python/src/datajoint/expression.py:644, in QueryExpression.fetch(self, offset, limit, order_by, format, as_dict, squeeze, *attrs)
    641 if attrs:
    642     if as_dict or as_dict is None:
    643         # fetch('col1', 'col2', as_dict=True) or fetch('col1', 'col2')
--> 644         return self.proj(*attrs).to_dicts(order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)
    645     else:
    646         # fetch('col1', 'col2', as_dict=False) -> tuple of arrays
    647         return self.to_arrays(*attrs, order_by=order_by, limit=limit, offset=offset, squeeze=squeeze)

File ~/datajoint-python/src/datajoint/expression.py:498, in QueryExpression.proj(self, *attributes, **named_attributes)
    496 # check that all attributes exist in heading
    497 try:
--> 498     raise DataJointError("Attribute `%s` not found." % next(a for a in attributes if a not in self.heading.names))
    499 except StopIteration:
    500     pass  # all ok

DataJointError: Attribute `KEY` not found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviortriageIndicates issues, pull requests, or discussions need to be reviewed for the first time

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions