mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) More helpful messages when formula probably needs to use Table.all
Summary: Raise an exception with a customised message for two cases when a user tries on operation directly on a table without `.all`: 1. For `Table.Col`, where `Col` is an existing column, suggest `Table.all.Col`. If `Col` doesn't exist as a column, fall back to the standard AttributeError. 2. When iterating directly over a table, e.g. `[r for r in Table]`, suggest looping over `Table.all` instead. Test Plan: Added Python unit tests. Reviewers: georgegevoian Reviewed By: georgegevoian Differential Revision: https://phab.getgrist.com/D3593
This commit is contained in:
@@ -133,6 +133,20 @@ class UserTable(object):
|
||||
# the constructor.
|
||||
return []
|
||||
|
||||
def __getattr__(self, item):
|
||||
if self.table.has_column(item):
|
||||
raise AttributeError(
|
||||
"To retrieve all values in a column, use `{table_id}.all.{item}`. "
|
||||
"Tables have no attribute '{item}'".format(table_id=self.table.table_id, item=item)
|
||||
)
|
||||
super(UserTable, self).__getattribute__(item)
|
||||
|
||||
def __iter__(self):
|
||||
raise TypeError(
|
||||
"To iterate (loop) over all records in a table, use `{table_id}.all`. "
|
||||
"Tables are not directly iterable.".format(table_id=self.table.table_id)
|
||||
)
|
||||
|
||||
|
||||
class Table(object):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user