Feature request: typed query artifacts for SQL files
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
Research direction
Start by reading SQLFileLoader, ParameterDeclaration, the SQLGlot parsing surfaces, and available data dictionary metadata. Define a focused artifact scope and resolve the listed output, inference, annotation, and placement questions before implementation; done means an agreed, testable design rather than a broad wishlist.
Written by the indexing model from the issue text.
Description
Summary
Follow-up feature request for brainstorming typed artifacts generated from SQL files. This is a wishlist / design exploration item, not committed implementation scope for PR #497.
Background
PR #497 adds -- param: metadata, runtime validation, optional named parameters, and declared type resolution. The next possible step is generating type-checker and IDE-friendly artifacts from the SQL file loader metadata plus query shape.
Wishlist / Brainstorming Topics
- Generate typed query artifacts from existing SQLSpec surfaces, especially
SQLFileLoader,ParameterDeclaration, SQLGlot parsing, and data dictionary metadata where available. - Factor in query shape, not just declared params: selected columns, aliases, expressions,
select *, joins, casts, functions, and scalar/count-style queries. - Generate parameter artifacts such as
TypedDictclasses, keyword-only accessor signatures, and shared type aliases for JSON/UUID/Decimal/date/time values. - Generate result artifacts in one or more forms:
msgspec.Struct- Pydantic
BaseModel TypedDict- potentially dataclass/attrs if there is demand
- Generate typed runtime accessors, not only
.pyistubs..pyimay still be useful when runtime code stays dynamic, but should not be assumed to be the only output. - Centralize import generation so emitted files include only the imports actually needed by generated parameter/result types.
- Use explicit SQL annotations as an escape hatch for ambiguous or non-portable query shapes, for example expression result types, JSON paths, driver-specific functions, duplicate column names, and unresolved
select *projections. - Keep runtime validation separate from generated typing. Generated code should improve IDE/type-checker ergonomics without duplicating the runtime validation contract.
Possible Artifact Shape
A generator might emit files similar to:
generated/
params.py # param TypedDicts and aliases
models.py # result models, e.g. msgspec/Pydantic/TypedDict
queries.py # typed runtime accessors / optional typed execution helpers
queries.pyi # optional, if needed for dynamic runtime wrappers
__init__.py
Open Design Questions
- Which output families should be supported first:
TypedDict,msgspec, Pydantic, or configurable combinations? - Should typed execution helpers call driver methods directly, or should generation stop at typed
(SQL, params)accessors? - How should cardinality be declared or inferred: many, one, one-or-none, scalar, execute?
- What annotation syntax should be used for result hints when SQL/data dictionary inference is insufficient?
- How much should this rely on live database data dictionary introspection versus offline SQL-only inference?
- Where should generated files live, and should the generator be CLI-based, Python API-based, or both?
Non-goals to consider
- Do not build a separate SQL parser when existing SQLFileLoader / SQLGlot / data dictionary surfaces can be reused.
- Do not duplicate execute-time runtime validation.
- Do not infer full ORM-like schema models or relationships.
- Dominant language
- Python
- Stars
- 102
- Forks
- 9
- Avg merge
- 8h 38m
- Merged PRs (30d)
- 64
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from litestar-org/sqlspec
-
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
litestar-org/sqlspec#816 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
litestar-org/sqlspec#815 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 45/100
litestar-org/sqlspec#810 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
litestar-org/sqlspec#788 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
litestar-org/sqlspec#728 ·
Maintainers usually reply within 1 day
All issues in litestar-org/sqlspec
Similar issues
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
kristofdegrave/homeassistant-smart-charging#1413 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
nasa/earthdata-varinfo#113 ·
-
curriculum documentation quality
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
githubnext/gh-aw-workshop#3849 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day