Files
pytest2/testing/test_stash.py
Pierre Sassoulas 4588653b24 Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyle, to ruff
ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
https://github.com/astral-sh/ruff/issues/4670, previousely based on
reorder-python-import (#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-02-02 09:27:00 +01:00

68 lines
1.8 KiB
Python

from _pytest.stash import Stash
from _pytest.stash import StashKey
import pytest
def test_stash() -> None:
stash = Stash()
assert len(stash) == 0
assert not stash
key1 = StashKey[str]()
key2 = StashKey[int]()
# Basic functionality - single key.
assert key1 not in stash
stash[key1] = "hello"
assert key1 in stash
assert stash[key1] == "hello"
assert stash.get(key1, None) == "hello"
stash[key1] = "world"
assert stash[key1] == "world"
# Has correct type (no mypy error).
stash[key1] + "string"
assert len(stash) == 1
assert stash
# No interaction with another key.
assert key2 not in stash
assert stash.get(key2, None) is None
with pytest.raises(KeyError):
stash[key2]
with pytest.raises(KeyError):
del stash[key2]
stash[key2] = 1
assert stash[key2] == 1
# Has correct type (no mypy error).
stash[key2] + 20
del stash[key1]
with pytest.raises(KeyError):
del stash[key1]
with pytest.raises(KeyError):
stash[key1]
# setdefault
stash[key1] = "existing"
assert stash.setdefault(key1, "default") == "existing"
assert stash[key1] == "existing"
key_setdefault = StashKey[bytes]()
assert stash.setdefault(key_setdefault, b"default") == b"default"
assert stash[key_setdefault] == b"default"
assert len(stash) == 3
assert stash
# Can't accidentally add attributes to stash object itself.
with pytest.raises(AttributeError):
stash.foo = "nope" # type: ignore[attr-defined]
# No interaction with another stash.
stash2 = Stash()
key3 = StashKey[int]()
assert key2 not in stash2
stash2[key2] = 100
stash2[key3] = 200
assert stash2[key2] + stash2[key3] == 300
assert stash[key2] == 1
assert key3 not in stash