-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
internal: clean up getfslineno #6656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from bisect import bisect_right | ||
from types import CodeType | ||
from types import FrameType | ||
from typing import Any | ||
from typing import Iterator | ||
from typing import List | ||
from typing import Optional | ||
|
@@ -17,6 +18,7 @@ | |
|
||
import py | ||
|
||
from _pytest.compat import get_real_func | ||
from _pytest.compat import overload | ||
from _pytest.compat import TYPE_CHECKING | ||
|
||
|
@@ -282,14 +284,21 @@ def compile_( # noqa: F811 | |
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. | ||
If the source cannot be determined return ("", -1). | ||
|
||
The line number is 0-based. | ||
""" | ||
from .code import Code | ||
|
||
# xxx let decorators etc specify a sane ordering | ||
# NOTE: this used to be done in _pytest.compat.getfslineno, initially added | ||
# in 6ec13a2b9. It ("place_as") appears to be something very custom. | ||
obj = get_real_func(obj) | ||
if hasattr(obj, "place_as"): | ||
obj = obj.place_as | ||
|
||
try: | ||
code = Code(obj) | ||
except TypeError: | ||
|
@@ -298,18 +307,16 @@ def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int | |
except TypeError: | ||
return "", -1 | ||
|
||
fspath = fn and py.path.local(fn) or None | ||
fspath = fn and py.path.local(fn) or "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cannot really happen (becoming |
||
lineno = -1 | ||
if fspath: | ||
try: | ||
_, lineno = findsource(obj) | ||
except IOError: | ||
pass | ||
return fspath, lineno | ||
else: | ||
fspath = code.path | ||
lineno = code.firstlineno | ||
assert isinstance(lineno, int) | ||
return fspath, lineno | ||
return code.path, code.firstlineno | ||
|
||
|
||
# | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bluetech had to take
Any
here instead ofobj
, for theplace_as
attribute check.