extra_field() - Context fields documented by their Extra class

A Context dataclass field declared with extra_field() takes its
documentation from the description on the registered Extra of the same
name, validated against the class's extras_scope. This keeps doc
strings next to the resolve() code instead of duplicating them on the
dataclass, ahead of introducing TableContext and RowContext.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2026-06-11 06:51:54 -07:00
commit 63995ce823
2 changed files with 91 additions and 1 deletions

View file

@ -44,6 +44,51 @@ def test_context_dataclass_fields_all_have_help(klass):
)
def test_extra_field_documentation_comes_from_the_extra_class():
from datasette.views import extra_field
from datasette.views.table_extras import CountExtra
@dataclass
class DemoContext(Context):
extras_scope = ExtraScope.TABLE
count: int = extra_field()
name: str = field(metadata={"help": "The name"})
fields = {f.name: f for f in DemoContext.documented_fields()}
assert fields["count"].help == CountExtra.description
assert fields["count"].from_extra
assert fields["name"].help == "The name"
assert not fields["name"].from_extra
def test_extra_field_must_match_a_registered_extra():
from datasette.views import extra_field
@dataclass
class BadContext(Context):
extras_scope = ExtraScope.TABLE
not_a_real_extra: str = extra_field()
with pytest.raises(KeyError):
BadContext.documented_fields()
def test_extra_field_must_be_available_for_the_scope():
from datasette.views import extra_field
@dataclass
class WrongScopeContext(Context):
extras_scope = ExtraScope.ROW
# count is a TABLE-scope extra, not available for ROW
count: int = extra_field()
with pytest.raises(ValueError):
WrongScopeContext.documented_fields()
@pytest.fixture
def isolate_extra_template_vars_plugins():
# Datasette instances created with plugins_dir (e.g. the session-scoped