Skip to content

PY: fix resourceType shadow warnings on family base types#191

Merged
ryukzak merged 3 commits into
mainfrom
py-resourcetype-shadow-warnings
Jun 26, 2026
Merged

PY: fix resourceType shadow warnings on family base types#191
ryukzak merged 3 commits into
mainfrom
py-resourcetype-shadow-warnings

Conversation

@ryukzak

@ryukzak ryukzak commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #190.

Importing the generated package emitted Pydantic UserWarning: Field name "resourceType" ... shadows an attribute in parent for every concrete resource. The fhirpy base model registers resourceType as a class attribute from the field default in __pydantic_init_subclass__; concrete resources subclassing the abstract family types (Resource, DomainResource) then "shadow" that attribute.

  • Omit the default on family/abstract types so no class attribute is registered. Their resourceType becomes a required str validated by pattern (these types are never instantiated directly).
  • Guard the fhirpy class-attribute registration against PydanticUndefined so required fields are skipped.
  • Update multi-package snapshots (sql-on-fhir, local-package) and regenerate the python-r4-us-core example.

Before (abstract base registered a class attribute that subclasses shadowed):

class Resource(FhirpyBaseModel):
    resourceType: str = Field(
        default='Resource',
        alias='resourceType',
        serialization_alias='resourceType',
        pattern='Resource'
    )

After (no default → no class attribute → no shadow warning):

class Resource(FhirpyBaseModel):
    resourceType: str = Field(
        alias='resourceType',
        serialization_alias='resourceType',
        pattern='Resource'
    )

Concrete resources still expose cls.resourceType for fhirpy, keep their default, and reject mismatched values.

ryukzak added 3 commits June 26, 2026 11:19
Concrete resources subclass the abstract family types (Resource,
DomainResource). The fhirpy base model registers resourceType as a class
attribute from the field default, which the subclasses then shadow,
producing Pydantic UserWarnings on import.

Omit the default on family/abstract types (their resourceType becomes a
required str validated by pattern) so no class attribute is registered,
and guard the fhirpy registration against PydanticUndefined.
@ryukzak ryukzak merged commit e87ff88 into main Jun 26, 2026
50 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.

1 participant