Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion assets/api/writer-generator/python/fhirpy_base_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Union, Optional, Iterator, Tuple, Dict
from pydantic import BaseModel, Field
from pydantic_core import PydanticUndefined
from typing import Protocol


Expand All @@ -22,7 +23,10 @@ class FhirpyBaseModel(BaseModel):
def __pydantic_init_subclass__(cls, **kwargs: Any) -> None:
super().__pydantic_init_subclass__(**kwargs)
field = cls.model_fields.get("resource_type") or cls.model_fields.get("resourceType")
if field is not None and field.default is not None:
# Only concrete resources carry a default resourceType. Abstract/family base types
# (Resource, DomainResource) leave it unset, so we skip them to avoid registering a
# class attribute that concrete subclasses would shadow.
if field is not None and field.default is not None and field.default is not PydanticUndefined:
type.__setattr__(cls, "resourceType", str(field.default))

def __iter__(self) -> Iterator[Tuple[str, Any]]: # type: ignore[override]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Union, Optional, Iterator, Tuple, Dict
from pydantic import BaseModel, Field
from pydantic_core import PydanticUndefined
from typing import Protocol


Expand All @@ -21,7 +22,10 @@ class FhirpyBaseModel(BaseModel):
def __pydantic_init_subclass__(cls, **kwargs: Any) -> None:
super().__pydantic_init_subclass__(**kwargs)
field = cls.model_fields.get("resource_type") or cls.model_fields.get("resourceType")
if field is not None and field.default is not None:
# Only concrete resources carry a default resourceType. Abstract/family base types
# (Resource, DomainResource) leave it unset, so we skip them to avoid registering a
# class attribute that concrete subclasses would shadow.
if field is not None and field.default is not None and field.default is not PydanticUndefined:
type.__setattr__(cls, "resourceType", str(field.default))

def __iter__(self) -> Iterator[Tuple[str, Any]]: # type: ignore[override]
Expand Down
6 changes: 5 additions & 1 deletion examples/python-r4-us-core/fhir_types/fhirpy_base_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Union, Optional, Iterator, Tuple, Dict
from pydantic import BaseModel, Field
from pydantic_core import PydanticUndefined
from typing import Protocol


Expand All @@ -21,7 +22,10 @@ class FhirpyBaseModel(BaseModel):
def __pydantic_init_subclass__(cls, **kwargs: Any) -> None:
super().__pydantic_init_subclass__(**kwargs)
field = cls.model_fields.get("resource_type") or cls.model_fields.get("resourceType")
if field is not None and field.default is not None:
# Only concrete resources carry a default resourceType. Abstract/family base types
# (Resource, DomainResource) leave it unset, so we skip them to avoid registering a
# class attribute that concrete subclasses would shadow.
if field is not None and field.default is not None and field.default is not PydanticUndefined:
type.__setattr__(cls, "resourceType", str(field.default))

def __iter__(self) -> Iterator[Tuple[str, Any]]: # type: ignore[override]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
class DomainResource(Resource, Generic[T]):
model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="forbid")
resourceType: str = Field(
default='DomainResource',
alias='resourceType',
serialization_alias='resourceType',
pattern='DomainResource'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
class Resource(FhirpyBaseModel):
model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="forbid")
resourceType: str = Field(
default='Resource',
alias='resourceType',
serialization_alias='resourceType',
pattern='Resource'
Expand Down
10 changes: 7 additions & 3 deletions src/api/writer-generator/python/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,15 @@ export class Python extends Writer<PythonGeneratorOptions> {
}

private generateResourceTypeField(schema: SpecializationTypeSchema): void {
// Always type as `str`; the value is validated on the pydantic side via `pattern`.
// A `Literal[...]` here would shadow the parent's field and trigger Pydantic warnings.
// Type as `str`; the value is validated on the pydantic side via `pattern`.
const hasChildren = (schema.typeFamily?.resources?.length ?? 0) > 0;
this.line(`${this.nameFormatFunction("resourceType")}: str = Field(`);
this.indentBlock(() => {
this.line(`default='${schema.identifier.name}',`);
// Family/abstract base types (Resource, DomainResource) are subclassed by concrete
// resources and never instantiated directly. Omitting the default keeps `resourceType`
// out of the class namespace (see fhirpy_base_model.__pydantic_init_subclass__), so the
// concrete subclasses don't "shadow" it and trigger Pydantic UserWarnings.
if (!hasChildren) this.line(`default='${schema.identifier.name}',`);
this.line(`alias='resourceType',`);
this.line(`serialization_alias='resourceType',`);
if (!this.forFhirpyClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ T = TypeVar('T', bound=Resource, default=Resource)
class DomainResource(Resource, Generic[T]):
model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="forbid")
resourceType: str = Field(
default='DomainResource',
alias='resourceType',
serialization_alias='resourceType',
pattern='DomainResource'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ T = TypeVar('T', bound=Resource, default=Resource)
class DomainResource(Resource, Generic[T]):
model_config = ConfigDict(validate_by_name=True, serialize_by_alias=True, extra="forbid")
resourceType: str = Field(
default='DomainResource',
alias='resourceType',
serialization_alias='resourceType',
pattern='DomainResource'
Expand Down
Loading