Skip to content

Commit

Permalink
Make schema field ordering consistent.
Browse files Browse the repository at this point in the history
  • Loading branch information
norpadon committed Jun 28, 2024
1 parent d37c3d4 commit 681e8a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def foo(
"enum": ["x", "y"],
},
"f": {
"type": ["integer", "string"],
"type": ["string", "integer"],
},
},
"required": ["a", "b", "c"],
Expand Down
25 changes: 13 additions & 12 deletions wanga/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@
"ObjectNode",
"PrimitiveNode",
"SchemaNode",
"SchemaValidationError",
"SequenceNode",
"TupleNode",
"UndefinedNode",
"UnionNode",
"UnsupportedSchemaError",
]


JSON: TypeAlias = int | float | str | bool | None | dict[str, "JSON"] | list["JSON"]


type_to_jsonname: dict[type | None, LeafTypeName] = {
int: "integer",
_type_to_jsonname: dict[type | None, LeafTypeName] = {
None: "null",
bool: "boolean",
float: "number",
int: "integer",
str: "string",
bool: "boolean",
None: "null",
}


Expand Down Expand Up @@ -105,7 +107,7 @@ class PrimitiveNode(SchemaNode):

def json_schema(self, parent_hint: str | None = None) -> LeafJsonSchema:
result = LeafJsonSchema(
type=type_to_jsonname[self.primitive_type],
type=_type_to_jsonname[self.primitive_type],
)
if parent_hint:
result["description"] = parent_hint
Expand Down Expand Up @@ -160,7 +162,7 @@ class TupleNode(SchemaNode):
item_schemas: list[SchemaNode]

def json_schema(self, parent_hint: str | None = None) -> JsonSchema:
raise UndefinedSchemaError(
raise UnsupportedSchemaError(
"JSON schema cannot be generated for heterogeneous tuple types."
)

Expand Down Expand Up @@ -223,14 +225,13 @@ def json_schema(self, parent_hint: str | None = None) -> JsonSchema:
raise UnsupportedSchemaError(
"JSON schema cannot be generated for non-trivial Union types."
)
type_names = {
type_to_jsonname[option.primitive_type] # type: ignore
type_names = [
_type_to_jsonname[option.primitive_type] # type: ignore
for option in self.options
if option is not None
}
]
if "number" in type_names and "integer" in type_names:
type_names.remove("integer")
type_names = list(type_names)
if len(type_names) == 1:
type_names = type_names[0]
result = LeafJsonSchema(
Expand Down Expand Up @@ -314,8 +315,8 @@ class ObjectNode(SchemaNode):
Attributes:
constructor_fn: Callable that can be used to construct an object.
constructor_signature: Signature of the constructor. Used to properly dispatch
positional and keyword-only args during evaluation.
constructor_signature: Used to properly dispatch positional and
keyword-only args during evaluation.
name: Name of the object.
fields: The fields of the object.
hint: Hint extracted from the docstring.
Expand Down

0 comments on commit 681e8a7

Please sign in to comment.