目录
Eleanor Boyd

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 lineno field to class nodes
  • Modify create_class_node() in python_files/vscode_pytest/__init__.py to extract and include line number
    • Update TypeScript types to allow lineno on 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.py to include lineno for class nodes
    • Add find_class_line_number() helper function

Unittest:

  • Update Python unittest adapter to add lineno field to class nodes
  • Modify build_test_tree() in python_files/unittestadapter/pvsc_utils.py to add line numbers
    • Add get_class_line() function to extract class line numbers
    • Make lineno optional field in TestNode TypedDict
  • 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.py to include lineno for class nodes
    • Add find_class_line_number() helper function

TypeScript:

  • Update TypeScript result resolver
  • Modify populateTestTree() in src/client/testing/testController/common/utils.ts to handle lineno for class nodes
  • Update type definitions to allow optional lineno on DiscoveredTestNode
    • Add ‘function’ to DiscoveredTestType enum

Test 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 lineno as 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 lineno field 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 lineno is present

This 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:

  1. “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.
  2. “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.
  3. “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.
  1. Technical Foundation:
  1. Codebase Status:
  • No specific files were modified or discussed in detail, but the focus was on the functionality of TestClass items.
  1. 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.
  1. 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.
  1. 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

4天前7867次提交
目录README.md

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 (or false).

  • If you set this setting to true, you will manually opt in to using the Python Environments Extension for environment management.
  • If you do not have this setting specified, you may be randomly assigned to have it turned on as we roll it out until it becomes the default experience for all users.

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 Tests command

Jupyter 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:

Command Description
Python: Select Interpreter Switch between Python interpreters, versions, and environments.
Python: Start Terminal REPL Start an interactive Python REPL using the selected interpreter in the VS Code terminal.
Python: Run Python File in Terminal Runs the active Python file in the VS Code terminal. You can also run a Python file by right-clicking on the file and selecting Run Python File in Terminal.
Python: Configure Tests Select a test framework and configure it to display the Test Explorer.

To see all available Python commands, open the Command Palette and type Python. For Jupyter extension commands, just type Jupyter.

Feature details

Learn more about the rich features of the Python extension:

  • IntelliSense: Edit your code with auto-completion, code navigation, syntax checking and more.
  • Linting: Get additional code analysis with Pylint, Flake8 and more.
  • Code formatting: Format your code with black, autopep or yapf.
  • Debugging: Debug your Python scripts, web apps, remote or multi-threaded processes.
  • Testing: Run and debug tests through the Test Explorer with unittest or pytest.
  • Jupyter Notebooks: Create and edit Jupyter Notebooks, add and run code cells, render plots, visualize variables through the variable explorer, visualize dataframes with the data viewer, and more.
  • Environments: Automatically activate and switch between virtualenv, venv, pipenv, conda and pyenv environments.
  • Refactoring: Restructure your Python code with variable extraction and method extraction. Additionally, there is componentized support to enable additional refactoring, such as import sorting, through extensions including isort and Ruff.

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-tw

Questions, issues, feature requests, and contributions

  • If you have a question about how to accomplish something with the extension, please ask on our Discussions page.
  • If you come across a problem with the extension, please file an issue.
  • Contributions are always welcome! Please see our contributing guide for more details.
  • Any and all feedback is appreciated and welcome!
    • If someone has already filed an issue that encompasses your feedback, please leave a 👍/👎 reaction on the issue.
    • Otherwise please start a new discussion.
  • If you’re interested in the development of the extension, you can read about our development process.

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.telemetryLevel setting which you can learn more about at https://code.visualstudio.com/docs/supporting/faq#_how-to-disable-telemetry-reporting.

    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号