(core) Update attributes of Table.all when column is renamed

Summary: Adds an InferenceTip which treats `Table.all` similarly to `Table.lookupRecords(...)`, so that `Table.all.foo` is changed to `Table.all.bar` when the column `foo` is renamed to `bar`.

Test Plan: Extended test for the `lookupRecords` case.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Differential Revision: https://phab.getgrist.com/D3521
This commit is contained in:
Alex Hall 2022-07-11 21:18:36 +02:00
parent c0852761b9
commit 29fb3360b6
2 changed files with 30 additions and 7 deletions

View File

@ -247,6 +247,23 @@ class InferLookupReference(InferenceTip):
def infer(cls, node, context=None):
yield astroid.bases.Instance(infer(node.func.expr))
class InferAllReference(InferenceTip):
"""
Inference helper to treat the return value of `Table.all` as returning instances
of table `Table`.
"""
node_class = astroid.nodes.Attribute
@classmethod
def filter(cls, node):
return node.attrname == "all" and _is_table(infer(node.expr))
@classmethod
def infer(cls, node, context=None):
yield astroid.bases.Instance(infer(node.expr))
class InferLookupComprehension(InferenceTip):
node_class = astroid.nodes.AssignName
@ -255,9 +272,11 @@ class InferLookupComprehension(InferenceTip):
compr = node.parent
if not isinstance(compr, astroid.nodes.Comprehension):
return False
if not isinstance(compr.iter, astroid.nodes.Call):
return False
return InferLookupReference.filter(compr.iter)
if isinstance(compr.iter, astroid.nodes.Call):
return InferLookupReference.filter(compr.iter)
if isinstance(compr.iter, astroid.nodes.Attribute):
return InferAllReference.filter(compr.iter)
return False
@classmethod
def infer(cls, node, context=None):
@ -311,7 +330,7 @@ def parse_grist_names(builder):
code_text = builder.get_text()
with use_inferences(InferReferenceColumn, InferReferenceFormula, InferLookupReference,
InferLookupComprehension):
InferLookupComprehension, InferAllReference):
atok = asttokens.ASTTokens(code_text, tree=astroid.builder.parse(code_text))
def make_tuple(start, end, table_id, col_id):

View File

@ -213,6 +213,7 @@ class TestRenames(test_engine.EngineTestCase):
self.load_sample(self.sample)
self.add_column("Address", "people", formula="People.lookupOne(addr=$id, city=$city).name")
self.add_column("Address", "people2", formula="People.lookupRecords(addr=$id).name")
self.add_column("Address", "people3", formula="People.all.name")
out_actions = self.apply_user_action(["RenameColumn", "People", "name", "nombre"])
self.assertPartialOutActions(out_actions, { "stored": [
["RenameColumn", "People", "name", "nombre"],
@ -220,11 +221,14 @@ class TestRenames(test_engine.EngineTestCase):
"People.lookupOne(addr=$id, city=$city).nombre"}],
["ModifyColumn", "Address", "people2", {"formula":
"People.lookupRecords(addr=$id).nombre"}],
["BulkUpdateRecord", "_grist_Tables_column", [22, 25, 26], {
"colId": ["nombre", "people", "people2"],
["ModifyColumn", "Address", "people3", {"formula":
"People.all.nombre"}],
["BulkUpdateRecord", "_grist_Tables_column", [22, 25, 26, 27], {
"colId": ["nombre", "people", "people2", "people3"],
"formula": ["",
"People.lookupOne(addr=$id, city=$city).nombre",
"People.lookupRecords(addr=$id).nombre"]
"People.lookupRecords(addr=$id).nombre",
"People.all.nombre"]
}],
]})