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
10 changes: 8 additions & 2 deletions src/spellbind/values.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing_extensions import deprecated

from abc import ABC, abstractmethod
from contextlib import contextmanager
from typing import TypeVar, Generic, Optional, Iterable, TYPE_CHECKING, Callable, Sequence, ContextManager, \
Expand Down Expand Up @@ -167,7 +169,11 @@ def value(self) -> _S: ...
def value(self, new_value: _S) -> None: ...

@abstractmethod
def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None: ...
def bind(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None: ...

@deprecated("Use bind() instead")
def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
return self.bind(value, already_bound_ok, bind_weakly)

@abstractmethod
def unbind(self, not_bound_ok: bool = False) -> None: ...
Expand Down Expand Up @@ -219,7 +225,7 @@ def _set_value_bypass_bound_check(self, new_value: _S) -> None:
def observable(self) -> BiObservable[_S, _S]:
return self._on_change

def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
def bind(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
if value is self:
raise RecursionError("Cannot bind a Variable to itself.")
if value.is_derived_from(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_values/test_int_values/test_add_int_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_bind_to_added_int_variables():
v1 = IntVariable(2)

variable = IntVariable(42)
variable.bind_to(v0 + v1)
variable.bind(v0 + v1)
assert variable.value == 3

v0.value = 5
Expand All @@ -104,7 +104,7 @@ def test_bind_and_unbind_to_added_int_variables():
v1 = IntVariable(2)

variable = IntVariable(42)
variable.bind_to(v0 + v1)
variable.bind(v0 + v1)
v0.value = 5

variable.unbind()
Expand Down
42 changes: 21 additions & 21 deletions tests/test_values/test_simple_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_simple_variable_bind_twice_to_same():
variable = SimpleVariable("test")
constant = Constant("value")

variable.bind_to(constant)
variable.bind_to(constant, already_bound_ok=True)
variable.bind(constant)
variable.bind(constant, already_bound_ok=True)

assert variable.value == "value"

Expand All @@ -97,7 +97,7 @@ def test_simple_variable_bind_to_constant():
variable = SimpleVariable("old")
constant = Constant("new")

variable.bind_to(constant)
variable.bind(constant)

assert variable.value == "new"

Expand All @@ -106,7 +106,7 @@ def test_simple_variable_bind_to_simple_variable():
variable1 = SimpleVariable(100)
variable2 = SimpleVariable(200)

variable1.bind_to(variable2)
variable1.bind(variable2)

assert variable1.value == 200

Expand All @@ -116,19 +116,19 @@ def test_simple_variable_bind_already_bound_error():
constant1 = Constant("value1")
constant2 = Constant("value2")

variable.bind_to(constant1)
variable.bind(constant1)

with pytest.raises(ValueError):
variable.bind_to(constant2)
variable.bind(constant2)


def test_simple_variable_bind_already_bound_ok():
variable = SimpleVariable("test")
constant1 = Constant("value1")
constant2 = Constant("value2")

variable.bind_to(constant1)
variable.bind_to(constant2, already_bound_ok=True)
variable.bind(constant1)
variable.bind(constant2, already_bound_ok=True)

assert variable.value == "value2"

Expand All @@ -137,7 +137,7 @@ def test_simple_variable_change_after_unbind():
variable = SimpleVariable("initial")
constant = Constant("bound_value")

variable.bind_to(constant)
variable.bind(constant)
variable.unbind()
variable.value = "after_unbind"

Expand All @@ -148,7 +148,7 @@ def test_simple_variable_change_without_unbind_raises():
variable = SimpleVariable("initial")
constant = Constant("bound_value")

variable.bind_to(constant)
variable.bind(constant)
with pytest.raises(ValueError):
variable.value = "after_unbind"

Expand All @@ -157,7 +157,7 @@ def test_simple_variable_change_root_after_unbind():
dependent = SimpleVariable("dependent")
root = SimpleVariable("root")

dependent.bind_to(root)
dependent.bind(root)
dependent.unbind()
root.value = "new_root_value"
assert dependent.value == "root"
Expand All @@ -184,7 +184,7 @@ def test_simple_variable_bind_updates_value():

variable.observe(observer)
constant = Constant(42)
variable.bind_to(constant)
variable.bind(constant)

observer.assert_called_once_with(42)

Expand All @@ -194,7 +194,7 @@ def test_simple_variable_bound_value_changes_propagate():
variable2 = SimpleVariable("initial")
observer = OneParameterObserver()

variable1.bind_to(variable2)
variable1.bind(variable2)
variable1.observe(observer)
variable2.value = "propagated"

Expand All @@ -214,14 +214,14 @@ def test_simple_variable_bind_to_itself():
variable = SimpleVariable("test")

with pytest.raises(RecursionError):
variable.bind_to(variable)
variable.bind(variable)


def test_simple_variable_set_value_while_bound_raises():
variable = SimpleVariable("initial")
constant = Constant("constant")

variable.bind_to(constant)
variable.bind(constant)
with pytest.raises(ValueError):
variable.value = "manual_value"

Expand All @@ -242,17 +242,17 @@ def test_simple_variable_rebind_after_unbind():
constant1 = Constant("first")
constant2 = Constant("second")

variable.bind_to(constant1)
variable.bind(constant1)
variable.unbind()
variable.bind_to(constant2)
variable.bind(constant2)

assert variable.value == "second"


def test_bind_weak_reference_clears():
root_var = SimpleVariable("root0")
dependent_var = SimpleVariable("")
dependent_var.bind_to(root_var, bind_weakly=True)
dependent_var.bind(root_var, bind_weakly=True)

values = []
dependent_var.observe(lambda value: values.append(value))
Expand All @@ -268,7 +268,7 @@ def test_bind_weak_reference_clears():
def test_bind_strong_reference_stays():
root_var = SimpleVariable("root0")
dependent_var = SimpleVariable("")
dependent_var.bind_to(root_var, bind_weakly=False)
dependent_var.bind(root_var, bind_weakly=False)

values = []
dependent_var.observe(lambda value: values.append(value))
Expand All @@ -286,8 +286,8 @@ def test_daisy_chain_variables_weak_reference_stays():
middle_var = SimpleVariable("")
dependent_var = SimpleVariable("")

middle_var.bind_to(root_var, bind_weakly=True)
dependent_var.bind_to(middle_var, bind_weakly=False)
middle_var.bind(root_var, bind_weakly=True)
dependent_var.bind(middle_var, bind_weakly=False)

values = []
dependent_var.observe(lambda value: values.append(value))
Expand Down
32 changes: 16 additions & 16 deletions tests/test_values/test_simple_variable_derived.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_simple_variable_derived_from_bound():
variable = SimpleVariable("test")
constant = Constant("bound")

variable.bind_to(constant)
variable.bind(constant)

assert variable.derived_from == frozenset()

Expand All @@ -28,7 +28,7 @@ def test_simple_variable_deep_derived_from_single_level():
variable = SimpleVariable("test")
constant = Constant("bound")

variable.bind_to(constant)
variable.bind(constant)

assert list(variable.deep_derived_from) == []

Expand All @@ -38,8 +38,8 @@ def test_simple_variable_deep_derived_from_two_levels():
variable2 = SimpleVariable("test2")
constant = Constant("bound")

variable2.bind_to(constant)
variable1.bind_to(variable2)
variable2.bind(constant)
variable1.bind(variable2)

dependencies = list(variable1.deep_derived_from)
assert len(dependencies) == 1
Expand All @@ -52,9 +52,9 @@ def test_simple_variable_deep_derived_from_three_levels():
variable3 = SimpleVariable("test3")
constant = Constant("bound")

variable3.bind_to(constant)
variable2.bind_to(variable3)
variable1.bind_to(variable2)
variable3.bind(constant)
variable2.bind(variable3)
variable1.bind(variable2)

dependencies = list(variable1.deep_derived_from)
assert len(dependencies) == 2
Expand All @@ -66,22 +66,22 @@ def test_simple_variable_deep_derived_from_circular_two_variables():
variable1 = SimpleVariable("test1")
variable2 = SimpleVariable("test2")

variable1.bind_to(variable2)
variable1.bind(variable2)

with pytest.raises(RecursionError):
variable2.bind_to(variable1)
variable2.bind(variable1)


def test_simple_variable_deep_derived_from_circular_three_variables():
variable1 = SimpleVariable("test1")
variable2 = SimpleVariable("test2")
variable3 = SimpleVariable("test3")

variable1.bind_to(variable2)
variable2.bind_to(variable3)
variable1.bind(variable2)
variable2.bind(variable3)

with pytest.raises(RecursionError):
variable3.bind_to(variable1)
variable3.bind(variable1)


def test_simple_variable_deep_derived_from_diamond_pattern():
Expand All @@ -90,10 +90,10 @@ def test_simple_variable_deep_derived_from_diamond_pattern():
variable_right = SimpleVariable("right")
variable_bottom = SimpleVariable("bottom")

variable_left.bind_to(variable_top)
variable_right.bind_to(variable_top)
variable_bottom.bind_to(variable_left)
variable_bottom.bind_to(variable_right, already_bound_ok=True)
variable_left.bind(variable_top)
variable_right.bind(variable_top)
variable_bottom.bind(variable_left)
variable_bottom.bind(variable_right, already_bound_ok=True)

dependencies = list(variable_bottom.deep_derived_from)
assert len(dependencies) == 2
Expand Down