Skip to content

Commit 32de2a5

Browse files
committed
Same property getter now checks if values actually the same
1 parent 12c91f2 commit 32de2a5

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

tests/test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,11 @@ def test_effects(self):
238238
self.assertEqual(self.magnifier.source.raw, self.window.current_rectangle)
239239
with self.magnifier.source.batch() as source:
240240
source.same = -1
241+
self.assertEqual(source.same, -1)
241242
source.start_same = 0
243+
self.assertEqual(source.start_same, 0)
242244
source.end_same = 8
243-
self.assertEqual(source.same, source.start_same)
245+
self.assertEqual(source.same, None)
244246
self.assertEqual(source.right, source.end_same)
245247
self.assertEqual((0, 0), source.start)
246248
self.assertEqual((8, 8), source.end)

win_magnification/_object_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,10 @@ def raw(self):
364364
def reset(self):
365365
"""Resets value of wrapped field to :attr:`.default`"""
366366
del self.raw
367+
368+
369+
def ensure_same(*values: T) -> typing.Optional[T]:
370+
pattern = values[0]
371+
if all(value == pattern for value in values):
372+
return pattern
373+
return None

win_magnification/_objects.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ def __init__(
6262
super().__init__(datasource)
6363

6464
@property
65-
def same(self) -> float:
65+
def same(self) -> typing.Optional[float]:
6666
"""
6767
| Get/set same value from/to (x, y)
68+
| Or None if values differs
6869
| |Accessors: Get Set|
6970
"""
70-
return self.x
71+
with self.batch():
72+
return _utils.ensure_same(
73+
self.x,
74+
self.y,
75+
)
7176

7277
@same.setter
7378
def same(self, value: float):
@@ -130,12 +135,17 @@ def __init__(
130135
super().__init__(datasource)
131136

132137
@property
133-
def same(self) -> int:
138+
def same(self) -> typing.Optional[int]:
134139
"""
135140
| Get/set same value from/to (x, y)
141+
| Or None if values differs
136142
| |Accessors: Get Set|
137143
"""
138-
return self.x
144+
with self.batch():
145+
return _utils.ensure_same(
146+
self.x,
147+
self.y,
148+
)
139149

140150
@same.setter
141151
def same(self, value: int):
@@ -207,12 +217,17 @@ def start(self, value: typing.Tuple[int, int]):
207217
self.left, self.top = value
208218

209219
@property
210-
def start_same(self) -> int:
220+
def start_same(self) -> typing.Optional[int]:
211221
"""
212222
| Get/set same value from/to (left, top)
223+
| Or None if values differs
213224
| |Accessors: Get Set|
214225
"""
215-
return self.left
226+
with self.batch():
227+
return _utils.ensure_same(
228+
self.left,
229+
self.top,
230+
)
216231

217232
@start_same.setter
218233
def start_same(self, value: int):
@@ -232,24 +247,36 @@ def end(self, value: typing.Tuple[int, int]):
232247
self.right, self.bottom = value
233248

234249
@property
235-
def end_same(self) -> int:
250+
def end_same(self) -> typing.Optional[int]:
236251
"""
237252
| Get/set same value from/to (right, bottom)
253+
| Or None if values differs
238254
| |Accessors: Get Set|
239255
"""
240-
return self.right
256+
with self.batch():
257+
return _utils.ensure_same(
258+
self.right,
259+
self.bottom,
260+
)
241261

242262
@end_same.setter
243263
def end_same(self, value: int):
244264
self.end = value, value
245265

246266
@property
247-
def same(self) -> int:
267+
def same(self) -> typing.Optional[int]:
248268
"""
249269
| Get/set same value from/to (left, top, right, bottom)
270+
| Or None if values differs
250271
| |Accessors: Get Set|
251272
"""
252-
return self.left
273+
with self.batch():
274+
return _utils.ensure_same(
275+
self.left,
276+
self.top,
277+
self.right,
278+
self.bottom,
279+
)
253280

254281
@same.setter
255282
def same(self, value: int):

0 commit comments

Comments
 (0)