Add line number support for class nodes in pytest and unittest (#25593)
Plan: Add Location to TestClass Items for Run Functionality
This PR implements the fix for issue #25592 - TestClass items need a location (lineno) to be runnable and show the green arrow in VS Code’s Test Explorer.
Changes Completed:
Pytest:
- Update Python pytest adapter to add
linenofield to class nodes- Modify
create_class_node()inpython_files/vscode_pytest/__init__.pyto extract and include line number
- Update TypeScript types to allow
linenoon class nodes- Fix Python type checking errors
- Update expected test outputs
- Update all expected discovery outputs in
python_files/tests/pytestadapter/expected_discovery_test_output.pyto includelinenofor class nodes
- Add
find_class_line_number()helper functionUnittest:
- Update Python unittest adapter to add
linenofield to class nodes- Modify
build_test_tree()inpython_files/unittestadapter/pvsc_utils.pyto add line numbers
- Add
get_class_line()function to extract class line numbers- Make
linenooptional field inTestNodeTypedDict- Return empty string instead of “*” when line cannot be determined
- Update expected test outputs
- Update expected discovery outputs in
python_files/tests/unittestadapter/expected_discovery_test_output.pyto includelinenofor class nodes
- Add
find_class_line_number()helper functionTypeScript:
- Update TypeScript result resolver
- Modify
populateTestTree()insrc/client/testing/testController/common/utils.tsto handlelinenofor class nodes- Update type definitions to allow optional
linenoonDiscoveredTestNode
- Add ‘function’ to
DiscoveredTestTypeenumTest Results:
Pytest tests: ✅ 13/15 passing (2 failures expected - pytest-describe plugin not installed) Unittest tests: ✅ Verified class nodes include lineno
Code quality:
- ✅ Ruff formatting and linting passed
- ⚠️ Pyright has pre-existing errors (not introduced by this change)
Technical Implementation:
Pytest:
- Extract line number from pytest.Class objects using Python’s
inspect.getsourcelines()- Add
linenoas optional field to TestNode TypedDict (using NotRequired)- Return empty string when line number cannot be determined
Unittest:
- Extract line number from test_case.class using
inspect.getsourcelines()- Add
linenofield to class nodes during tree building- Use same optional field pattern in TestNode TypedDict
- Return empty string when line number cannot be determined (consistent with pytest)
TypeScript:
- Create VS Code ranges for class nodes when
linenois presentThis minimal change ensures TestClass items are treated similarly to test items in terms of having a location, making them runnable in the VS Code UI for both pytest and unittest.
Original prompt
fantastic analysis. Please implement this and keep in mind all this context you have; both how this should be done, what should be changed, and how tests are impacted
[Chronological Review: The conversation began with the user requesting a discussion about issue #25592 on GitHub, specifically regarding the need for TestClass items to have a location for proper functionality. The user then asked about the risks associated with this change and which tests would need updating. Following a positive response to the analysis, the user requested implementation of the discussed changes while considering the context of how it should be done, what should be changed, and how tests are impacted.] [Intent Mapping:
- “lets talk about this issue: https://github.com/microsoft/vscode-python/issues/25592 what needs to be done (likely) is TestClass items need a location so it shows as the green arrow and makes it runnable.” - User wants to discuss the issue and create a plan for implementation.
- “what risk does this change have? what tests will need to be updated?” - User seeks to understand the implications of the changes and necessary test updates.
- “fantastic analysis. Please implement this and keep in mind all this context you have; both how this should be done, what should be changed, and how tests are impacted.” - User requests implementation of the analysis with a focus on context and impact.]
[Technical Inventory:
- Repository: vscode-python
- Current Branch: supposed-spoonbill
- Default Branch: main
- Active Pull Request: support extra patching for doctest (https://github.com/microsoft/vscode-python/pull/25591)
- Issue Discussed: #25592 regarding TestClass items needing a location.]
[Code Archaeology: No specific files or code changes were discussed in detail, but the focus was on the functionality of TestClass items and their integration into the existing codebase.]
[Progress Assessment:
- Completed Tasks: Analysis of the issue and identification of necessary changes.
- Partially Complete Work: Implementation of the discussed changes is pending.
- Validated Outcomes: None yet, as implementation has not been executed.]
[Context Validation: All critical information for continuation is captured, including the need for changes to TestClass items and the implications for testing.]
[Recent Commands Analysis:
- Last Agent Commands: User requested a plan for the issue, an analysis of risks, and implementation of the discussed changes.
- Tool Results Summary: No specific tool results were returned as the conversation focused on planning and analysis rather than executing code or commands.
- Pre-Summary State: The agent was actively discussing the implementation of changes related to issue #25592 when the token budget was exceeded.
- Operation Context: The commands were executed to align with the user’s goals of addressing the issue and ensuring proper functionality of TestClass items.]
1. Conversation Overview: - Primary Objectives: The user aimed to discuss issue #25592, focusing on making TestClass items runnable by providing them with a location. The user also sought to understand the risks and necessary test updates related to this change. - Session Context: The conversation flowed from discussing the issue, analyzing risks, and finally requesting implementation of the proposed changes. - User Intent Evolution: The user transitioned from seeking information and analysis to requesting concrete implementation of the discussed changes.
- Technical Foundation:
- Repository: vscode-python
- Current Branch: supposed-spoonbill
- Default Branch: main
- Active Pull Request: support extra patching for doctest (https://github.com/microsoft/vscode-python/pull/25591)
- Codebase Status:
- No specific files were modified or discussed in detail, but the focus was on the functionality of TestClass items.
- Problem Resolution:
- Issues Encountered: The need for TestClass items to have a location for proper functionality.
- Solutions Implemented: Analysis of the issue and identification of necessary changes were completed, but implementation is pending.
- Debugging Context: No ongoing troubleshooting efforts were mentioned.
- Lessons Learned: Understanding the implications of changes on existing functionality and tests is crucial.
- Progress Tracking:
- Completed Tasks: Analysis of the issue and identification of necessary changes.
- Partially Complete Work: Implementation of the discussed changes is pending.
- Validated Outcomes: None yet, as implementation has not been executed.
- Active Work State:
- Current Focus: The user was focused on implementing changes related to issue #25592.
- Recent Context: The last few exchanges involved discussing the issue, analyzing risks, and planning for implementation.
- Working Code: No specific code snippets were discussed recently.
- Immediate Context: The specific problem being addressed was the need for TestClass items to have a locat…
Created from VS Code.
Co-authored-by: copilot-swe-agent[bot] 198982749+Copilot@users.noreply.github.com
Python extension for Visual Studio Code
A Visual Studio Code extension with rich support for the Python language (for all actively supported Python versions), providing access points for extensions to seamlessly integrate and offer support for IntelliSense (Pylance), debugging (Python Debugger), formatting, linting, code navigation, refactoring, variable explorer, test explorer, environment management (NEW Python Environments Extension).
Support for vscode.dev
The Python extension does offer some support when running on vscode.dev (which includes github.dev). This includes partial IntelliSense for open files in the editor.
Installed extensions
The Python extension will automatically install the following extensions by default to provide the best Python development experience in VS Code:
These extensions are optional dependencies, meaning the Python extension will remain fully functional if they fail to be installed. Any or all of these extensions can be disabled or uninstalled at the expense of some features. Extensions installed through the marketplace are subject to the Marketplace Terms of Use.
About the Python Environments Extension
You may now see that the Python Environments Extension is installed for you, but it may or may not be “enabled” in your VS Code experience. Enablement is controlled by the setting
"python.useEnvironmentsExtension": true(orfalse).true, you will manually opt in to using the Python Environments Extension for environment management.The Python Environments Extension is still under active development and experimentation. Its goal is to provide a dedicated view and improved workflows for creating, deleting, and switching between Python environments, as well as managing packages. If you have feedback, please let us know via issues.
Extensibility
The Python extension provides pluggable access points for extensions that extend various feature areas to further improve your Python development experience. These extensions are all optional and depend on your project configuration and preferences.
If you encounter issues with any of the listed extensions, please file an issue in its corresponding repo.
Quick start
Set up your environment
Select your Python interpreter by clicking on the status bar
Configure the debugger through the Debug Activity Bar
Configure tests by running the
Configure TestscommandJupyter Notebook quick start
The Python extension offers support for Jupyter notebooks via the Jupyter extension to provide you a great Python notebook experience in VS Code.
Install the Jupyter extension.
Open or create a Jupyter Notebook file (.ipynb) and start coding in our Notebook Editor!
For more information you can:
Useful commands
Open the Command Palette (Command+Shift+P on macOS and Ctrl+Shift+P on Windows/Linux) and type in one of the following commands:
Python: Select InterpreterPython: Start Terminal REPLPython: Run Python File in TerminalRun Python File in Terminal.Python: Configure TestsTo see all available Python commands, open the Command Palette and type
Python. For Jupyter extension commands, just typeJupyter.Feature details
Learn more about the rich features of the Python extension:
Supported locales
The extension is available in multiple languages:
de,en,es,fa,fr,it,ja,ko-kr,nl,pl,pt-br,ru,tr,zh-cn,zh-twQuestions, issues, feature requests, and contributions
Data and telemetry
The Microsoft Python Extension for Visual Studio Code collects usage data and sends it to Microsoft to help improve our products and services. Read our privacy statement to learn more. This extension respects the
telemetry.telemetryLevelsetting which you can learn more about at https://code.visualstudio.com/docs/supporting/faq#_how-to-disable-telemetry-reporting.