PY: reduce Any in profile_helpers.py#194
Merged
Merged
Conversation
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.
Closes #177.
Investigated whether the ~40
Anyinassets/api/writer-generator/python/profile_helpers.pycould be meaningfully reduced. The count is dominated by intrinsicdict[str, Any]/Mapping[str, Any]value positions on untyped FHIR JSON, which are legitimatelyAny. Generics don't help: slice-getter inputs arrive viagetattr(self._resource, ...)(typedAny), so anyTypeVarcollapses toAnyand call-sitecast()s remain.The sound, non-cosmetic change is tightening "arbitrary input value" parameters from
Anytoobject(Python'sunknown) — only where the body already narrows viais_record/isinstance/hasattr, or just stores the value. This adds a mypy guardrail against un-narrowed access without any new casts or runtime change.Changes
Any→objecton 19 parameters that accept an arbitrary dict / Pydantic-model / primitive:is_record,_get_key,_model_get,matches_value(both params),is_extension,get_extension_value,extract_complex_extension,set_array_slice(value),set_array_slices(values)_get_fieldand allvalidate_*resparams, plusvalidate_fixed_valueexpectedAny(cannot narrow soundly):push_extension/ensure_profile(assign attributes / calldict()directly on the param), alldict[str, Any]value positions (intrinsic FHIR data), and-> Anyreturn types (tightening would just move casts to call sites).python-r4-us-coreexample (verbatim asset copy).Before:
After:
Verification
make allpasses (exit 0)mypyonpython-r4-us-core: no issues