Skip to content

Commit b899df3

Browse files
authored
Merge pull request #18 from beda-software/issue-6-improved
Use underscore suffix instead of prefix for primitive #6
2 parents 642bc0b + 62b0485 commit b899df3

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

fhir_py_types/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class StructurePropertyType:
3333
isarray: bool = False
3434
literal: bool = False
3535
target_profile: list[str] | None = None
36+
alias: str | None = None
3637

3738

3839
@dataclass(frozen=True)

fhir_py_types/ast.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def make_default_initializer(
5151
) -> ast.expr | None:
5252
default: ast.expr | None = None
5353

54-
if keyword.iskeyword(identifier):
54+
if keyword.iskeyword(identifier) or type_.alias:
5555
default = ast.Call(
5656
ast.Name("Field"),
5757
args=[],
@@ -61,7 +61,7 @@ def make_default_initializer(
6161
if not type_.required
6262
else []
6363
),
64-
ast.keyword(arg="alias", value=ast.Constant(identifier)),
64+
ast.keyword(arg="alias", value=ast.Constant(type_.alias or identifier)),
6565
],
6666
)
6767
else:
@@ -115,19 +115,20 @@ def zip_identifier_type(
115115
result = []
116116

117117
for t in [remap_type(definition, t) for t in definition.type]:
118-
result.append((format_identifier(definition, identifier, t), t))
119-
if (
120-
definition.kind != StructureDefinitionKind.PRIMITIVE
121-
and is_primitive_type(t)
118+
name = format_identifier(definition, identifier, t)
119+
result.append((name, t))
120+
if definition.kind != StructureDefinitionKind.PRIMITIVE and is_primitive_type(
121+
t
122122
):
123123
result.append(
124124
(
125-
f"_{format_identifier(definition, identifier, t)}",
125+
f"{name}__ext",
126126
StructurePropertyType(
127127
code="Element",
128128
target_profile=[],
129129
required=False,
130130
isarray=definition.type[0].isarray,
131+
alias=f"_{name}"
131132
),
132133
)
133134
)

fhir_py_types/cli.py

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def main() -> None:
4040
load_from_bundle(bundle) for bundle in args.from_bundles
4141
)
4242
)
43-
4443
with open(os.path.join(dir_path, "header.py.tpl")) as header_file:
4544
header_lines = header_file.readlines()
4645

fhir_py_types/header.py.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from typing import List as List_, Optional as Optional_, Literal as Literal_, An
33
from pydantic import BaseModel as BaseModel_, Field, Extra
44

55

6+
67
class BaseModel(BaseModel_):
78
class Config:
89
extra = Extra.forbid

tests/test_ast.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ def assert_eq(
2020
assert generated == expected
2121

2222

23+
def build_field_with_alias(identifier: str) -> ast.Call:
24+
return ast.Call(
25+
func=ast.Name(id="Field"),
26+
args=[],
27+
keywords=[
28+
ast.keyword(arg="default", value=ast.Constant(value=None)),
29+
ast.keyword(arg="alias", value=ast.Constant(value=identifier)),
30+
],
31+
)
32+
33+
2334
def test_generates_empty_ast_from_empty_definitions() -> None:
2435
assert build_ast([]) == []
2536

@@ -64,12 +75,12 @@ def test_generates_class_for_flat_definition() -> None:
6475
),
6576
ast.Expr(value=ast.Constant(value="test resource property 1")),
6677
ast.AnnAssign(
67-
target=ast.Name(id="_property1"),
78+
target=ast.Name(id="property1__ext"),
6879
annotation=ast.Subscript(
6980
value=ast.Name(id="Optional_"),
7081
slice=ast.Constant("Element"),
7182
),
72-
value=ast.Constant(None),
83+
value=build_field_with_alias("_property1"),
7384
simple=1,
7485
),
7586
ast.Expr(value=ast.Constant(value="test resource property 1")),
@@ -176,13 +187,13 @@ def test_generates_multiple_classes_for_compound_definition() -> None:
176187
value=ast.Constant(value="nested test resource property 1")
177188
),
178189
ast.AnnAssign(
179-
target=ast.Name(id="_property1"),
190+
target=ast.Name(id="property1__ext"),
180191
annotation=ast.Subscript(
181192
value=ast.Name(id="Optional_"),
182193
slice=ast.Constant("Element"),
183194
),
184195
simple=1,
185-
value=ast.Constant(None),
196+
value=build_field_with_alias("_property1"),
186197
),
187198
ast.Expr(
188199
value=ast.Constant(value="nested test resource property 1")
@@ -329,7 +340,7 @@ def test_generates_annotations_according_to_structure_type(
329340
),
330341
ast.Expr(value=ast.Constant(value="test resource property 1")),
331342
ast.AnnAssign(
332-
target=ast.Name(id="_property1"),
343+
target=ast.Name(id="property1__ext"),
333344
annotation=ast.Subscript(
334345
value=ast.Name(id="Optional_"),
335346
slice=ast.Subscript(
@@ -343,7 +354,7 @@ def test_generates_annotations_according_to_structure_type(
343354
slice=ast.Constant("Element"),
344355
),
345356
simple=1,
346-
value=ast.Constant(None),
357+
value=build_field_with_alias("_property1"),
347358
),
348359
ast.Expr(value=ast.Constant(value="test resource property 1")),
349360
],
@@ -418,13 +429,13 @@ def test_unrolls_required_polymorphic_into_class_union() -> None:
418429
),
419430
ast.Expr(value=ast.Constant(value="monotype property definition")),
420431
ast.AnnAssign(
421-
target=ast.Name(id="_monotype"),
432+
target=ast.Name(id="monotype__ext"),
422433
annotation=ast.Subscript(
423434
value=ast.Name(id="Optional_"),
424435
slice=ast.Constant("Element"),
425436
),
426437
simple=1,
427-
value=ast.Constant(None),
438+
value=build_field_with_alias("_monotype"),
428439
),
429440
ast.Expr(value=ast.Constant(value="monotype property definition")),
430441
ast.AnnAssign(
@@ -440,13 +451,13 @@ def test_unrolls_required_polymorphic_into_class_union() -> None:
440451
value=ast.Constant(value="polymorphic property definition")
441452
),
442453
ast.AnnAssign(
443-
target=ast.Name(id="_valueBoolean"),
454+
target=ast.Name(id="valueBoolean__ext"),
444455
annotation=ast.Subscript(
445456
value=ast.Name(id="Optional_"),
446457
slice=ast.Constant("Element"),
447458
),
448459
simple=1,
449-
value=ast.Constant(None),
460+
value=build_field_with_alias("_valueBoolean"),
450461
),
451462
ast.Expr(
452463
value=ast.Constant(value="polymorphic property definition")

0 commit comments

Comments
 (0)