typing: fix getfslineno

Closes https://github.com/pytest-dev/pytest/pull/6590.
This commit is contained in:
Daniel Hahler 2020-02-03 18:50:12 +01:00
parent 61f2a26675
commit dab90ef726
2 changed files with 7 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import warnings
from bisect import bisect_right from bisect import bisect_right
from types import CodeType from types import CodeType
from types import FrameType from types import FrameType
from typing import Any
from typing import Iterator from typing import Iterator
from typing import List from typing import List
from typing import Optional from typing import Optional
@ -283,7 +284,7 @@ def compile_( # noqa: F811
return s.compile(filename, mode, flags, _genframe=_genframe) return s.compile(filename, mode, flags, _genframe=_genframe)
def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int]: def getfslineno(obj: Any) -> Tuple[Union[str, py.path.local], int]:
""" Return source location (path, lineno) for the given object. """ Return source location (path, lineno) for the given object.
If the source cannot be determined return ("", -1). If the source cannot be determined return ("", -1).
@ -306,18 +307,16 @@ def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int
except TypeError: except TypeError:
return "", -1 return "", -1
fspath = fn and py.path.local(fn) or None fspath = fn and py.path.local(fn) or ""
lineno = -1 lineno = -1
if fspath: if fspath:
try: try:
_, lineno = findsource(obj) _, lineno = findsource(obj)
except IOError: except IOError:
pass pass
return fspath, lineno
else: else:
fspath = code.path return code.path, code.firstlineno
lineno = code.firstlineno
assert isinstance(lineno, int)
return fspath, lineno
# #

View File

@ -308,9 +308,9 @@ def get_real_method(obj, holder):
def getfslineno(obj) -> Tuple[Union[str, py.path.local], int]: def getfslineno(obj) -> Tuple[Union[str, py.path.local], int]:
"""(**Deprecated**, use _pytest._code.source.getfslineno directly)""" """(**Deprecated**, use _pytest._code.source.getfslineno directly)"""
from _pytest._code.source import getfslineno import _pytest._code.source
return getfslineno(obj) return _pytest._code.source.getfslineno(obj)
def getimfunc(func): def getimfunc(func):