Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A bit more typing around Node #6167

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprExceptionInfo
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 @@ -78,11 +79,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 @@ -91,14 +92,20 @@ def __init__(
self.parent = parent

#: the pytest config object
self.config = config or parent.config
if config:
bluetech marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -119,6 +126,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 @@ -182,7 +191,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 @@ -263,7 +272,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):
RonnyPfannschmidt marked this conversation as resolved.
Show resolved Hide resolved
current = current.parent
return current
Expand Down