def test_store() -> None: store = Store() key1 = StoreKey[str]() key2 = StoreKey[int]() # Basic functionality - single key. assert key1 not in store store[key1] = "hello" assert key1 in store assert store[key1] == "hello" assert store.get(key1, None) == "hello" store[key1] = "world" assert store[key1] == "world" # Has correct type (no mypy error). store[key1] + "string" # No interaction with another key. assert key2 not in store assert store.get(key2, None) is None with pytest.raises(KeyError): store[key2] with pytest.raises(KeyError): del store[key2] store[key2] = 1 assert store[key2] == 1 # Has correct type (no mypy error). store[key2] + 20 del store[key1] with pytest.raises(KeyError): del store[key1] with pytest.raises(KeyError): store[key1] # setdefault store[key1] = "existing" assert store.setdefault(key1, "default") == "existing" assert store[key1] == "existing" key_setdefault = StoreKey[bytes]() assert store.setdefault(key_setdefault, b"default") == b"default" assert store[key_setdefault] == b"default" # Can't accidentally add attributes to store object itself. with pytest.raises(AttributeError): store.foo = "nope" # type: ignore[attr-defined] # noqa: F821 # No interaction with anoter store. store2 = Store() key3 = StoreKey[int]() assert key2 not in store2 store2[key2] = 100 store2[key3] = 200 assert store2[key2] + store2[key3] == 300 assert store[key2] == 1 assert key3 not in store
def __init__(self, pluginmanager, *, invocation_params=None) -> None: from .argparsing import Parser, FILE_OR_DIR if invocation_params is None: invocation_params = self.InvocationParams(args=(), plugins=None, dir=Path().resolve()) self.option = argparse.Namespace() self.invocation_params = invocation_params _a = FILE_OR_DIR self._parser = Parser( usage="%(prog)s [options] [{}] [{}] [...]".format(_a, _a), processopt=self._processopt, ) self.pluginmanager = pluginmanager self.trace = self.pluginmanager.trace.root.get("config") self.hook = self.pluginmanager.hook self._inicache = {} # type: Dict[str, Any] self._override_ini = () # type: Sequence[str] self._opt2dest = {} # type: Dict[str, str] self._cleanup = [] # type: List[Callable[[], None]] # A place where plugins can store information on the config for their # own use. Currently only intended for internal plugins. self._store = Store() self.pluginmanager.register(self, "pytestconfig") self._configured = False self.hook.pytest_addoption.call_historic( kwargs=dict(parser=self._parser, pluginmanager=self.pluginmanager)) if TYPE_CHECKING: from _pytest.cacheprovider import Cache self.cache = None # type: Optional[Cache]
def __init__( self, name: str, parent: Optional["Node"] = None, config: Optional[Config] = None, session: Optional["Session"] = None, fspath: Optional[py.path.local] = None, nodeid: Optional[str] = None, ) -> None: #: a unique name within the scope of the parent node self.name = name #: the parent collector node. self.parent = parent #: the pytest config object if config: self.config = config else: if not parent: raise TypeError("config or parent must be provided") self.config = parent.config #: the session this node is part of if session: self.session = session else: if not parent: raise TypeError("session or parent must be provided") self.session = parent.session #: filesystem path where this node was collected from (can be None) self.fspath = fspath or getattr(parent, "fspath", None) #: keywords/markers collected from all scopes self.keywords = NodeKeywords(self) #: the marker objects belonging to this node self.own_markers = [] # type: List[Mark] #: allow adding of extra keywords to use for matching self.extra_keyword_matches = set() # type: Set[str] # used for storing artificial fixturedefs for direct parametrization self._name2pseudofixturedef = {} # type: Dict[str, FixtureDef] if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: if not self.parent: raise TypeError("nodeid or parent must be provided") self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name # A place where plugins can store information on the node for their # own use. Currently only intended for internal plugins. self._store = Store()
def __init__( self, name: str, parent: "Optional[Node]" = None, config: Optional[Config] = None, session: "Optional[Session]" = None, fspath: Optional[LEGACY_PATH] = None, path: Optional[Path] = None, nodeid: Optional[str] = None, ) -> None: #: A unique name within the scope of the parent node. self.name = name #: The parent collector node. self.parent = parent #: The pytest config object. if config: self.config: Config = config else: if not parent: raise TypeError("config or parent must be provided") self.config = parent.config #: The pytest session this node is part of. if session: self.session = session else: if not parent: raise TypeError("session or parent must be provided") self.session = parent.session #: Filesystem path where this node was collected from (can be None). self.path = _imply_path(path or getattr(parent, "path", None), fspath=fspath)[0] # The explicit annotation is to avoid publicly exposing NodeKeywords. #: Keywords/markers collected from all scopes. self.keywords: MutableMapping[str, Any] = NodeKeywords(self) #: The marker objects belonging to this node. self.own_markers: List[Mark] = [] #: Allow adding of extra keywords to use for matching. self.extra_keyword_matches: Set[str] = set() if nodeid is not None: assert "::()" not in nodeid self._nodeid = nodeid else: if not self.parent: raise TypeError("nodeid or parent must be provided") self._nodeid = self.parent.nodeid if self.name != "()": self._nodeid += "::" + self.name # A place where plugins can store information on the node for their # own use. Currently only intended for internal plugins. self._store = Store()