Skip to content

Commit dd55ef8

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

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/_pytest/nodes.py

Lines changed: 22 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,21 @@ 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
103+
if session:
104+
self.session = session # type: Session
100105
else:
101-
self.session = session
106+
if not parent:
107+
raise TypeError("session or parent must be provided")
108+
self.session = parent.session
109+
assert self.session
102110

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

118-
if nodeid is not None:
126+
if nodeid:
119127
assert "::()" not in nodeid
120128
self._nodeid = nodeid
121129
else:
130+
if not self.parent:
131+
raise TypeError("nodeid or parent must be provided")
122132
self._nodeid = self.parent.nodeid
123133
if self.name != "()":
124134
self._nodeid += "::" + self.name
@@ -182,7 +192,7 @@ def listchain(self):
182192
""" return list of all parent collectors up to self,
183193
starting from root of collection tree. """
184194
chain = []
185-
item = self
195+
item = self # type: Optional[Node]
186196
while item is not None:
187197
chain.append(item)
188198
item = item.parent
@@ -263,7 +273,7 @@ def addfinalizer(self, fin):
263273
def getparent(self, cls):
264274
""" get the next parent node (including ourself)
265275
which is an instance of the given class"""
266-
current = self
276+
current = self # type: Optional[Node]
267277
while current and not isinstance(current, cls):
268278
current = current.parent
269279
return current

0 commit comments

Comments
 (0)