Better Object Internal Structure and Type Selection #16

Merged
HideyoshiNakazone merged 10 commits from improvement/better-internal-structure into main 2025-06-04 04:27:29 +00:00
2 changed files with 14 additions and 35 deletions
Showing only changes of commit dbbb8e0419 - Show all commits

View File

@@ -30,7 +30,6 @@ class GenericTypeParser(ABC, Generic[T]):
:param kwargs: Additional options for type parsing. :param kwargs: Additional options for type parsing.
:return: A tuple containing the type and its properties. :return: A tuple containing the type and its properties.
""" """
pass
def from_properties( def from_properties(
self, name: str, properties: dict[str, Any], **kwargs: Unpack[TypeParserOptions] self, name: str, properties: dict[str, Any], **kwargs: Unpack[TypeParserOptions]

View File

@@ -1,41 +1,21 @@
from jambo.parser import StringTypeParser
from jambo.parser._type_parser import GenericTypeParser from jambo.parser._type_parser import GenericTypeParser
import gc
from contextlib import contextmanager
from unittest import TestCase from unittest import TestCase
@contextmanager
def with_test_parser():
class InvalidGenericTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "type:invalid"
def from_properties_impl(
self, name: str, properties: dict[str, any], required: bool = False
): ...
try:
yield InvalidGenericTypeParser
finally:
del InvalidGenericTypeParser
gc.collect()
class TestGenericTypeParser(TestCase): class TestGenericTypeParser(TestCase):
def test_invalid_get_impl(self): def test_get_impl(self):
# Assuming GenericTypeParser is imported from the module parser = GenericTypeParser._get_impl({"type": "string"})
with (
with_test_parser(),
self.assertRaises(ValueError),
):
GenericTypeParser._get_impl({"type": "another_invalid_type"})
def test_invalid_json_schema_type(self): self.assertIsInstance(parser(), StringTypeParser)
# This is more for the developer's sanity check
with ( def test_get_impl_invalid_json_schema(self):
with_test_parser() as InvalidGenericTypeParser, with self.assertRaises(RuntimeError):
self.assertRaises(RuntimeError), StringTypeParser.json_schema_type = None
): GenericTypeParser._get_impl({"type": "string"})
InvalidGenericTypeParser.json_schema_type = None StringTypeParser.json_schema_type = "type:string"
GenericTypeParser._get_impl({"type": "another_invalid_type"})
def test_get_impl_invalid_type(self):
with self.assertRaises(ValueError):
GenericTypeParser._get_impl({"type": "invalid_type"})