-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb_plugged.py
44 lines (38 loc) · 828 Bytes
/
b_plugged.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# b_plugged.py
from copy import copy
from a_encapsulation import Bob, Leaky, check_for_leaks
class Plugged(Leaky): # Only to reduce code here
def __init__(self, n: int, lst: list):
super().__init__(n, lst)
@property
def x(self) -> int:
return self._n
@property # Decouple by copying:
def lst(self) -> list:
return self._lst.copy()
@property # Ditto:
def bob(self) -> Bob:
return copy(self._bob)
# Can re-leak here & in subclasses:
def leak(self) -> Bob:
return self._bob
def test_leaks():
before, after = check_for_leaks(Plugged)
assert (
before
== """
Plugged:
n: 42
lst: ['x', 'y']
bob: Bob
"""
)
assert (
after
== """
Plugged:
n: 42
lst: ['x', 'y']
bob: Bob
"""
)