stash: implement __len__
Part of the MutableMapping abc (though we can't actually implement that).
This commit is contained in:
parent
5470d33e82
commit
5f39e31736
|
@ -123,3 +123,7 @@ class Stash:
|
||||||
def __contains__(self, key: StashKey[T]) -> bool:
|
def __contains__(self, key: StashKey[T]) -> bool:
|
||||||
"""Return whether key was set."""
|
"""Return whether key was set."""
|
||||||
return key in self._storage
|
return key in self._storage
|
||||||
|
|
||||||
|
def __len__(self) -> int:
|
||||||
|
"""Return how many items exist in the stash."""
|
||||||
|
return len(self._storage)
|
||||||
|
|
|
@ -6,6 +6,9 @@ from _pytest.stash import StashKey
|
||||||
def test_stash() -> None:
|
def test_stash() -> None:
|
||||||
stash = Stash()
|
stash = Stash()
|
||||||
|
|
||||||
|
assert len(stash) == 0
|
||||||
|
assert not stash
|
||||||
|
|
||||||
key1 = StashKey[str]()
|
key1 = StashKey[str]()
|
||||||
key2 = StashKey[int]()
|
key2 = StashKey[int]()
|
||||||
|
|
||||||
|
@ -19,6 +22,8 @@ def test_stash() -> None:
|
||||||
assert stash[key1] == "world"
|
assert stash[key1] == "world"
|
||||||
# Has correct type (no mypy error).
|
# Has correct type (no mypy error).
|
||||||
stash[key1] + "string"
|
stash[key1] + "string"
|
||||||
|
assert len(stash) == 1
|
||||||
|
assert stash
|
||||||
|
|
||||||
# No interaction with another key.
|
# No interaction with another key.
|
||||||
assert key2 not in stash
|
assert key2 not in stash
|
||||||
|
@ -44,6 +49,8 @@ def test_stash() -> None:
|
||||||
key_setdefault = StashKey[bytes]()
|
key_setdefault = StashKey[bytes]()
|
||||||
assert stash.setdefault(key_setdefault, b"default") == b"default"
|
assert stash.setdefault(key_setdefault, b"default") == b"default"
|
||||||
assert stash[key_setdefault] == b"default"
|
assert stash[key_setdefault] == b"default"
|
||||||
|
assert len(stash) == 3
|
||||||
|
assert stash
|
||||||
|
|
||||||
# Can't accidentally add attributes to stash object itself.
|
# Can't accidentally add attributes to stash object itself.
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
|
|
Loading…
Reference in New Issue