Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a FLATTEN_RECORD_LIST formula #1215

Closed
wants to merge 3 commits into from

Conversation

hexaltation
Copy link
Collaborator

Context

Formulas Carts.lookupRecords(customer=$id).Cart returns a "list of lists/RecordSet" ([Foods[[1, 2, 3]], Foods[[1]]]) which is not handled properly by Grist when trying to type it as a Reference List.

A fix is to return a flattened list using a list comprehension [el for val in Carts.lookupRecords(customer=$id).Cart for el in val]

Proposed solution

Add a FLATTEN_RECORD_LIST(RecordSet, dedup=False) formula that returns a much more manageable list of Records

Related issues

Fixes #1130

Has this been tested?

  • 👍 yes, I added tests to the test suite
  • 💭 no, because this PR is a draft and still needs work
  • 🙅 no, because this is not relevant here
  • 🙋 no, because I need help

I'm not sure of the best way to test this. Creating a .grist file that verify the formula returns the good result in nbrowser tests or python tests inside the docstring, but how create a proper RecordSet to do unit tests ?

Thanks for your help.

Copy link
Collaborator

@fflorent fflorent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hexaltation! Few remarks from my side.

def FLATTEN_RECORDS_LIST(list_of_records, dedup=False):
"""Returns a flat list of records"""
if type(list_of_records) is not list:
raise TypeError("Unexpected Argument, waiting of a list of Records")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't waiting for a list of Records more English?

return list_of_records
flatten_list = [el for val in list_of_records for el in val]
el_type = {el.__class__.__name__ for el in flatten_list}
if list(el_type)[0] != 'Record':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have to check the first element only?

Wouldn't 'Record' in el_type be more robust?

@berhalak
Copy link
Contributor

berhalak commented Oct 7, 2024

Closing this one. I'll create new one with option 2 from the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lists of RecordSets cannot be typed as Reference List
3 participants