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

Remove Optional typing for arrays. #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 18 additions & 17 deletions fhir_py_types/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,34 @@ 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):
default: ast.expr | None = None

default_value = ast.Constant(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 type_.required:
default_value = ast.Ellipsis()
elif type_.literal:
default_value = ast.Str(type_.code)
else:
default_value = ast.Constant(None)
keywords.append(ast.keyword(arg="default", value=default_value))
if keyword.iskeyword(identifier):
default = ast.Call(
keywords.append(
ast.keyword(arg="alias", value=ast.Constant(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(identifier)),
],
keywords=keywords,
)
else:
if not type_.required:
default = ast.Constant(None)
elif type_.literal and not type_.isarray:
default = ast.Str(type_.code)

return default

Expand Down