Skip to content

Commit

Permalink
A bit more typing around Node (#6167)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored Nov 12, 2019
2 parents b352e34 + 3ef8aa8 commit dad4985
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from _pytest._code.code import ReprExceptionInfo
from _pytest.compat import cached_property
from _pytest.compat import getfslineno
from _pytest.config import Config
from _pytest.fixtures import FixtureDef
from _pytest.fixtures import FixtureLookupError
from _pytest.fixtures import FixtureLookupErrorRepr
Expand Down Expand Up @@ -79,11 +80,11 @@ class Node:
def __init__(
self,
name,
parent=None,
config=None,
parent: Optional["Node"] = None,
config: Optional[Config] = None,
session: Optional["Session"] = None,
fspath=None,
nodeid=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
Expand All @@ -92,14 +93,20 @@ def __init__(
self.parent = parent

#: 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
if session is None:
assert parent.session is not None
self.session = parent.session
else:
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)
Expand All @@ -120,6 +127,8 @@ def __init__(
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
Expand Down Expand Up @@ -183,7 +192,7 @@ def listchain(self):
""" return list of all parent collectors up to self,
starting from root of collection tree. """
chain = []
item = self
item = self # type: Optional[Node]
while item is not None:
chain.append(item)
item = item.parent
Expand Down Expand Up @@ -264,7 +273,7 @@ def addfinalizer(self, fin):
def getparent(self, cls):
""" get the next parent node (including ourself)
which is an instance of the given class"""
current = self
current = self # type: Optional[Node]
while current and not isinstance(current, cls):
current = current.parent
return current
Expand Down

0 comments on commit dad4985

Please sign in to comment.