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
Showing only changes of commit 3273fd84bf - Show all commits

View File

@@ -1,38 +1,49 @@
from jambo.parser._type_parser import GenericTypeParser
import gc
from contextlib import contextmanager
from unittest import TestCase
@contextmanager
def with_test_parser():
class InvalidGenericTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "type:invalid"
def from_properties(
self, name: str, properties: dict[str, any], required: bool = False
): ...
try:
yield InvalidGenericTypeParser
finally:
del InvalidGenericTypeParser
gc.collect()
class TestGenericTypeParser(TestCase):
def setUp(self):
class InvalidGenericTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "type:invalid"
def from_properties(
self, name: str, properties: dict[str, any], required: bool = False
): ...
if hasattr(self, "InvalidGenericTypeParser"):
delattr(self, "InvalidGenericTypeParser")
self.InvalidGenericTypeParser = InvalidGenericTypeParser
def tearDown(self):
del self.InvalidGenericTypeParser
def test_invalid_get_impl(self):
# Assuming GenericTypeParser is imported from the module
with self.assertRaises(ValueError):
with (
with_test_parser(),
self.assertRaises(ValueError),
):
GenericTypeParser._get_impl({"type": "another_invalid_type"})
def test_invalid_json_schema_type(self):
self.InvalidGenericTypeParser.json_schema_type = None
# This is more for the developer's sanity check
with self.assertRaises(RuntimeError):
with (
with_test_parser() as InvalidGenericTypeParser,
self.assertRaises(RuntimeError),
):
InvalidGenericTypeParser.json_schema_type = None
GenericTypeParser._get_impl({"type": "another_invalid_type"})
def test_invalid_mappings_properties_builder(self):
parser = self.InvalidGenericTypeParser()
with self.assertRaises(NotImplementedError):
with (
with_test_parser() as InvalidGenericTypeParser,
self.assertRaises(NotImplementedError),
):
parser = InvalidGenericTypeParser()
parser.mappings_properties_builder({}, required=False)