Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/default empty list revised #21

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 17 additions & 18 deletions fhir_py_types/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,35 @@ def make_type_annotation(
if type_.isarray:
annotation = ast.Subscript(value=ast.Name("List_"), slice=annotation)

if not type_.required and form != AnnotationForm.TypeAlias:
if not type_.required and form != AnnotationForm.TypeAlias and not type_.isarray:
annotation = ast.Subscript(value=ast.Name("Optional_"), slice=annotation)

return annotation



def make_default_initializer(
identifier: str, type_: StructurePropertyType
) -> ast.expr | None:
default: ast.expr | None = None

keywords: list[ast.keyword] = []

if type_.isarray and not type_.required:
keywords.append(ast.keyword(arg="default_factory", value=ast.Name("list")))
else:
if not type_.required:
keywords.append(ast.keyword(arg="default", value=ast.Constant(None)))
elif type_.literal:
keywords.append(ast.keyword(arg="default", value=ast.Constant(type_.code)))
if keyword.iskeyword(identifier) or type_.alias:
default = ast.Call(
keywords.append(
ast.keyword(arg="alias", value=ast.Constant(type_.alias or identifier)),
)
default = ast.Call(
ast.Name("Field"),
args=[],
keywords=[
*(
[ast.keyword(arg="default", value=ast.Constant(None))]
if not type_.required
else []
),
ast.keyword(arg="alias", value=ast.Constant(type_.alias or identifier)),
],
keywords=keywords,
)
else:
if not type_.required:
default = ast.Constant(None)
elif type_.literal and not type_.isarray:
default = ast.Constant(type_.code)


return default


Expand Down
36 changes: 33 additions & 3 deletions fhir_py_types/header.py.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class BaseModel(BaseModel_):
exclude: IncEx = None,
context: Any_ | None = None,
by_alias: bool = True,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_unset: bool = True,
exclude_defaults: bool = True,
exclude_none: bool = True,
round_trip: bool = False,
warnings: bool | Literal_["none", "warn", "error"] = True,
serialize_as_any: bool = False,
):
# Override default parameters for by_alias and exclude_none preserving function declaration
# Override default parameters for by_alias and exclude_* preserving function declaration
return super().model_dump(
mode=mode,
include=include,
Expand All @@ -69,6 +69,36 @@ class BaseModel(BaseModel_):
serialize_as_any=serialize_as_any,
)

def model_dump_json(
self,
*,
indent: int | None = None,
include: IncEx | None = None,
exclude: IncEx | None = None,
context: Any_ | None = None,
by_alias: bool = True,
exclude_unset: bool = True,
exclude_defaults: bool = True,
exclude_none: bool = True,
round_trip: bool = False,
warnings: bool | Literal_['none', 'warn', 'error'] = True,
serialize_as_any: bool = False,
) -> str:
# Override default parameters for by_alias and exclude_* preserving function declaration
return super().model_dump_json(
indent=indent,
include=include,
exclude=exclude,
context=context,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
round_trip=round_trip,
warnings=warnings,
serialize_as_any=serialize_as_any,
)

@field_serializer("*")
@classmethod
def serialize_all_fields(cls, value: Any_, info: SerializationInfo):
Expand Down
Loading