Generate precise catalog type stubs#109
Merged
Merged
Conversation
Collaborator
|
This seems really promising. However, when I install cfa-dataops into a virtual environment, it doesn't pick up the dataops_catalog_stubs command |
Contributor
Author
|
@ryanraaschCDC did you do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
100% vibecoded alternative to #98.
I'm not super comfortable adding this to the repo, but it does work as advertised
:
Codex Explanation
Summary
This PR replaces the broad inline
CatalogNamespacetype-hint workaround with generated.pyistubs for the installed catalog tree.The original approach improved editor inference for expressions like:
but it did so by declaring endpoint attributes such as
load,extract, anddataon every catalog namespace. That made invalid paths look valid to static analyzers.This PR adds a
dataops_catalog_stubscommand that inspects the installed catalog maps and dataset TOML configs, then writes project-local stubs under:The generated stubs model:
datacatnamespace pathsreportcatnamespace pathsDatasetEndpointload,extract,data, andstage_*BlobEndpoint.get_dataframe()overloads, preserving inference such asoutput="lazy" -> polars.LazyFrameWhy
cfa-dataopsdiscovers catalogs dynamically from installed packages such asCDCgov/cfa-catalog-pub, so inline annotations incatalog.pycannot accurately describe the real installed namespace tree.Generated stubs are a better fit because they are created from the same installed catalog state used at runtime. For
cfa-catalog-pub, this means paths like:can be represented precisely, while non-terminal namespaces like:
do not falsely appear to have blob endpoint methods.
Maintainability
The first generated-stub draft built the
.pyioutput with a large hand-assembled string, which was too fragile. This version hardens the implementation by:.pyisyntax into Mako templatestype_stubs.pyfocused on catalog-tree/model constructionast.parse()in testsUser Workflow
After installing the relevant catalog packages, run:
Pyright/Pylance can use the generated
typings/directory directly. For mypy, use:Regenerate the stubs whenever dataset TOMLs, report notebooks, or catalog paths change.
Validation
uv run ruff check cfa/dataops/type_stubs.py tests/test_type_stubs.py cfa/dataops/stub_templatesuv run pytest tests/test_type_stubs.py -quv run dataops_catalog_stubs --output-root /private/tmp/cfa-dataops-typings-template-smokeuv build --out-dir /private/tmp/cfa-dataops-build-template-smokecfa/dataops/py.typedand both Mako templates.