Skip to content

optimize getting of line number in pytest discovery#26017

Open
vaclavHala wants to merge 1 commit into
microsoft:mainfrom
vaclavHala:optimize-pytest-discovery-lineno
Open

optimize getting of line number in pytest discovery#26017
vaclavHala wants to merge 1 commit into
microsoft:mainfrom
vaclavHala:optimize-pytest-discovery-lineno

Conversation

@vaclavHala

Copy link
Copy Markdown

The discovery for pytest is still quite a bit slower than running plain -m pytest --collect-only so I investigated a bit more and the next most significant (at least for our test suite) slowdown is caused by the pytest.Item.location call used to get line number of the test:

profile_location

This is because the .location returns tuple of path and line number, of which only the line number is used by the discovery, but computing the path is rather expensive. This PR proposes an optimization which uses internal pytest API in exactly the same way as the location function itself, but without also computing the path.

In particular, the location call hierarchy is:

This PR calls Code.from_function directly and only gets .firstlineno from it.

profile_firstlineno

Both profiles are captured with this PR cherry-picked to main branch prior to the discovery result compaction introduced in 5389068 which causes significant performance hit that overshadows the optimization done here, for that I created a separate PR #26016 with proposed fix.

This change makes the discovery about 20% faster for our suite, now almost on par with the raw pytest call

…xpensive call before falling back to location query
@eleanorjboyd eleanorjboyd requested a review from Copilot July 2, 2026 22:09
@eleanorjboyd eleanorjboyd self-assigned this Jul 2, 2026
@eleanorjboyd eleanorjboyd added bug Issue identified by VS Code Team member as probable bug skip tests Updates to tests unnecessary skip-issue-check labels Jul 2, 2026
@eleanorjboyd eleanorjboyd removed their assignment Jul 2, 2026
@eleanorjboyd eleanorjboyd self-requested a review July 2, 2026 22:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR speeds up pytest test discovery by avoiding the expensive pytest.Item.location path computation when only the line number is needed, using pytest’s internal Code.from_function(...).firstlineno as a fast-path with a fallback to the public API.

Changes:

  • Use _pytest._code.code.Code.from_function(...).firstlineno to obtain the test item’s line number without computing the path.
  • Add a fallback to test_case.location[1] when the internal API cannot be used.
Show a summary per file
File Description
python_files/vscode_pytest/init.py Optimizes line-number retrieval during pytest discovery by using an internal pytest API fast-path with fallback.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment on lines 24 to +26
import pytest
from _pytest.python import PyobjMixin
from _pytest._code.code import Code
# Before falling back to that high-level API, we try to use
# internal API to get just line number without the path.
lineno0 = Code.from_function(cast(PyobjMixin, test_case).obj).firstlineno
except:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug skip tests Updates to tests unnecessary skip-issue-check

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants