Skip to content

Commit 7e2d97e

Browse files
committed
try getting pytest case lineno by internal API to avoid unnecessary expensive call before falling back to location query
1 parent 3434815 commit 7e2d97e

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

python_files/vscode_pytest/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
)
2323

2424
import pytest
25+
from _pytest.python import PyobjMixin
26+
from _pytest._code.code import Code
2527

2628
if TYPE_CHECKING:
2729
from pluggy import Result
@@ -842,8 +844,18 @@ def create_test_node(
842844
Keyword arguments:
843845
test_case -- the pytest test case.
844846
"""
847+
848+
try:
849+
# test_case.location returns tuple of path and line number
850+
# Obtaining the path is expensive and we do not need it anyway.
851+
# Before falling back to that high-level API, we try to use
852+
# internal API to get just line number without the path.
853+
lineno0 = Code.from_function(cast(PyobjMixin, test_case).obj).firstlineno
854+
except:
855+
lineno0 = test_case.location[1]
856+
845857
test_case_loc: str = (
846-
str(test_case.location[1] + 1) if (test_case.location[1] is not None) else ""
858+
str(lineno0 + 1) if (lineno0 is not None) else ""
847859
)
848860
absolute_test_id = get_absolute_test_id(test_case.nodeid, get_node_path(test_case))
849861
return {
@@ -1261,3 +1273,4 @@ def pytest_plugin_registered(plugin: object, manager: pytest.PytestPluginManager
12611273
and not manager.hasplugin(plugin_name)
12621274
):
12631275
manager.register(DeferPlugin(), name=plugin_name)
1276+

0 commit comments

Comments
 (0)