Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion jsonversation/aio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ def __init__(self) -> None:
self._parsed_keys = []
self._value = {}

for key, type_hint in type(self).__annotations__.items():
# initialize keys for potential parent classes
for cls in self.__class__.mro()[1:-1]:
if cls.__name__ == "Object" or cls.__name__ == "StreamingObject":
break

self._initialize_attributes(cls.__annotations__)

self._initialize_attributes(type(self).__annotations__)

def _initialize_attributes(self, attributes: dict[str, Any]) -> None:
for key, type_hint in attributes.items():
self._keys.append(key)

# Handle List[T]
Expand Down
30 changes: 21 additions & 9 deletions jsonversation/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,42 @@


class StreamingObject[T]:
_on_complete_funcs: list[Callable[[T], None]]

def __init__(self) -> None:
self._on_complete_funcs = []

def update(self, value: T) -> None:
return None

def _complete(self) -> None: ...

def on_complete(self, func: Callable[[T], None]) -> None:
self._on_complete_funcs.append(func)


class Object(StreamingObject[dict[str, Any]]):
_keys: list[str]
_parsed_keys: list[str]
_on_complete_funcs: list[Callable[[dict[str, Any]], None]]
_value: dict[str, Any]

def __init__(self) -> None:
super().__init__()
self._keys = []
self._parsed_keys = []
self._value = {}
self._on_complete_funcs = []

for key, type_hint in type(self).__annotations__.items():
# initialize keys for potential parent classes
for cls in self.__class__.mro()[1:-1]:
if cls.__name__ == "Object" or cls.__name__ == "StreamingObject":
break

self._initialize_attributes(cls.__annotations__)

self._initialize_attributes(type(self).__annotations__)

def _initialize_attributes(self, attributes: dict[str, Any]) -> None:
for key, type_hint in attributes.items():
self._keys.append(key)

# Handle List[T]
Expand Down Expand Up @@ -167,23 +183,19 @@ def get_value(self) -> list[T]:
return self._values


class Atomic[T](StreamingObject[T]):
class Atomic[T](StreamingObject[T | None]):
_is_empty: bool
_value: T | None
_on_complete_funcs: list[Callable[[T | None], None]]

def __init__(self, item_cls: type[T]) -> None:
self._is_empty = True
self._value = None
self._on_complete_funcs = []

def update(self, value: T) -> None:
def update(self, value: T | None) -> None:
self._value = value
self._is_empty = False

def on_complete(self, func: Callable[[T | None], None]) -> None:
self._on_complete_funcs.append(func)

def _complete(self) -> None:
if not self._is_empty:
for func in self._on_complete_funcs:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "jsonversation"
version = "0.1.2"
version = "0.1.3"
description = "Fast and type-safe JSON stream parser"
authors = [{name = "Vetted.ai Engineering", email = "engineering@vetted.ai"}]
readme = "README.md"
Expand Down
12 changes: 12 additions & 0 deletions tests/aio/models/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,15 @@ async def footer_callback(value: str) -> None:

assert footer_completed == ["Stream Footer"] # Completed on final _complete()
assert len(completed_values) == 1


def test_object_creation_with_parent_classes() -> None:
class ParentObject(jv.Object):
name: jv.String

class ChildObject(ParentObject):
test_field: jv.String

o = ChildObject()

assert o.__getattribute__("name") is not None
12 changes: 12 additions & 0 deletions tests/sync/models/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,15 @@ def footer_callback(value: str) -> None:

assert footer_completed == ["Stream Footer"] # Completed on final _complete()
assert len(completed_values) == 1


def test_object_creation_with_parent_classes() -> None:
class ParentObject(jv.Object):
name: jv.String

class ChildObject(ParentObject):
test_field: jv.String

o = ChildObject()

assert o.__getattribute__("name") is not None
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading