Skip to content

Commit 1e960ff

Browse files
authored
Rename Variable.bind_to to Variable.bind (#73)
1 parent 43eab3e commit 1e960ff

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

src/spellbind/values.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing_extensions import deprecated
4+
35
from abc import ABC, abstractmethod
46
from contextlib import contextmanager
57
from typing import TypeVar, Generic, Optional, Iterable, TYPE_CHECKING, Callable, Sequence, ContextManager, \
@@ -167,7 +169,11 @@ def value(self) -> _S: ...
167169
def value(self, new_value: _S) -> None: ...
168170

169171
@abstractmethod
170-
def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None: ...
172+
def bind(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None: ...
173+
174+
@deprecated("Use bind() instead")
175+
def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
176+
return self.bind(value, already_bound_ok, bind_weakly)
171177

172178
@abstractmethod
173179
def unbind(self, not_bound_ok: bool = False) -> None: ...
@@ -219,7 +225,7 @@ def _set_value_bypass_bound_check(self, new_value: _S) -> None:
219225
def observable(self) -> BiObservable[_S, _S]:
220226
return self._on_change
221227

222-
def bind_to(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
228+
def bind(self, value: Value[_S], already_bound_ok: bool = False, bind_weakly: bool = True) -> None:
223229
if value is self:
224230
raise RecursionError("Cannot bind a Variable to itself.")
225231
if value.is_derived_from(self):

tests/test_values/test_int_values/test_add_int_values.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def test_bind_to_added_int_variables():
9191
v1 = IntVariable(2)
9292

9393
variable = IntVariable(42)
94-
variable.bind_to(v0 + v1)
94+
variable.bind(v0 + v1)
9595
assert variable.value == 3
9696

9797
v0.value = 5
@@ -104,7 +104,7 @@ def test_bind_and_unbind_to_added_int_variables():
104104
v1 = IntVariable(2)
105105

106106
variable = IntVariable(42)
107-
variable.bind_to(v0 + v1)
107+
variable.bind(v0 + v1)
108108
v0.value = 5
109109

110110
variable.unbind()

tests/test_values/test_simple_variable.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def test_simple_variable_bind_twice_to_same():
8787
variable = SimpleVariable("test")
8888
constant = Constant("value")
8989

90-
variable.bind_to(constant)
91-
variable.bind_to(constant, already_bound_ok=True)
90+
variable.bind(constant)
91+
variable.bind(constant, already_bound_ok=True)
9292

9393
assert variable.value == "value"
9494

@@ -97,7 +97,7 @@ def test_simple_variable_bind_to_constant():
9797
variable = SimpleVariable("old")
9898
constant = Constant("new")
9999

100-
variable.bind_to(constant)
100+
variable.bind(constant)
101101

102102
assert variable.value == "new"
103103

@@ -106,7 +106,7 @@ def test_simple_variable_bind_to_simple_variable():
106106
variable1 = SimpleVariable(100)
107107
variable2 = SimpleVariable(200)
108108

109-
variable1.bind_to(variable2)
109+
variable1.bind(variable2)
110110

111111
assert variable1.value == 200
112112

@@ -116,19 +116,19 @@ def test_simple_variable_bind_already_bound_error():
116116
constant1 = Constant("value1")
117117
constant2 = Constant("value2")
118118

119-
variable.bind_to(constant1)
119+
variable.bind(constant1)
120120

121121
with pytest.raises(ValueError):
122-
variable.bind_to(constant2)
122+
variable.bind(constant2)
123123

124124

125125
def test_simple_variable_bind_already_bound_ok():
126126
variable = SimpleVariable("test")
127127
constant1 = Constant("value1")
128128
constant2 = Constant("value2")
129129

130-
variable.bind_to(constant1)
131-
variable.bind_to(constant2, already_bound_ok=True)
130+
variable.bind(constant1)
131+
variable.bind(constant2, already_bound_ok=True)
132132

133133
assert variable.value == "value2"
134134

@@ -137,7 +137,7 @@ def test_simple_variable_change_after_unbind():
137137
variable = SimpleVariable("initial")
138138
constant = Constant("bound_value")
139139

140-
variable.bind_to(constant)
140+
variable.bind(constant)
141141
variable.unbind()
142142
variable.value = "after_unbind"
143143

@@ -148,7 +148,7 @@ def test_simple_variable_change_without_unbind_raises():
148148
variable = SimpleVariable("initial")
149149
constant = Constant("bound_value")
150150

151-
variable.bind_to(constant)
151+
variable.bind(constant)
152152
with pytest.raises(ValueError):
153153
variable.value = "after_unbind"
154154

@@ -157,7 +157,7 @@ def test_simple_variable_change_root_after_unbind():
157157
dependent = SimpleVariable("dependent")
158158
root = SimpleVariable("root")
159159

160-
dependent.bind_to(root)
160+
dependent.bind(root)
161161
dependent.unbind()
162162
root.value = "new_root_value"
163163
assert dependent.value == "root"
@@ -184,7 +184,7 @@ def test_simple_variable_bind_updates_value():
184184

185185
variable.observe(observer)
186186
constant = Constant(42)
187-
variable.bind_to(constant)
187+
variable.bind(constant)
188188

189189
observer.assert_called_once_with(42)
190190

@@ -194,7 +194,7 @@ def test_simple_variable_bound_value_changes_propagate():
194194
variable2 = SimpleVariable("initial")
195195
observer = OneParameterObserver()
196196

197-
variable1.bind_to(variable2)
197+
variable1.bind(variable2)
198198
variable1.observe(observer)
199199
variable2.value = "propagated"
200200

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

216216
with pytest.raises(RecursionError):
217-
variable.bind_to(variable)
217+
variable.bind(variable)
218218

219219

220220
def test_simple_variable_set_value_while_bound_raises():
221221
variable = SimpleVariable("initial")
222222
constant = Constant("constant")
223223

224-
variable.bind_to(constant)
224+
variable.bind(constant)
225225
with pytest.raises(ValueError):
226226
variable.value = "manual_value"
227227

@@ -242,17 +242,17 @@ def test_simple_variable_rebind_after_unbind():
242242
constant1 = Constant("first")
243243
constant2 = Constant("second")
244244

245-
variable.bind_to(constant1)
245+
variable.bind(constant1)
246246
variable.unbind()
247-
variable.bind_to(constant2)
247+
variable.bind(constant2)
248248

249249
assert variable.value == "second"
250250

251251

252252
def test_bind_weak_reference_clears():
253253
root_var = SimpleVariable("root0")
254254
dependent_var = SimpleVariable("")
255-
dependent_var.bind_to(root_var, bind_weakly=True)
255+
dependent_var.bind(root_var, bind_weakly=True)
256256

257257
values = []
258258
dependent_var.observe(lambda value: values.append(value))
@@ -268,7 +268,7 @@ def test_bind_weak_reference_clears():
268268
def test_bind_strong_reference_stays():
269269
root_var = SimpleVariable("root0")
270270
dependent_var = SimpleVariable("")
271-
dependent_var.bind_to(root_var, bind_weakly=False)
271+
dependent_var.bind(root_var, bind_weakly=False)
272272

273273
values = []
274274
dependent_var.observe(lambda value: values.append(value))
@@ -286,8 +286,8 @@ def test_daisy_chain_variables_weak_reference_stays():
286286
middle_var = SimpleVariable("")
287287
dependent_var = SimpleVariable("")
288288

289-
middle_var.bind_to(root_var, bind_weakly=True)
290-
dependent_var.bind_to(middle_var, bind_weakly=False)
289+
middle_var.bind(root_var, bind_weakly=True)
290+
dependent_var.bind(middle_var, bind_weakly=False)
291291

292292
values = []
293293
dependent_var.observe(lambda value: values.append(value))

tests/test_values/test_simple_variable_derived.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_simple_variable_derived_from_bound():
1313
variable = SimpleVariable("test")
1414
constant = Constant("bound")
1515

16-
variable.bind_to(constant)
16+
variable.bind(constant)
1717

1818
assert variable.derived_from == frozenset()
1919

@@ -28,7 +28,7 @@ def test_simple_variable_deep_derived_from_single_level():
2828
variable = SimpleVariable("test")
2929
constant = Constant("bound")
3030

31-
variable.bind_to(constant)
31+
variable.bind(constant)
3232

3333
assert list(variable.deep_derived_from) == []
3434

@@ -38,8 +38,8 @@ def test_simple_variable_deep_derived_from_two_levels():
3838
variable2 = SimpleVariable("test2")
3939
constant = Constant("bound")
4040

41-
variable2.bind_to(constant)
42-
variable1.bind_to(variable2)
41+
variable2.bind(constant)
42+
variable1.bind(variable2)
4343

4444
dependencies = list(variable1.deep_derived_from)
4545
assert len(dependencies) == 1
@@ -52,9 +52,9 @@ def test_simple_variable_deep_derived_from_three_levels():
5252
variable3 = SimpleVariable("test3")
5353
constant = Constant("bound")
5454

55-
variable3.bind_to(constant)
56-
variable2.bind_to(variable3)
57-
variable1.bind_to(variable2)
55+
variable3.bind(constant)
56+
variable2.bind(variable3)
57+
variable1.bind(variable2)
5858

5959
dependencies = list(variable1.deep_derived_from)
6060
assert len(dependencies) == 2
@@ -66,22 +66,22 @@ def test_simple_variable_deep_derived_from_circular_two_variables():
6666
variable1 = SimpleVariable("test1")
6767
variable2 = SimpleVariable("test2")
6868

69-
variable1.bind_to(variable2)
69+
variable1.bind(variable2)
7070

7171
with pytest.raises(RecursionError):
72-
variable2.bind_to(variable1)
72+
variable2.bind(variable1)
7373

7474

7575
def test_simple_variable_deep_derived_from_circular_three_variables():
7676
variable1 = SimpleVariable("test1")
7777
variable2 = SimpleVariable("test2")
7878
variable3 = SimpleVariable("test3")
7979

80-
variable1.bind_to(variable2)
81-
variable2.bind_to(variable3)
80+
variable1.bind(variable2)
81+
variable2.bind(variable3)
8282

8383
with pytest.raises(RecursionError):
84-
variable3.bind_to(variable1)
84+
variable3.bind(variable1)
8585

8686

8787
def test_simple_variable_deep_derived_from_diamond_pattern():
@@ -90,10 +90,10 @@ def test_simple_variable_deep_derived_from_diamond_pattern():
9090
variable_right = SimpleVariable("right")
9191
variable_bottom = SimpleVariable("bottom")
9292

93-
variable_left.bind_to(variable_top)
94-
variable_right.bind_to(variable_top)
95-
variable_bottom.bind_to(variable_left)
96-
variable_bottom.bind_to(variable_right, already_bound_ok=True)
93+
variable_left.bind(variable_top)
94+
variable_right.bind(variable_top)
95+
variable_bottom.bind(variable_left)
96+
variable_bottom.bind(variable_right, already_bound_ok=True)
9797

9898
dependencies = list(variable_bottom.deep_derived_from)
9999
assert len(dependencies) == 2

0 commit comments

Comments
 (0)