Introspection/Type Hinting with Row Objects

It would be extremely helpful if there was more ability for introspection with Row objects.

For example, we often want to verify that a row is a member of a table:

tables = (app_tables.table_1, app_tables.table_2, app_tables.table_3)

# current implementation
has_row = any((table.has_row(row) for table in tables))

# Desired implementation
has_row = row.get_table() in tables

The second is more readable. I also assume that has_row requires a trip to the database (could be wrong), so it would also be more efficient.

It would also be helpful to be able to type-check Row objects. Right now the best workaround is to use anvil.LiveObjectProxy client-side and anvil._server.LiveObjectProxy server-side, but we can’t then differentiate between row objects and search iterators (or other proxies), without using hasattr as well.

row = app_tables.table_1.get()

# Current implementation
isinstance(row, app_tables.Row) # resolves to False

# Desired implementation
isinstance(row, app_tables.Row) # resovles to True, client-side or server-side

Finally it would be helpful to be able to type-hint row objects specific to a table:

# current implementation
def get_user(email: str): -> anvil.tables.Row
    return app_tables.users.get_user(email=email)

# desired implementation (probably not exact but something like this)
def get_user(email: str): -> app_tables.users.Row
    return app_tables.users.get_user(email=email)

Related: Working with Table and Row objects?

2 Likes

Some of these are partially solved in accelerated tables


You might find this post handy: Get_by_row_id()
But I guess take it with a pinch of salt while accelerated tables are still in beta and the API isn’t documented


That should just work with accelerated tables
worth noting that a Row in accelerated tables has nothing to do with LiveObjectProxy anymore

>>> from anvil.tables import app_tables, Row
>>> r = app_tables.table_1.search()[0]
>>> isinstance(r, Row)
# True

We’ve just implemented partial support type annotations in the auto completer suggestions :trophy:

I’ll add the Row type hinting implementation to our list.

2 Likes

Wow, wonderful! As you can probably guess I haven’t dove into accelerated tables yet. Seems like I ought to…

Thanks.