add type annotations, pass over Node to more

This commit is contained in:
Ronny Pfannschmidt 2023-11-14 16:44:55 +01:00
parent 2eda3e36fe
commit 68b985346b
3 changed files with 20 additions and 10 deletions

View File

@ -545,6 +545,7 @@ class Session(nodes.Collector):
``Session`` collects the initial paths given as arguments to pytest. ``Session`` collects the initial paths given as arguments to pytest.
""" """
parent: None
Interrupted = Interrupted Interrupted = Interrupted
Failed = Failed Failed = Failed
# Set on the session by runner.pytest_sessionstart. # Set on the session by runner.pytest_sessionstart.

View File

@ -138,8 +138,13 @@ class Node(abc.ABC, metaclass=NodeMeta):
#: for methods not migrated to ``pathlib.Path`` yet, such as #: for methods not migrated to ``pathlib.Path`` yet, such as
#: :meth:`Item.reportinfo <pytest.Item.reportinfo>`. Will be deprecated in #: :meth:`Item.reportinfo <pytest.Item.reportinfo>`. Will be deprecated in
#: a future release, prefer using :attr:`path` instead. #: a future release, prefer using :attr:`path` instead.
name: str
parent: Node | None
config: Config
session: Session
fspath: LEGACY_PATH fspath: LEGACY_PATH
_nodeid: str
# Use __slots__ to make attribute access faster. # Use __slots__ to make attribute access faster.
# Note that __dict__ is still available. # Note that __dict__ is still available.
__slots__ = ( __slots__ = (
@ -156,7 +161,7 @@ class Node(abc.ABC, metaclass=NodeMeta):
def __init__( def __init__(
self, self,
name: str, name: str,
parent: Node | None = None, parent: Node | None,
config: Config | None = None, config: Config | None = None,
session: Session | None = None, session: Session | None = None,
fspath: LEGACY_PATH | None = None, fspath: LEGACY_PATH | None = None,
@ -200,13 +205,9 @@ class Node(abc.ABC, metaclass=NodeMeta):
#: Allow adding of extra keywords to use for matching. #: Allow adding of extra keywords to use for matching.
self.extra_keyword_matches: set[str] = set() self.extra_keyword_matches: set[str] = set()
if nodeid is not None: self._nodeid = self._make_nodeid(
assert "::()" not in nodeid name=self.name, parent=self.parent, given=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 #: A place where plugins can store information on the node for their
#: own use. #: own use.
@ -214,6 +215,15 @@ class Node(abc.ABC, metaclass=NodeMeta):
# Deprecated alias. Was never public. Can be removed in a few releases. # Deprecated alias. Was never public. Can be removed in a few releases.
self._store = self.stash self._store = self.stash
@classmethod
def _make_nodeid(cls, name: str, parent: Node | None, given: str | None) -> str:
if given is not None:
assert "::()" not in given
return given
else:
assert parent is not None
return f"{parent.nodeid}::{name}"
@classmethod @classmethod
def from_parent(cls, parent: Node, **kw) -> Self: def from_parent(cls, parent: Node, **kw) -> Self:
"""Public constructor for Nodes. """Public constructor for Nodes.
@ -598,7 +608,6 @@ class FSCollector(Collector, abc.ABC):
if nodeid and os.sep != SEP: if nodeid and os.sep != SEP:
nodeid = nodeid.replace(os.sep, SEP) nodeid = nodeid.replace(os.sep, SEP)
super().__init__( super().__init__(
name=name, name=name,
parent=parent, parent=parent,

View File

@ -31,7 +31,7 @@ def test_node_direct_construction_deprecated() -> None:
" for more details." " for more details."
), ),
): ):
nodes.Node(None, session=None) # type: ignore[arg-type] nodes.Node(None, parent=None, session=None) # type: ignore[arg-type]
def test_subclassing_both_item_and_collector_deprecated( def test_subclassing_both_item_and_collector_deprecated(