(core) Make CONTAINS a function for consistency with mkpydocs etc.

Summary: Having CONTAINS be a class is a pain, undoing that mistake now

Test Plan: none needed

Reviewers: dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2929
This commit is contained in:
Alex Hall 2021-07-20 22:57:35 +02:00
parent 1f6e693b6e
commit f5981606e1
5 changed files with 21 additions and 12 deletions

View File

@ -35,7 +35,7 @@ class AutocompleteContext(object):
# TODO It would be nice to include builtin functions too, but getargspec doesn't work there. # TODO It would be nice to include builtin functions too, but getargspec doesn't work there.
self._functions = {} self._functions = {}
for key, value in six.iteritems(self._context): for key, value in six.iteritems(self._context):
if value and callable(value) and not isinstance(value, type): if value and callable(value):
argspec = inspect.formatargspec(*inspect.getargspec(value)) argspec = inspect.formatargspec(*inspect.getargspec(value))
self._functions[key] = Completion(key, argspec, is_grist_func(value)) self._functions[key] = Completion(key, argspec, is_grist_func(value))

View File

@ -151,7 +151,7 @@ def VLOOKUP(table, **field_value_pairs):
""" """
return table.lookupOne(**field_value_pairs) return table.lookupOne(**field_value_pairs)
class CONTAINS(namedtuple("CONTAINS", "value")): class _Contains(namedtuple("_Contains", "value")):
""" """
Use this marker with `Table.lookupRecords` to find records Use this marker with `Table.lookupRecords` to find records
where a column contains the given value, e.g: where a column contains the given value, e.g:
@ -171,4 +171,13 @@ class CONTAINS(namedtuple("CONTAINS", "value")):
# While users should apply this marker to values in queries, internally # While users should apply this marker to values in queries, internally
# the marker is moved to the column ID so that the LookupMapColumn knows how to # the marker is moved to the column ID so that the LookupMapColumn knows how to
# update its index correctly for that column. # update its index correctly for that column.
# The _Contains class is used internally, especially with isinstance()
# The CONTAINS function is for users
# Having a function as the interface makes things like docs and autocomplete
# work more consistently
pass pass
def CONTAINS(value):
return _Contains(value)
CONTAINS.__doc__ = _Contains.__doc__

View File

@ -9,7 +9,7 @@ import records
import relation import relation
import twowaymap import twowaymap
import usertypes import usertypes
from functions.lookup import CONTAINS from functions.lookup import _Contains
import logger import logger
log = logger.Logger(__name__, logger.INFO) log = logger.Logger(__name__, logger.INFO)
@ -187,7 +187,7 @@ class ContainsLookupMapColumn(BaseLookupMapColumn):
# that the columns used to index by are brought up-to-date (in case they are formula columns). # that the columns used to index by are brought up-to-date (in case they are formula columns).
group = rec._get_col(extract_column_id(col_id)) group = rec._get_col(extract_column_id(col_id))
if isinstance(col_id, CONTAINS): if isinstance(col_id, _Contains):
# Check that the cell targeted by CONTAINS() has an appropriate type. # Check that the cell targeted by CONTAINS() has an appropriate type.
# Don't iterate over characters of a string. # Don't iterate over characters of a string.
# group = [] essentially means there are no new keys in this call # group = [] essentially means there are no new keys in this call
@ -300,7 +300,7 @@ class _LookupRelation(relation.Relation):
def extract_column_id(c): def extract_column_id(c):
if isinstance(c, CONTAINS): if isinstance(c, _Contains):
return c.value return c.value
else: else:
return c return c

View File

@ -417,12 +417,12 @@ class Table(object):
col_ids = [] col_ids = []
for col_id in sorted(kwargs): for col_id in sorted(kwargs):
value = kwargs[col_id] value = kwargs[col_id]
if isinstance(value, lookup.CONTAINS): if isinstance(value, lookup._Contains):
value = value.value value = value.value
# While users should use CONTAINS on lookup values, # While users should use CONTAINS on lookup values,
# the marker is moved to col_id so that the LookupMapColumn knows how to # the marker is moved to col_id so that the LookupMapColumn knows how to
# update its index correctly for that column. # update its index correctly for that column.
col_id = lookup.CONTAINS(col_id) col_id = lookup._Contains(col_id)
key.append(value) key.append(value)
col_ids.append(col_id) col_ids.append(col_id)
col_ids = tuple(col_ids) col_ids = tuple(col_ids)
@ -455,7 +455,7 @@ class Table(object):
c = lookup.extract_column_id(c) c = lookup.extract_column_id(c)
if not self.has_column(c): if not self.has_column(c):
raise KeyError("Table %s has no column %s" % (self.table_id, c)) raise KeyError("Table %s has no column %s" % (self.table_id, c))
if any(isinstance(col_id, lookup.CONTAINS) for col_id in col_ids_tuple): if any(isinstance(col_id, lookup._Contains) for col_id in col_ids_tuple):
column_class = lookup.ContainsLookupMapColumn column_class = lookup.ContainsLookupMapColumn
else: else:
column_class = lookup.SimpleLookupMapColumn column_class = lookup.SimpleLookupMapColumn
@ -481,7 +481,7 @@ class Table(object):
# _summary_source_table._summary_simple determines whether # _summary_source_table._summary_simple determines whether
# the column named self._summary_helper_col_id is a single reference # the column named self._summary_helper_col_id is a single reference
# or a reference list. # or a reference list.
lookup_value = rec if self._summary_simple else lookup.CONTAINS(rec) lookup_value = rec if self._summary_simple else lookup._Contains(rec)
return self._summary_source_table.lookup_records(**{ return self._summary_source_table.lookup_records(**{
self._summary_helper_col_id: lookup_value self._summary_helper_col_id: lookup_value
}) })

View File

@ -142,10 +142,10 @@ class TestSummaryChoiceList(EngineTestCase):
{k: type(v) for k, v in self.engine.tables["Source"]._special_cols.items()}, {k: type(v) for k, v in self.engine.tables["Source"]._special_cols.items()},
{ {
'#summary#GristSummary_6_Source': column.ReferenceListColumn, '#summary#GristSummary_6_Source': column.ReferenceListColumn,
"#lookup#CONTAINS(value='#summary#GristSummary_6_Source')": "#lookup#_Contains(value='#summary#GristSummary_6_Source')":
lookup.ContainsLookupMapColumn, lookup.ContainsLookupMapColumn,
'#summary#GristSummary_6_Source2': column.ReferenceListColumn, '#summary#GristSummary_6_Source2': column.ReferenceListColumn,
"#lookup#CONTAINS(value='#summary#GristSummary_6_Source2')": "#lookup#_Contains(value='#summary#GristSummary_6_Source2')":
lookup.ContainsLookupMapColumn, lookup.ContainsLookupMapColumn,
# simple summary and lookup # simple summary and lookup
@ -153,7 +153,7 @@ class TestSummaryChoiceList(EngineTestCase):
'#lookup##summary#GristSummary_6_Source3': lookup.SimpleLookupMapColumn, '#lookup##summary#GristSummary_6_Source3': lookup.SimpleLookupMapColumn,
'#summary#GristSummary_6_Source4': column.ReferenceListColumn, '#summary#GristSummary_6_Source4': column.ReferenceListColumn,
"#lookup#CONTAINS(value='#summary#GristSummary_6_Source4')": "#lookup#_Contains(value='#summary#GristSummary_6_Source4')":
lookup.ContainsLookupMapColumn, lookup.ContainsLookupMapColumn,
} }
) )