Skip to content

Commit fed319e

Browse files
committed
A bit more typing around Node
1 parent abcedd6 commit fed319e

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/_pytest/nodes.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from _pytest._code.code import ExceptionInfo
1717
from _pytest._code.code import ReprExceptionInfo
1818
from _pytest.compat import getfslineno
19+
from _pytest.config import Config
1920
from _pytest.fixtures import FixtureDef
2021
from _pytest.fixtures import FixtureLookupError
2122
from _pytest.fixtures import FixtureLookupErrorRepr
@@ -78,11 +79,11 @@ class Node:
7879
def __init__(
7980
self,
8081
name,
81-
parent=None,
82-
config=None,
82+
parent: Optional["Node"] = None,
83+
config: Optional[Config] = None,
8384
session: Optional["Session"] = None,
84-
fspath=None,
85-
nodeid=None,
85+
fspath: Optional[py.path.local] = None,
86+
nodeid: Optional[str] = None,
8687
) -> None:
8788
#: a unique name within the scope of the parent node
8889
self.name = name
@@ -91,14 +92,20 @@ def __init__(
9192
self.parent = parent
9293

9394
#: the pytest config object
94-
self.config = config or parent.config
95+
if config:
96+
self.config = config
97+
else:
98+
if not parent:
99+
raise TypeError("config or parent must be provided")
100+
self.config = parent.config
95101

96102
#: the session this node is part of
97-
if session is None:
98-
assert parent.session is not None
99-
self.session = parent.session
100-
else:
103+
if session:
101104
self.session = session
105+
else:
106+
if not parent:
107+
raise TypeError("session or parent must be provided")
108+
self.session = parent.session
102109

103110
#: filesystem path where this node was collected from (can be None)
104111
self.fspath = fspath or getattr(parent, "fspath", None)
@@ -115,10 +122,12 @@ def __init__(
115122
# used for storing artificial fixturedefs for direct parametrization
116123
self._name2pseudofixturedef = {} # type: Dict[str, FixtureDef]
117124

118-
if nodeid is not None:
125+
if nodeid:
119126
assert "::()" not in nodeid
120127
self._nodeid = nodeid
121128
else:
129+
if not self.parent:
130+
raise TypeError("nodeid or parent must be provided")
122131
self._nodeid = self.parent.nodeid
123132
if self.name != "()":
124133
self._nodeid += "::" + self.name
@@ -182,7 +191,7 @@ def listchain(self):
182191
""" return list of all parent collectors up to self,
183192
starting from root of collection tree. """
184193
chain = []
185-
item = self
194+
item = self # type: Optional[Node]
186195
while item is not None:
187196
chain.append(item)
188197
item = item.parent
@@ -263,7 +272,7 @@ def addfinalizer(self, fin):
263272
def getparent(self, cls):
264273
""" get the next parent node (including ourself)
265274
which is an instance of the given class"""
266-
current = self
275+
current = self # type: Optional[Node]
267276
while current and not isinstance(current, cls):
268277
current = current.parent
269278
return current

0 commit comments

Comments
 (0)