Skip to content

Commit

Permalink
added python support for objects and optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
olynch committed Jan 5, 2024
1 parent b69954c commit 8587248
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/intertypes/python.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function topy(intertype::InterType; forward_ref=true)
Str => "str"
Sym => "str"
Binary => "str"
OptionalType(elemtype) => "$(topy(elemtype)) | None"
ObjectType(elemtype) => "dict[str, $(topy(elemtype))]"
List(elemtype) => "list[$(topy(elemtype))]"
Map(keytype, valuetype) => "OrderedDict[$(topy(keytype)), $(topy(valuetype))]"
Record(_) => error("no native record type for python")
Expand Down
17 changes: 14 additions & 3 deletions test/intertypes/InterTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,29 @@ optionals_schema = JSONSchema.Schema(read("optionals_schema.json", String))
generate_module(simpleast, PydanticTarget, dir)
generate_module(model, PydanticTarget, dir)
generate_module(wgraph, PydanticTarget, dir)
generate_module(objects, PydanticTarget, dir)
generate_module(optionals, PydanticTarget, dir)

pushfirst!(PyList(pyimport("sys")."path"), Py(dir))

pyast = pyimport("simpleast")
pymodel = pyimport("model")
pywgraph = pyimport("wgraph")
pyobjects = pyimport("objects")
pyoptionals = pyimport("optionals")
pyjson = pyimport("json")

py_m = pymodel.Model.model_validate_json(Py(jsonwrite(m)))
py_m_str = string(py_m.model_dump_json())
function python_roundtrip(pythontype, val)
py_val = pythontype.model_validate_json(Py(jsonwrite(val)))
py_val_str = string(py_val.model_dump_json())

@test jsonread(py_m_str, Model) == m
jsonread(py_val_str, typeof(val)) == val
end

@test python_roundtrip(pymodel.Model, m)
@test python_roundtrip(pyobjects.Metadata, md)
@test python_roundtrip(pyoptionals.NullableInt, x)
@test python_roundtrip(pyoptionals.NullableInt, y)

py_g = pywgraph.EDWeightedGraph.read_json(Py(jsonwrite(g)))
py_g_str = string(py_g.to_json_str())
Expand Down

0 comments on commit 8587248

Please sign in to comment.