Beispiel #1
0
    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

        if config:
            #: The pytest config object.
            self.config: Config = config
        else:
            if not parent:
                raise TypeError("config or parent must be provided")
            self.config = parent.config

        if session:
            #: The pytest session this node is part of.
            self.session = session
        else:
            if not parent:
                raise TypeError("session or parent must be provided")
            self.session = parent.session

        if path is None and fspath is None:
            path = getattr(parent, "path", None)
        #: Filesystem path where this node was collected from (can be None).
        self.path: Path = _imply_path(type(self), path, fspath=fspath)

        # 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 + "::" + self.name

        #: A place where plugins can store information on the node for their
        #: own use.
        #:
        #: :type: Stash
        self.stash = Stash()
        # Deprecated alias. Was never public. Can be removed in a few releases.
        self._store = self.stash
Beispiel #2
0
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