Skip to content

refactor(coverage): build each dialect's obligation config instead of installing it - #532

Queued
kvlonge wants to merge 14 commits into
mainfrom
refactor/dialect-obligation-config
Queued

kvlonge wants to merge 14 commits into
mainfrom
refactor/dialect-obligation-config

Conversation

@kvlonge

@kvlonge kvlonge commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Why

Each dialect's obligation build used to install() its rules by setting names on the shared grammar-obligations modules, so one dialect's build could change another's output in the same process. #528 snapshotted and restored those modules around each build; Databricks and Snowflake still ran without it. This removes the shared state instead of guarding it.

Changes

  • A frozen DialectConfig holds what install() used to set: token, class rules, templates, list and fill rules, family styles, the walk and renderer classes, and the root readers. The grammar reader, walk, renderer, derive, obligations and cases code take it as config.
  • Each adapter returns its config (sqlite_config(), ..., bigquery_config(root)); the builders, cases modules and tests build it once and pass it down.
  • The fix(coverage): run each dialect's obligation build on the shared modules as imported #528 guard, its decorators and the BigQuery test fixture are removed. Databricks and Snowflake lose their strict xfail: all seven builds pass forward and in reverse in one process.
  • A static check flags any global and any set, delete, setattr, augmented or in-place change of a name imported from the shared package; a test runs it over scripts/ and tests/ (none found; on main it finds 75 across the seven adapters). The tables install() wrote are read-only mappings, so a write through an alias fails at run time.

Verification

Each builder's written obligations and cases, hashed on main a2e871d (with #529, #530 and #531) and on this branch merged with it, run with --check (0 stale):

dialect files sha256 (before = after)
BigQuery 99 6ad26ee7e2e9ddcd
ClickHouse 16 dbbb3c2fcc05c27a
Databricks 18 973b67ba82a1df8a
DuckDB 49 a40da2d40d66423d
PostgreSQL 55 665b419008991ae1
Snowflake 21 f48beffe87afa6ab
SQLite 64 35e6e076c8172bda

Targeted pytest over grammar_obligations and the seven syntax_obligations suites: 273 passed, no xfail. fensu check: 0 faults.

…a dialect config

Grammar parsing, Walk, Renderer, the derivation, the submissions, the named definitions and the
obligation and case builders take a DialectConfig instead of reading module globals a dialect
sets. shared_config() holds the shared defaults; Walk.operand_class and Renderer.reserved_word
give a dialect's subclass the one place it changes them.
…ng it

sqlite_config() returns the shared config with the SQLite tokens, operand classes, styles and root
readers, where install() set them on the shared modules.
…ng it

duckdb_config() returns the shared config with the DuckDB tokens, operand classes, styles and root
readers, where install() set them on the shared modules.
…stalling it

postgres_config() returns the shared config with the PostgreSQL tokens, operand classes, styles and root
readers, where install() set them on the shared modules.
…installing it

clickhouse_config() returns the shared config with the ClickHouse tokens, classes, cased names,
styles and readers. ClickHouseWalk classes operands as each form's page does and names a number
as the wrong fill for a name, for its own forms only, where install() swapped the shared walk's
globals around every token; ClickHouseRenderer accepts a keyword in place of a name.
…installing it

