File tree Expand file tree Collapse file tree 2 files changed +11
-0
lines changed Expand file tree Collapse file tree 2 files changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -123,3 +123,7 @@ def __delitem__(self, key: StashKey[T]) -> None:
123
123
def __contains__ (self , key : StashKey [T ]) -> bool :
124
124
"""Return whether key was set."""
125
125
return key in self ._storage
126
+
127
+ def __len__ (self ) -> int :
128
+ """Return how many items exist in the stash."""
129
+ return len (self ._storage )
Original file line number Diff line number Diff line change 6
6
def test_stash () -> None :
7
7
stash = Stash ()
8
8
9
+ assert len (stash ) == 0
10
+ assert not stash
11
+
9
12
key1 = StashKey [str ]()
10
13
key2 = StashKey [int ]()
11
14
@@ -19,6 +22,8 @@ def test_stash() -> None:
19
22
assert stash [key1 ] == "world"
20
23
# Has correct type (no mypy error).
21
24
stash [key1 ] + "string"
25
+ assert len (stash ) == 1
26
+ assert stash
22
27
23
28
# No interaction with another key.
24
29
assert key2 not in stash
@@ -44,6 +49,8 @@ def test_stash() -> None:
44
49
key_setdefault = StashKey [bytes ]()
45
50
assert stash .setdefault (key_setdefault , b"default" ) == b"default"
46
51
assert stash [key_setdefault ] == b"default"
52
+ assert len (stash ) == 3
53
+ assert stash
47
54
48
55
# Can't accidentally add attributes to stash object itself.
49
56
with pytest .raises (AttributeError ):
You can’t perform that action at this time.
0 commit comments