4242
4343 # Imported here due to circular import.
4444 from _pytest .main import Session
45+ from _pytest .warning_types import PytestWarning
46+
4547
4648SEP = "/"
4749
@@ -118,9 +120,9 @@ class Node(metaclass=NodeMeta):
118120 def __init__ (
119121 self ,
120122 name : str ,
121- parent : Optional [" Node" ] = None ,
123+ parent : " Optional[Node]" = None ,
122124 config : Optional [Config ] = None ,
123- session : Optional [" Session" ] = None ,
125+ session : " Optional[Session]" = None ,
124126 fspath : Optional [py .path .local ] = None ,
125127 nodeid : Optional [str ] = None ,
126128 ) -> None :
@@ -201,7 +203,7 @@ def ihook(self):
201203 def __repr__ (self ) -> str :
202204 return "<{} {}>" .format (self .__class__ .__name__ , getattr (self , "name" , None ))
203205
204- def warn (self , warning ) :
206+ def warn (self , warning : "PytestWarning" ) -> None :
205207 """Issue a warning for this item.
206208
207209 Warnings will be displayed after the test session, unless explicitly suppressed
@@ -226,11 +228,9 @@ def warn(self, warning):
226228 )
227229 )
228230 path , lineno = get_fslocation_from_item (self )
231+ assert lineno is not None
229232 warnings .warn_explicit (
230- warning ,
231- category = None ,
232- filename = str (path ),
233- lineno = lineno + 1 if lineno is not None else None ,
233+ warning , category = None , filename = str (path ), lineno = lineno + 1 ,
234234 )
235235
236236 # methods for ordering nodes
@@ -417,24 +417,26 @@ def repr_failure(
417417
418418
419419def get_fslocation_from_item (
420- item : "Item " ,
420+ node : "Node " ,
421421) -> Tuple [Union [str , py .path .local ], Optional [int ]]:
422- """Tries to extract the actual location from an item , depending on available attributes:
422+ """Tries to extract the actual location from a node , depending on available attributes:
423423
424- * "fslocation ": a pair (path, lineno)
425- * "obj": a Python object that the item wraps.
424+ * "location ": a pair (path, lineno)
425+ * "obj": a Python object that the node wraps.
426426 * "fspath": just a path
427427
428428 :rtype: a tuple of (str|LocalPath, int) with filename and line number.
429429 """
430- try :
431- return item .location [:2 ]
432- except AttributeError :
433- pass
434- obj = getattr (item , "obj" , None )
430+ # See Item.location.
431+ location = getattr (
432+ node , "location" , None
433+ ) # type: Optional[Tuple[str, Optional[int], str]]
434+ if location is not None :
435+ return location [:2 ]
436+ obj = getattr (node , "obj" , None )
435437 if obj is not None :
436438 return getfslineno (obj )
437- return getattr (item , "fspath" , "unknown location" ), - 1
439+ return getattr (node , "fspath" , "unknown location" ), - 1
438440
439441
440442class Collector (Node ):
0 commit comments