databricks_config() returns the shared config with the Databricks tokens, classes, styles and
readers. Its Walk and Renderer subclasses carry what install() patched onto the shared classes:
the page's operand classes, reserved-word specs, backticked identifiers, the reserved word an
alias takes (now set on the case's own renderer, not swapped on the module), and a dropped `(`
dropping its `)`.
…alling it

bigquery_config(root) returns the shared config with the BigQuery tokens, classes, styles and
readers, and the Withdrawals its suppression hook fills. A form's FORM_CLASS_RULES go first in a
config derived for that form alone, where install() swapped the shared class rules around it.
…stalling it

snowflake_config() returns the shared config with Snowflake's number token, cased words,
templates, repeated items, fill and bare-value classes, clause callees, mixed-case quoting, one
written option and widened integer rule, where install() set each on the shared modules.
Every builder, cases module and test that called a dialect's install() now builds that dialect's
config once and passes it to family_obligations, submissions, style_of and derive_trees.
…s the shared modules

With every dialect building its own config, nothing sets or changes the shared grammar-obligations
modules, so the snapshot-and-restore guard, its decorators and the BigQuery tests' fixture go.
Databricks and Snowflake leave strict xfail: all seven dialects build forward and in reverse in one
process with no stale file. A static check finds any global statement, and any set, delete,
setattr or in-place change of a name imported from the shared package, and a test runs it over
every module under scripts/ and tests/.
…pter styles in a loop

The grammar readers that take a config take every argument by name, DialectConfig's walk and
renderer types come from types.py, and each adapter builds its family styles in a loop instead of a
nested comprehension.

kvlonge commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Independent review at 35d35d4: the refactor is correct. Changes requested only on the scanner.

Behaviour

  • Every builder's --check at this head reports 0 stale files against its pin: BigQuery, ClickHouse, Databricks, DuckDB, PostgreSQL, SQLite and Snowflake (all families). The checks outside the builders also pass: build_syntax_signatures_bigquery --check reports 0 differences, and build_syntax_cases --check (Snowflake, with --held) reports 0 files differ.
  • test_shared_state.py passes: 15 tests, with every dialect built forward and in reverse in one process. It needs the SQLBABEL_EVIDENCE_ROOT_* variables that make sets.
  • No caller of install() or of the deleted guard remains in scripts/, tests/, recorder/, referee/ or tools/.

Config against install()
I ran your scanner over main's adapters and matched each of its 75 writes to a config field.

  • class_rules is (dialect, then shared) in all six adapters, as before.
  • Snowflake's widened integer rule is the same expression. BigQuery's per-form rules are (*scoped, *config.class_rules), the same as (*scoped, *general).
  • repeat_fills={**shared, **dialect} gives the same result as REPEAT_FILLS.update(dialect).
  • with_styles keeps the shared styles first, then the dialect's, as the old setdefault and update calls did.
  • shared_config() copies the dicts per call, and FormStyle is frozen, so one config can't write into another.
  • Byte-identical outputs confirm this for the current data.

Scanner (requested)
shared_writes misses realistic write paths. I checked each case:

  1. Augmented assignment to an imported name, such as REPEAT_FILLS |= {...} or X += [...]. This mutates the shared dict or list in place, but _written skips AugAssign with a Name target (dialect.py, the ast.AugAssign | ast.AnnAssign branch).
  2. Mutating methods missing from MUTATORS (constants.py:1943): popitem, discard, sort, reverse, __setitem__, __delitem__, and the set *_update methods.
  3. Tuple targets: constants.TOKEN, x = ... returns "" from _root.
  4. Writes through a local alias, such as styles = FAMILY_STYLES[f] followed by styles[k] = v.
  • Smallest fix for 1 to 3: count AugAssign on a shared Name, add those mutators, and unpack Tuple and List targets.
  • Item 4 can't be caught statically. Freezing the shared dicts in constants.py (REPEAT_FILLS, FAMILY_STYLES, LEXICAL_WRONG, LEXICALLY_DISTINCT_WRONG_FILL) with MappingProxyType would make any such write fail at run time, leaving the scan as a backstop.

Merge order: this conflicts with #530 in _helpers/render.py, classes/renderer.py and syntax_obligations_postgres/_helpers/adapt.py. Whichever lands second needs its builders re-checked across all seven dialects.

…s and freeze the tables install() wrote

The scan now counts an augmented assignment to an imported shared name, unpacks tuple, list and
starred targets, and knows popitem, discard, sort, reverse, the set update methods and the in-place
dunders. The family styles, repeat fills and both wrong-fill tables are read-only mappings, so a
write through an alias the scan cannot see fails when it runs.

kvlonge commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Fixes-only check at d477823: approved. The scanner findings are fixed.

  • Scanner. I re-ran my probes against shared_writes.
    • It now flags augmented assignment to an imported name (REPEAT_FILLS |= …), popitem, discard, __setitem__, tuple targets and for targets.
    • A local x += 2 is still not flagged.
    • It still misses a write through a local alias, as expected. That write now fails at run time instead: REPEAT_FILLS, LEXICAL_WRONG, LEXICALLY_DISTINCT_WRONG_FILL, FAMILY_STYLES and FAMILY_STYLES["query"] each raise TypeError on assignment.
    • The named style tables (such as QUERY_STYLES) are read only in constants.py, and FAMILY_STYLES holds copies of them, so a config can't see a write to one.
  • 3422770: limiting the report to ast.stmt | ast.expr drops nothing the scan reported, because every write path it checks is a statement or a call expression.
  • Behaviour:
    • All seven builders report 0 stale at this head.
    • test_shared_state.py passes its 16 fixture and frozen-table tests. I didn't rerun the 7 one-process forward and reverse builds; they passed at 35d35d4, and these commits only make shared state harder to write.
  • The merge with fix(postgres): give each repeated list item a distinct name and refuse an accept case that repeats one #530 waits for its own fixes-only check, which will re-check all seven builders.

@kvlonge
kvlonge added this pull request to the merge queue Dec 6, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant