Skip to content

Commit

Permalink
Fix path name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ committed Dec 10, 2024
1 parent 7c42e8c commit 8406e13
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
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.6.3"
version = "0.6.4"
readme = "README.md"
maintainers = [
{ name = "Kacper Kaliński", email = "[email protected]" },
Expand Down
12 changes: 0 additions & 12 deletions src/haiway/state/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,6 @@ def __init__(
),
)

@classmethod
def path[Attribute](
cls,
path: Attribute,
/,
) -> AttributePath[Self, Attribute]:
assert isinstance( # nosec: B101
path, AttributePath
), "Prepare parameter path by using Self._.path.to.property or explicitly"

return cast(AttributePath[Self, Attribute], path)

def updating[Value](
self,
path: AttributePath[Self, Value] | Value,
Expand Down
80 changes: 56 additions & 24 deletions tests/test_attribute_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Mapping, Sequence
from typing import cast

from haiway import AttributePath, State

Expand Down Expand Up @@ -60,78 +61,99 @@ class ExampleState(State):


def test_id_path_points_to_self():
path: AttributePath[ExampleState, ExampleState] = ExampleState.path(ExampleState._)
path: AttributePath[ExampleState, ExampleState] = cast(
AttributePath[ExampleState, ExampleState],
ExampleState._,
)
assert path(state) == state
assert path.__repr__() == "ExampleState"
assert str(path) == ""


def test_attribute_path_points_to_attribute():
path: AttributePath[ExampleState, str] = ExampleState.path(ExampleState._.answer)
path: AttributePath[ExampleState, str] = cast(
AttributePath[ExampleState, str],
ExampleState._.answer,
)
assert path(state) == state.answer
assert path.__repr__() == "ExampleState.answer"
assert str(path) == "answer"


def test_nested_attribute_path_points_to_nested_attribute():
path: AttributePath[ExampleState, float] = ExampleState.path(ExampleState._.nested.value)
path: AttributePath[ExampleState, float] = cast(
AttributePath[ExampleState, float],
ExampleState._.nested.value,
)
assert path(state) == state.nested.value
assert path.__repr__() == "ExampleState.nested.value"
assert str(path) == "nested.value"


def test_recursive_attribute_path_points_to_attribute():
path: AttributePath[ExampleState, RecursiveState] = ExampleState.path(ExampleState._.recursive)
path: AttributePath[ExampleState, RecursiveState] = cast(
AttributePath[ExampleState, RecursiveState],
ExampleState._.recursive,
)
assert path(state) == state.recursive
assert path.__repr__() == "ExampleState.recursive"
assert str(path) == "recursive"


def test_list_item_path_points_to_item():
path: AttributePath[ExampleState, SequenceState] = ExampleState.path(
ExampleState._.list_models[1]
path: AttributePath[ExampleState, SequenceState] = cast(
AttributePath[ExampleState, SequenceState],
ExampleState._.list_models[1],
)
assert path(state) == state.list_models[1]
assert path.__repr__() == "ExampleState.list_models[1]"
assert str(path) == "list_models[1]"


def test_tuple_item_path_points_to_item():
path: AttributePath[ExampleState, SequenceState] = ExampleState.path(
ExampleState._.tuple_models[1]
path: AttributePath[ExampleState, SequenceState] = cast(
AttributePath[ExampleState, SequenceState],
ExampleState._.tuple_models[1],
)
assert path(state) == state.tuple_models[1]
assert path.__repr__() == "ExampleState.tuple_models[1]"
assert str(path) == "tuple_models[1]"


def test_mixed_tuple_item_path_points_to_item():
path: AttributePath[ExampleState, DictState] = ExampleState.path(
ExampleState._.tuple_mixed_models[1]
path: AttributePath[ExampleState, DictState] = cast(
AttributePath[ExampleState, DictState], ExampleState._.tuple_mixed_models[1]
)
assert path(state) == state.tuple_mixed_models[1]
assert path.__repr__() == "ExampleState.tuple_mixed_models[1]"
assert str(path) == "tuple_mixed_models[1]"


def test_dict_item_path_points_to_item():
path: AttributePath[ExampleState, DictState] = ExampleState.path(
ExampleState._.dict_models["B"]
path: AttributePath[ExampleState, DictState] = cast(
AttributePath[ExampleState, DictState],
ExampleState._.dict_models["B"],
)
assert path(state) == state.dict_models["B"]
assert path.__repr__() == "ExampleState.dict_models[B]"
assert str(path) == "dict_models[B]"


def test_id_path_set_updates_self():
path: AttributePath[ExampleState, ExampleState] = ExampleState.path(ExampleState._)
path: AttributePath[ExampleState, ExampleState] = cast(
AttributePath[ExampleState, ExampleState],
ExampleState._,
)
assert path(state, updated=state) == state
assert path.__repr__() == "ExampleState"
assert str(path) == ""


def test_attribute_path_set_updates_attribute():
path: AttributePath[ExampleState, str] = ExampleState.path(ExampleState._.answer)
path: AttributePath[ExampleState, str] = cast(
AttributePath[ExampleState, str],
ExampleState._.answer,
)
updated: ExampleState = path(state, updated="changed")
assert updated != state
assert updated.answer == "changed"
Expand All @@ -141,7 +163,10 @@ def test_attribute_path_set_updates_attribute():


def test_nested_attribute_path_set_updates_nested_attribute():
path: AttributePath[ExampleState, float] = ExampleState.path(ExampleState._.nested.value)
path: AttributePath[ExampleState, float] = cast(
AttributePath[ExampleState, float],
ExampleState._.nested.value,
)
updated: ExampleState = path(state, updated=11.0)
assert updated != state
assert updated.nested.value == 11
Expand All @@ -151,7 +176,10 @@ def test_nested_attribute_path_set_updates_nested_attribute():


def test_recursive_attribute_set_updates_attribute():
path: AttributePath[ExampleState, RecursiveState] = ExampleState.path(ExampleState._.recursive)
path: AttributePath[ExampleState, RecursiveState] = cast(
AttributePath[ExampleState, RecursiveState],
ExampleState._.recursive,
)
updated: ExampleState = path(state, updated=RecursiveState(more=None))
assert updated != state
assert updated.recursive == RecursiveState(more=None)
Expand All @@ -163,8 +191,9 @@ def test_recursive_attribute_set_updates_attribute():


def test_list_item_path_set_updates_item():
path: AttributePath[ExampleState, SequenceState] = ExampleState.path(
ExampleState._.list_models[1]
path: AttributePath[ExampleState, SequenceState] = cast(
AttributePath[ExampleState, SequenceState],
ExampleState._.list_models[1],
)
updated: ExampleState = path(state, updated=SequenceState(value=11))
assert updated != state
Expand All @@ -177,8 +206,9 @@ def test_list_item_path_set_updates_item():


def test_tuple_item_path_set_updates_item():
path: AttributePath[ExampleState, SequenceState] = ExampleState.path(
ExampleState._.tuple_models[1]
path: AttributePath[ExampleState, SequenceState] = cast(
AttributePath[ExampleState, SequenceState],
ExampleState._.tuple_models[1],
)
updated: ExampleState = path(state, updated=SequenceState(value=11))
assert updated != state
Expand All @@ -191,8 +221,9 @@ def test_tuple_item_path_set_updates_item():


def test_mixed_tuple_item_set_updates_item():
path: AttributePath[ExampleState, DictState] = ExampleState.path(
ExampleState._.tuple_mixed_models[1]
path: AttributePath[ExampleState, DictState] = cast(
AttributePath[ExampleState, DictState],
ExampleState._.tuple_mixed_models[1],
)
updated: ExampleState = path(state, updated=DictState(key="updated"))
assert updated != state
Expand All @@ -205,8 +236,9 @@ def test_mixed_tuple_item_set_updates_item():


def test_dict_item_path_set_updates_item():
path: AttributePath[ExampleState, DictState] = ExampleState.path(
ExampleState._.dict_models["B"]
path: AttributePath[ExampleState, DictState] = cast(
AttributePath[ExampleState, DictState],
ExampleState._.dict_models["B"],
)
updated: ExampleState = path(state, updated=DictState(key="updated"))
assert updated != state
Expand Down

0 comments on commit 8406e13

Please sign in to comment.