Replace sed post-processing in 2pydantic with gen-pydantic template override#410
Merged
Merged
Conversation
… override
The `2pydantic` hatch script previously stripped LinkML's namespace-prefix
munging from enum member names with `sed -E 's,[a-z]+COLON,,g'` over the
whole generated file. Replace that with an `enum.py.jinja` override passed
via `gen-pydantic --template-dir`, so the substitution happens at the
exact place the labels are emitted (using `pv.label.split("COLON") | last`)
rather than as blind text replacement after the fact.
Verified byte-identical output against the previous `sed`-based pipeline
on `dandischema/models.yaml` from `linkml-auto-converted` (all 116 `COLON`
occurrences are preceded by a lowercase prefix, so `split("COLON") | last`
is equivalent to the `sed` substitution on this schema).
Co-Authored-By: Claude Code 2.1.141 / Claude Opus claude-opus-4-7 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## linkml-conversion #410 +/- ##
=====================================================
+ Coverage 96.67% 97.83% +1.16%
=====================================================
Files 20 19 -1
Lines 2436 2407 -29
=====================================================
Hits 2355 2355
+ Misses 81 52 -29
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
sed -E 's,[a-z]+COLON,,g'post-processing in the2pydantichatch script with anenum.py.jinjaoverride passed togen-pydanticvia--template-dir.pv.label.split("COLON") | last), rather than as blind text replacement over the whole generated file.sedfrom the Pydantic generation pipeline, which makes the "Validate the data instances against the Pydantic models generated from the LinkML schema" task in Add LinkML behavior tests for required: False -> True slot_usage refinement #408 easier to implement (e.g. generating the Pydantic models in-process or in environments where shelling out tosedis awkward).Why
The old pipeline was:
gen-pydanticemits enum members whose Python identifiers carry LinkML's namespace-prefix munging — e.g.dandiCOLONOpenAccess = "dandi:OpenAccess". Thesedstripped the<prefix>COLONportion file-wide.Doing this at the template level is more precise:
sed, which is useful for Add LinkML behavior tests for required: False -> True slot_usage refinement #408 (validating data instances against the generated Pydantic models) where we'd rather not have the generation step shell out.Verification
Generated
models_linkml.pytwo ways fromdandischema/models.yaml(restored from thelinkml-auto-convertedbranch):gen-pydantic --black … | sed -E 's,[a-z]+COLON,,g'gen-pydantic --black --template-dir tools/linkml_conversion_tools/pydantic_templates …diffof the two outputs is empty — byte-identical. All 116COLONoccurrences in the rawgen-pydanticoutput are preceded by a lowercase prefix, sosplit("COLON") | lastis equivalent to thesedon this schema.Test plan
dandischema/models.yamlfromlinkml-auto-converted— byte-identical.linkml-auto-convertedbranch (or one stacked on top), runhatch run linkml-auto-converted:2pydanticand confirmdandischema/models_linkml.pyis unchanged from before this PR.