Better Object Internal Structure and Type Selection #16
@@ -30,7 +30,6 @@ class GenericTypeParser(ABC, Generic[T]):
|
||||
:param kwargs: Additional options for type parsing.
|
||||
:return: A tuple containing the type and its properties.
|
||||
"""
|
||||
pass
|
||||
|
||||
def from_properties(
|
||||
self, name: str, properties: dict[str, Any], **kwargs: Unpack[TypeParserOptions]
|
||||
|
||||
@@ -1,41 +1,21 @@
|
||||
from jambo.parser import StringTypeParser
|
||||
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_impl(
|
||||
self, name: str, properties: dict[str, any], required: bool = False
|
||||
): ...
|
||||
|
||||
try:
|
||||
yield InvalidGenericTypeParser
|
||||
finally:
|
||||
del InvalidGenericTypeParser
|
||||
gc.collect()
|
||||
|
||||
|
||||
class TestGenericTypeParser(TestCase):
|
||||
def test_invalid_get_impl(self):
|
||||
# Assuming GenericTypeParser is imported from the module
|
||||
with (
|
||||
with_test_parser(),
|
||||
self.assertRaises(ValueError),
|
||||
):
|
||||
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
||||
def test_get_impl(self):
|
||||
parser = GenericTypeParser._get_impl({"type": "string"})
|
||||
|
||||
def test_invalid_json_schema_type(self):
|
||||
# This is more for the developer's sanity check
|
||||
with (
|
||||
with_test_parser() as InvalidGenericTypeParser,
|
||||
self.assertRaises(RuntimeError),
|
||||
):
|
||||
InvalidGenericTypeParser.json_schema_type = None
|
||||
GenericTypeParser._get_impl({"type": "another_invalid_type"})
|
||||
self.assertIsInstance(parser(), StringTypeParser)
|
||||
|
||||
def test_get_impl_invalid_json_schema(self):
|
||||
with self.assertRaises(RuntimeError):
|
||||
StringTypeParser.json_schema_type = None
|
||||
GenericTypeParser._get_impl({"type": "string"})
|
||||
StringTypeParser.json_schema_type = "type:string"
|
||||
|
||||
def test_get_impl_invalid_type(self):
|
||||
with self.assertRaises(ValueError):
|
||||
GenericTypeParser._get_impl({"type": "invalid_type"})
|
||||
Reference in New Issue
Block a user