Skip to content

PY: reduce Any in profile_helpers.py#194

Merged
ryukzak merged 2 commits into
mainfrom
py-profile-helpers-reduce-any
Jul 10, 2026
Merged

PY: reduce Any in profile_helpers.py#194
ryukzak merged 2 commits into
mainfrom
py-profile-helpers-reduce-any

Conversation

@ryukzak

@ryukzak ryukzak commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Closes #177.

Investigated whether the ~40 Any in assets/api/writer-generator/python/profile_helpers.py could be meaningfully reduced. The count is dominated by intrinsic dict[str, Any] / Mapping[str, Any] value positions on untyped FHIR JSON, which are legitimately Any. Generics don't help: slice-getter inputs arrive via getattr(self._resource, ...) (typed Any), so any TypeVar collapses to Any and call-site cast()s remain.

The sound, non-cosmetic change is tightening "arbitrary input value" parameters from Any to object (Python's unknown) — only where the body already narrows via is_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

  • Anyobject on 19 parameters that accept an arbitrary dict / Pydantic-model / primitive:
    • Core helpers: 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)
    • Validation helpers: _get_field and all validate_* res params, plus validate_fixed_value expected
  • Left as Any (cannot narrow soundly): push_extension / ensure_profile (assign attributes / call dict() directly on the param), all dict[str, Any] value positions (intrinsic FHIR data), and -> Any return types (tightening would just move casts to call sites).
  • Regenerated the python-r4-us-core example (verbatim asset copy).

Before:

def matches_value(value: Any, match: Any) -> bool: ...
def validate_required(res: Any, profile_name: str, field: str) -> list[str]: ...

After:

def matches_value(value: object, match: object) -> bool: ...
def validate_required(res: object, profile_name: str, field: str) -> list[str]: ...

Verification

  • make all passes (exit 0)
  • Strict mypy on python-r4-us-core: no issues
  • All Python and generator-snapshot tests pass; no snapshot drift

@ryukzak ryukzak merged commit 01bb92c into main Jul 10, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PY: reduce Any in profile_helpers.py

1 participant