(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:
Alex Hall
2022-08-20 20:02:45 +02:00
parent 56e8e1f4b3
commit eac1f26f3e
2 changed files with 89 additions and 0 deletions

View File

@@ -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):
"""