A bit more typing around Node

This commit is contained in:
Daniel Hahler 2019-11-09 22:43:43 +01:00
parent abcedd6095
commit 3ef8aa8173
1 changed files with 20 additions and 11 deletions

View File

@ -16,6 +16,7 @@ from _pytest._code.code import ExceptionChainRepr
from _pytest._code.code import ExceptionInfo from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprExceptionInfo from _pytest._code.code import ReprExceptionInfo
from _pytest.compat import getfslineno from _pytest.compat import getfslineno
from _pytest.config import Config
from _pytest.fixtures import FixtureDef from _pytest.fixtures import FixtureDef
from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureLookupError
from _pytest.fixtures import FixtureLookupErrorRepr from _pytest.fixtures import FixtureLookupErrorRepr
@ -78,11 +79,11 @@ class Node:
def __init__( def __init__(
self, self,
name, name,
parent=None, parent: Optional["Node"] = None,
config=None, config: Optional[Config] = None,
session: Optional["Session"] = None, session: Optional["Session"] = None,
fspath=None, fspath: Optional[py.path.local] = None,
nodeid=None, nodeid: Optional[str] = None,
) -> None: ) -> None:
#: a unique name within the scope of the parent node #: a unique name within the scope of the parent node
self.name = name self.name = name
@ -91,14 +92,20 @@ class Node:
self.parent = parent self.parent = parent
#: the pytest config object #: the pytest config object
self.config = config or parent.config 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 #: the session this node is part of
if session is None: if session:
assert parent.session is not None
self.session = parent.session
else:
self.session = 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) #: filesystem path where this node was collected from (can be None)
self.fspath = fspath or getattr(parent, "fspath", None) self.fspath = fspath or getattr(parent, "fspath", None)
@ -119,6 +126,8 @@ class Node:
assert "::()" not in nodeid assert "::()" not in nodeid
self._nodeid = nodeid self._nodeid = nodeid
else: else:
if not self.parent:
raise TypeError("nodeid or parent must be provided")
self._nodeid = self.parent.nodeid self._nodeid = self.parent.nodeid
if self.name != "()": if self.name != "()":
self._nodeid += "::" + self.name self._nodeid += "::" + self.name
@ -182,7 +191,7 @@ class Node:
""" return list of all parent collectors up to self, """ return list of all parent collectors up to self,
starting from root of collection tree. """ starting from root of collection tree. """
chain = [] chain = []
item = self item = self # type: Optional[Node]
while item is not None: while item is not None:
chain.append(item) chain.append(item)
item = item.parent item = item.parent
@ -263,7 +272,7 @@ class Node:
def getparent(self, cls): def getparent(self, cls):
""" get the next parent node (including ourself) """ get the next parent node (including ourself)
which is an instance of the given class""" which is an instance of the given class"""
current = self current = self # type: Optional[Node]
while current and not isinstance(current, cls): while current and not isinstance(current, cls):
current = current.parent current = current.parent
return current return current