From b14d3b81f4fa76a84a5f8c6cf4fe30890c31edde Mon Sep 17 00:00:00 2001 From: Eleanor Boyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Thu, 15 May 2025 06:30:21 -0700 Subject: [PATCH] fix regression with import packaging for branch coverage (#25070) fixes https://github.com/Microsoft/vscode-python/issues/25044 --- python_files/unittestadapter/execution.py | 9 +++++++-- python_files/vscode_pytest/__init__.py | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/python_files/unittestadapter/execution.py b/python_files/unittestadapter/execution.py index 8df2f279aa71..176703c20ce0 100644 --- a/python_files/unittestadapter/execution.py +++ b/python_files/unittestadapter/execution.py @@ -12,8 +12,6 @@ from types import TracebackType from typing import Dict, List, Optional, Set, Tuple, Type, Union -from packaging.version import Version - # Adds the scripts directory to the PATH as a workaround for enabling shell for test execution. path_var_name = "PATH" if "PATH" in os.environ else "Path" os.environ[path_var_name] = ( @@ -326,6 +324,13 @@ def send_run_data(raw_data, test_run_pipe): ) import coverage + # insert "python_files/lib/python" into the path so packaging can be imported + python_files_dir = pathlib.Path(__file__).parent.parent + bundled_dir = pathlib.Path(python_files_dir / "lib" / "python") + sys.path.append(os.fspath(bundled_dir)) + + from packaging.version import Version + coverage_version = Version(coverage.__version__) # only include branches if coverage version is 7.7.0 or greater (as this was when the api saves) if coverage_version >= Version("7.7.0"): diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 649e5bc59058..18469cd0627f 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -13,7 +13,6 @@ from typing import TYPE_CHECKING, Any, Dict, Generator, Literal, TypedDict import pytest -from packaging.version import Version if TYPE_CHECKING: from pluggy import Result @@ -443,6 +442,13 @@ def pytest_sessionfinish(session, exitstatus): # load the report and build the json result to return import coverage + # insert "python_files/lib/python" into the path so packaging can be imported + python_files_dir = pathlib.Path(__file__).parent.parent + bundled_dir = pathlib.Path(python_files_dir / "lib" / "python") + sys.path.append(os.fspath(bundled_dir)) + + from packaging.version import Version + coverage_version = Version(coverage.__version__) global INCLUDE_BRANCHES # only include branches if coverage version is 7.7.0 or greater (as this was when the api saves)