Skip to content

PY: default fieldFormat to camelCase#189

Merged
ryukzak merged 4 commits into
mainfrom
python-camelcase-default
Jun 19, 2026
Merged

PY: default fieldFormat to camelCase#189
ryukzak merged 4 commits into
mainfrom
python-camelcase-default

Conversation

@ryukzak

@ryukzak ryukzak commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Default change

  • Python generator now defaults fieldFormat to camelCase (was snake_case). Fields keep their FHIR spelling instead of being converted.

Generated models before/after — using FHIR field names directly avoids the snake_case ↔ alias indirection:

# before (snake_case default)
birth_date: str | None = Field(None, alias="birthDate", serialization_alias="birthDate")
marital_status: CodeableConcept[...] | None = Field(None, alias="maritalStatus", ...)

# after (camelCase default)
birthDate: str | None = Field(None, alias="birthDate", serialization_alias="birthDate")
maritalStatus: CodeableConcept[...] | None = Field(None, alias="maritalStatus", ...)

Opt back into snake_case explicitly (unchanged behavior):

.python({ fieldFormat: "snake_case" })

Generator fixes

  • __pydantic_fields_set__.add(...) now uses the formatted resourceType field name instead of a hardcoded "resource_type" literal — for camelCase models the field is resourceType, so the old literal marked a non-existent field as set.
# before — wrong field name under camelCase
self.__pydantic_fields_set__.add("resource_type")
# after
self.__pydantic_fields_set__.add("resourceType")
  • Dropped the TYPE_CHECKING resourceType shim from the fhirpy base model. It existed so static type-checkers saw a resourceType attribute on snake_case models (where the field is resource_type). camelCase models expose resourceType natively, so the shim is unnecessary.

Example & tests

  • examples/python-r4-us-core: dropped explicit fieldFormat: "snake_case" (now uses the camelCase default) and migrated hand-written demos/tests to camelCase field access, kwargs, raw-extension dict keys, and validation-message assertions. mypy and the offline pytest suite pass.
  • examples/python-r4 keeps fieldFormat: "snake_case" to retain snake_case coverage at generation level.
  • Updated the python writer test assertion and regenerated affected snapshots.

Note

  • snake_case + fhirpy client no longer ships the static resourceType shim, so mypy against snake_case fhirpy models will report ResourceProtocol non-conformance. The default (camelCase) is unaffected.

ryukzak added 4 commits June 19, 2026 09:49
- Change Python generator default fieldFormat from snake_case to camelCase
- Use the formatted resourceType field name in __pydantic_fields_set__.add
  instead of a hardcoded snake_case literal
- Drop the TYPE_CHECKING resourceType shim from fhirpy base model; camelCase
  models expose resourceType natively, so the static alias workaround is
  no longer needed
- Update marital_status assertion to maritalStatus in python writer test
- Regenerate Python snapshots (camelCase field names, resourceType)
- Drop explicit fieldFormat: snake_case from us-core generate.ts (now uses
  the camelCase default)
- Update hand-written demos/tests to camelCase field access, kwargs, raw
  extension dict keys, and validation error message assertions
@ryukzak ryukzak requested a review from MikhailArtemyev June 19, 2026 07:54
@ryukzak ryukzak merged commit 2ecedd6 into main Jun 19, 2026
35 checks passed
@ryukzak ryukzak deleted the python-camelcase-default branch June 19, 2026 12:30
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