Skip to content

PY: type-check fixes for python-us-core example (mypy clean) - #180

Merged
ryukzak merged 7 commits into
mainfrom
py-mypy-example-fixes
Jun 18, 2026
Merged

PY: type-check fixes for python-us-core example (mypy clean)#180
ryukzak merged 7 commits into
mainfrom
py-mypy-example-fixes

Conversation

@ryukzak

@ryukzak ryukzak commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Makes mypy . on examples/python-us-core clean (0 errors). The bulk of prior noise came from a broken local mypy.ini; the genuine 18 errors are fixed here.

Slice getter raw-mode overloads

The Python profile generator emitted typed @overloads for extension getters but not for slice getters — plain slice getters declared a single flat return type yet returned the raw element object for mode="raw", so accessing raw attributes failed type checking.

Slice getters now mirror the extension pattern (Literal["raw"] → element type), and the slice element type / Literal/overload/Any imports are threaded through.

Motivation: get_systolic("raw").value_quantity no longer fails [attr-defined].

# before
def get_systolic(self, mode: str | None = None) -> Quantity | None: ...

# after
@overload
def get_systolic(self) -> Quantity | None: ...
@overload
def get_systolic(self, mode: Literal["raw"]) -> ObservationComponent | None: ...
def get_systolic(self, mode: Literal["raw"] | None = None) -> Quantity | ObservationComponent | None: ...
  • Also fixes the race/ethnicity/tribal extension sub-extension getters, which had the same latent gap.
  • elementTypeId is now imported explicitly for plain slices (previously compiled only because accessors happened to import the same type).

Example test cleanups

  • Drop stale # type: ignore[call-arg] on raw slice getters (now type-check via overloads; the ignores never matched the real attr-defined error).
  • Typed-bundle test: pass BundleEntry(resource=...) instead of dict literals — type-discriminated slices use the typed base directly, with no flat-dict form — and read entry.resource directly instead of the dead dict fallback.
  • Patient equivalence test: bind identifier/name as locals and pass them directly instead of an untyped dict(...) splat (which erased value types to object).

Regenerated

  • Regenerated fhir_types/ for the example.

ryukzak added 7 commits June 18, 2026 14:56
Non-type-discriminated slice getters declared a single flat return type but
returned the raw element object for mode="raw", so accessing raw attributes
failed type checking. Emit @overload signatures (mirroring extension getters):
default -> flat type, Literal["raw"] -> element type. Import the slice element
type and Literal/overload/Any accordingly.
The raw-mode slice getters now type-check via overloads, so the
# type: ignore[call-arg] comments (which never matched the real attr-defined
error anyway) are no longer needed.
Type-discriminated slices use the typed base type directly (no flat-dict form),
so passing dict literals to set_patient_entry mismatched the signature. Build
BundleEntry(resource=...) like the other cases, and read entry.resource
directly instead of the dead dict fallback.
Building args via dict(...) erased per-key value types to object, so splatting
into create()/create_resource() and indexing into set_identifier()/set_name()
mismatched the typed signatures. Bind identifier/name as locals and pass them
directly.
mypy only scanned generated code, so type errors in the hand-written example
tests went unnoticed. Widen the scope to the whole example dir, matching the
other python example targets (which run mypy over '.').
@ryukzak
ryukzak force-pushed the py-mypy-example-fixes branch from b56d8c0 to 2f4e4f4 Compare June 18, 2026 13:05
@ryukzak
ryukzak merged commit 36e662a into main Jun 18, 2026
35 checks passed
@ryukzak
ryukzak deleted the py-mypy-example-fixes branch June 18, 2026 13:10
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.

2 participants