py: profiles - #167
Merged
Merged
Conversation
* added collectProhibitedChoiceValidation for prohibited value[x] variants * added resolveExtensionProfile, multi-form getter overloads, setter dispatch; raw branch normalizes dict -> Extension and attribute access works * push_extension preserves Pydantic instances as-is * all tests should pass
fixed base-import bug with BackboneElement
py: removed resource_families py: added a test for bundle profile class
…ated/typeDiscriminatorResource to SliceDef
…xt instead of 11 positional parameters
* Added nameCandidates: NameCandidates to SliceDef, populated from slice.nameCandidates in collectSliceDefs * Replaced resolveProfileMethodBaseNames with a simple loop reading snakeCase(recommended) from each extension/slice * Deleted resolveNameCollisions, pyExtensionMethodBaseName,pyQualifiedExtensionMethodBaseName, pySliceMethodBaseName, pyQualifiedSliceMethodBaseName from profile-naming.ts
… call to using pre-built SnapshotProfileTypeSchema snapshots from the index, making fields non-optional throughout
… FHIR package loading
…resolveExtensionProfile
…eliminate duplicated boilerplate across getter and setter variants
…ndant filter in collectSliceDefs, extracted sliceElementRetType helper
ryukzak
requested changes
Jun 3, 2026
…for constrained choice, dict | None for regular)
… prevent broken resources
ryukzak
requested changes
Jun 9, 2026
ryukzak
left a comment
Collaborator
There was a problem hiding this comment.
Please, rebase on the main.
Reconcile the Python generator refactor (profiles, writer dir-split, free-function helpers) with main's resource-generics work. Resolutions: - src/api/writer-generator/python/writer.ts: kept the branch's structure (it already contains the generics feature); main's parallel python.ts changes were stylistic (Map->Record, method vs free-function). - examples/python-r4 test_bundle.py / test_sdk.py: took main's versions (supersets, added from_json validation tests). - Generated example trees and python.test.ts snapshot: regenerated from the merged generator. Verified: typecheck, lint, 290 unit/api + 38 multi-package + 26 python-writer tests, 65 us-core + 10 r4 pytest all green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ryukzak
requested changes
Jun 11, 2026
py: type typed-bundle base generic param as Resource instead of Any
py: minor fix
ryukzak
approved these changes
Jun 12, 2026
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.
PY: Python profile class generation
Implements
generateProfile: truefor the Python generator — produces a typed wrapper class for each FHIR profile alongside the base Pydantic models.Runtime library
A static
profile_helpers.pyis copied into eachprofiles/package. It provides all slice/extension/validation utilities the generated classes depend on:build_resource,ensure_profile,apply_slice_match,get_array_slice,set_array_slice,push_extension,get_extension_value,extract_complex_extension,ensure_slice_defaults, and validation helpers.Profile class API
Each generated profile class exposes:
create(*, required_field: T, ...) -> Profile— constructs a valid resource with all fixed values, required slices, andmeta.profilepre-populatedapply(resource) -> Profile— wraps an existing resource (adds canonical URL tometa.profile)from_resource(resource) -> Profile— wraps and validates; raises on errorsto_resource() -> BaseType— unwraps back to the Pydantic modelvalidate() -> dict[str, list[str]]— checks required fields, fixed value constraints, slice cardinality, must-support fields (warnings), prohibited choices, enum values, and reference target typesField accessors
get_X/set_Xmethods; setters returnselffor fluent chainingvalue[x]) promoted to direct params when only one variant is constrainedExtension accessors
Each
ProfileExtensiongenerates overloadedget_X/set_Xmethods with three return modes:dict,str,Coding)"raw"— bareExtensionmodel"profile"— wrapped in the extension's own profile class (when one exists in the same package)Supports complex (sub-extension), single-value, and generic extension shapes.
Slice accessors
Each named slice generates
get_X/set_Xmethods that read and write into the correct position within the array field. Supports:max: *) variants withget_X() -> list[T]/set_X(values: list[T])component.value[x]fixed tovalueQuantity) — automatically wraps/unwraps the choice keyExample
Added
examples/python-profiles/us-core-demo/— Python equivalent of the TypeScript US Core demo:Before (raw Pydantic):
After (profile class):
load.pyconvertspatients.csv(5 patients) to a FHIR transaction bundle;avg.pyreads it back via profile accessors and prints average BP.