Feature/adds const #30

Merged
HideyoshiNakazone merged 3 commits from feature/adds-const into main 2025-06-23 18:32:37 +00:00
5 changed files with 87 additions and 12 deletions
Showing only changes of commit 65a81a8da5 - Show all commits

View File

@@ -3,6 +3,7 @@ from .allof_type_parser import AllOfTypeParser
from .anyof_type_parser import AnyOfTypeParser from .anyof_type_parser import AnyOfTypeParser
from .array_type_parser import ArrayTypeParser from .array_type_parser import ArrayTypeParser
from .boolean_type_parser import BooleanTypeParser from .boolean_type_parser import BooleanTypeParser
from .const_type_parser import ConstTypeParser
from .enum_type_parser import EnumTypeParser from .enum_type_parser import EnumTypeParser
from .float_type_parser import FloatTypeParser from .float_type_parser import FloatTypeParser
from .int_type_parser import IntTypeParser from .int_type_parser import IntTypeParser
@@ -14,6 +15,7 @@ from .string_type_parser import StringTypeParser
__all__ = [ __all__ = [
"GenericTypeParser", "GenericTypeParser",
"EnumTypeParser", "EnumTypeParser",
"ConstTypeParser",
"AllOfTypeParser", "AllOfTypeParser",
"AnyOfTypeParser", "AnyOfTypeParser",
"ArrayTypeParser", "ArrayTypeParser",

View File

@@ -0,0 +1,46 @@
from jambo.parser._type_parser import GenericTypeParser
from jambo.types.json_schema_type import JSONSchemaNativeTypes
from jambo.types.type_parser_options import TypeParserOptions
from pydantic import AfterValidator
from typing_extensions import Annotated, Any, Unpack
class ConstTypeParser(GenericTypeParser):
json_schema_type = "const"
def from_properties_impl(
self, name, properties, **kwargs: Unpack[TypeParserOptions]
):
if "const" not in properties:
raise ValueError(f"Const type {name} must have 'const' property defined.")
const_value = properties["const"]
if not isinstance(const_value, JSONSchemaNativeTypes):
raise ValueError(
f"Const type {name} must have 'const' value of allowed types: {JSONSchemaNativeTypes}."
)
const_type = self._build_const_type(const_value)
parsed_properties = {
"default": const_value,
"description": properties.get("description"),
}
return const_type, parsed_properties
def _build_const_type(self, const_value):
def _validate_const_value(value: Any) -> Any:
if value != const_value:
raise ValueError(
f"Value must be equal to the constant value: {const_value}"
)
return value
return Annotated[
type(const_value),
AfterValidator(
_validate_const_value
)
]

View File

@@ -1,4 +1,5 @@
from jambo.parser._type_parser import GenericTypeParser from jambo.parser._type_parser import GenericTypeParser
from jambo.types.json_schema_type import JSONSchemaNativeTypes
from jambo.types.type_parser_options import TypeParserOptions from jambo.types.type_parser_options import TypeParserOptions
from typing_extensions import Unpack from typing_extensions import Unpack
@@ -9,16 +10,6 @@ from enum import Enum
class EnumTypeParser(GenericTypeParser): class EnumTypeParser(GenericTypeParser):
json_schema_type = "enum" json_schema_type = "enum"
allowed_types: tuple[type] = (
str,
int,
float,
bool,
list,
set,
type(None),
)
def from_properties_impl( def from_properties_impl(
self, name, properties, **kwargs: Unpack[TypeParserOptions] self, name, properties, **kwargs: Unpack[TypeParserOptions]
): ):
@@ -31,10 +22,10 @@ class EnumTypeParser(GenericTypeParser):
raise ValueError(f"Enum type {name} must have 'enum' as a list of values.") raise ValueError(f"Enum type {name} must have 'enum' as a list of values.")
if any( if any(
not isinstance(value, self.allowed_types) for value in enum_values not isinstance(value, JSONSchemaNativeTypes) for value in enum_values
): ):
raise ValueError( raise ValueError(
f"Enum type {name} must have 'enum' values of allowed types: {self.allowed_types}." f"Enum type {name} must have 'enum' values of allowed types: {JSONSchemaNativeTypes}."
) )
# Create a new Enum type dynamically # Create a new Enum type dynamically

View File

@@ -1,11 +1,24 @@
from typing_extensions import Dict, List, Literal, TypedDict, Union from typing_extensions import Dict, List, Literal, TypedDict, Union
from types import NoneType
JSONSchemaType = Literal[ JSONSchemaType = Literal[
"string", "number", "integer", "boolean", "object", "array", "null" "string", "number", "integer", "boolean", "object", "array", "null"
] ]
JSONSchemaNativeTypes: tuple[type, ...] = (
str,
int,
float,
bool,
list,
set,
NoneType,
)
JSONType = Union[str, int, float, bool, None, Dict[str, "JSONType"], List["JSONType"]] JSONType = Union[str, int, float, bool, None, Dict[str, "JSONType"], List["JSONType"]]

View File

@@ -0,0 +1,23 @@
from typing_extensions import Annotated, get_args, get_origin
from webbrowser import get
from jambo.parser import ConstTypeParser
from unittest import TestCase
class TestConstTypeParser(TestCase):
def test_parse_const_type(self):
parser = ConstTypeParser()
expected_const_value = "United States of America"
properties = {
"const": expected_const_value
}
parsed_type, parsed_properties = parser.from_properties(
"country", properties
)
self.assertEqual(get_origin(parsed_type), Annotated)
self.assertIn(str, get_args(parsed_type))