3939if TYPE_CHECKING :
4040 from typing import Type
4141
42- # Imported here due to circular import.
43- from _pytest .main import Session # noqa: F401
42+ from _pytest . main import Session
43+ from _pytest .warning_types import PytestWarning
4444
4545SEP = "/"
4646
@@ -104,9 +104,9 @@ class Node(metaclass=NodeMeta):
104104 def __init__ (
105105 self ,
106106 name : str ,
107- parent : Optional [" Node" ] = None ,
107+ parent : " Optional[Node]" = None ,
108108 config : Optional [Config ] = None ,
109- session : Optional [" Session" ] = None ,
109+ session : " Optional[Session]" = None ,
110110 fspath : Optional [py .path .local ] = None ,
111111 nodeid : Optional [str ] = None ,
112112 ) -> None :
@@ -187,7 +187,7 @@ def ihook(self):
187187 def __repr__ (self ) -> str :
188188 return "<{} {}>" .format (self .__class__ .__name__ , getattr (self , "name" , None ))
189189
190- def warn (self , warning ) :
190+ def warn (self , warning : "PytestWarning" ) -> None :
191191 """Issue a warning for this item.
192192
193193 Warnings will be displayed after the test session, unless explicitly suppressed
@@ -212,11 +212,9 @@ def warn(self, warning):
212212 )
213213 )
214214 path , lineno = get_fslocation_from_item (self )
215+ assert lineno is not None
215216 warnings .warn_explicit (
216- warning ,
217- category = None ,
218- filename = str (path ),
219- lineno = lineno + 1 if lineno is not None else None ,
217+ warning , category = None , filename = str (path ), lineno = lineno + 1 ,
220218 )
221219
222220 # methods for ordering nodes
@@ -396,24 +394,26 @@ def repr_failure(
396394
397395
398396def get_fslocation_from_item (
399- item : "Item " ,
397+ node : "Node " ,
400398) -> Tuple [Union [str , py .path .local ], Optional [int ]]:
401- """Tries to extract the actual location from an item , depending on available attributes:
399+ """Tries to extract the actual location from a node , depending on available attributes:
402400
403- * "fslocation ": a pair (path, lineno)
404- * "obj": a Python object that the item wraps.
401+ * "location ": a pair (path, lineno)
402+ * "obj": a Python object that the node wraps.
405403 * "fspath": just a path
406404
407405 :rtype: a tuple of (str|LocalPath, int) with filename and line number.
408406 """
409- try :
410- return item .location [:2 ]
411- except AttributeError :
412- pass
413- obj = getattr (item , "obj" , None )
407+ # See Item.location.
408+ location = getattr (
409+ node , "location" , None
410+ ) # type: Optional[Tuple[str, Optional[int], str]]
411+ if location is not None :
412+ return location [:2 ]
413+ obj = getattr (node , "obj" , None )
414414 if obj is not None :
415415 return getfslineno (obj )
416- return getattr (item , "fspath" , "unknown location" ), - 1
416+ return getattr (node , "fspath" , "unknown location" ), - 1
417417
418418
419419class Collector (Node ):
0 commit comments