Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update State as_dict #16

Merged
merged 1 commit into from
Nov 18, 2024
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "haiway"
description = "Framework for dependency injection and state management within structured concurrency model."
version = "0.5.0"
version = "0.5.1"
readme = "README.md"
maintainers = [
{ name = "Kacper Kaliński", email = "[email protected]" },
Expand Down
10 changes: 8 additions & 2 deletions src/haiway/state/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from haiway.state.attributes import AttributeAnnotation, attribute_annotations
from haiway.state.validation import attribute_type_validator
from haiway.types.missing import MISSING, Missing
from haiway.types import MISSING, Missing, not_missing

__all__ = [
"State",
Expand Down Expand Up @@ -184,7 +184,13 @@ def updated(
return self.__replace__(**kwargs)

def as_dict(self) -> dict[str, Any]:
return vars(self)
dict_result: dict[str, Any] = {}
for key in self.__ATTRIBUTES__.keys():
value: Any | Missing = getattr(self, key, MISSING)
if not_missing(value):
dict_result[key] = value

return dict_result

def __str__(self) -> str:
attributes: str = ", ".join([f"{key}: {value}" for key, value in vars(self).items()])
Expand Down
33 changes: 32 additions & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Callable
from typing import Literal, Protocol, Self, TypedDict, runtime_checkable

from haiway import State, frozenlist
from haiway import MISSING, Missing, State, frozenlist


def test_basic_initializes_with_arguments() -> None:
Expand Down Expand Up @@ -63,6 +63,17 @@ class Basics(State):
assert basic.optional is None


def test_basic_equals_checks_properties() -> None:
class Basics(State):
string: str
integer: int

assert Basics(string="a", integer=1) == Basics(string="a", integer=1)
assert Basics(string="a", integer=1) != Basics(string="b", integer=1)
assert Basics(string="a", integer=1) != Basics(string="a", integer=2)
assert Basics(string="a", integer=1) != Basics(string="b", integer=2)


def test_basic_initializes_with_arguments_and_defaults() -> None:
class Basics(State):
string: str
Expand Down Expand Up @@ -119,3 +130,23 @@ class Recursive(State):
recursion=None,
self_recursion=None,
)


def test_dict_skips_missing_properties() -> None:
class Basics(State):
string: str
integer: int | Missing | None

assert Basics(string="a", integer=1).as_dict() == {"string": "a", "integer": 1}
assert Basics(string="a", integer=MISSING).as_dict() == {"string": "a"}
assert Basics(string="a", integer=None).as_dict() == {"string": "a", "integer": None}


def test_initialization_allows_missing_properties() -> None:
class Basics(State):
string: str
integer: int | Missing | None

assert Basics(**{"string": "a", "integer": 1}) == Basics(string="a", integer=1)
assert Basics(**{"string": "a", "integer": None}) == Basics(string="a", integer=None)
assert Basics(**{"string": "a"}) == Basics(string="a", integer=MISSING)