From 566091fd9aa73cd610bc74da66b11ef6dbbb4ddd Mon Sep 17 00:00:00 2001 From: mayeut Date: Mon, 25 May 2026 07:10:53 +0200 Subject: [PATCH 1/2] fix: add missing TYPE_CHECKING guard in tests `typing_extensions` is not part of the test dependency group. When re-using an existing environment for Python 3.9 tests (default with the current noxfile), tests might fail at collection time because of the missing `typing_extensions` module. --- .pre-commit-config.yaml | 1 + tests/test_dependency_groups.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 377664c11..220d0371b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,7 @@ repos: - tomli - tomli_w - types-invoke + - 'typing_extensions>=4.5' - httpx - repo: https://github.com/crate-ci/typos diff --git a/tests/test_dependency_groups.py b/tests/test_dependency_groups.py index de8f6caf6..317a89357 100644 --- a/tests/test_dependency_groups.py +++ b/tests/test_dependency_groups.py @@ -1,9 +1,8 @@ from __future__ import annotations import re -import sys import unittest.mock -from typing import Any +from typing import TYPE_CHECKING, Any import pytest @@ -18,12 +17,15 @@ from packaging.errors import ExceptionGroup from packaging.requirements import Requirement -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias +if TYPE_CHECKING: + import sys -GroupsTable: TypeAlias = "dict[str, list[str | dict[str, str]]]" + if sys.version_info >= (3, 10): + from typing import TypeAlias + else: + from typing_extensions import TypeAlias + + GroupsTable: TypeAlias = "dict[str, list[str | dict[str, str]]]" def _group_contains( From 7286e75134f221bcbdfa9f18ca594cfef2bdf605 Mon Sep 17 00:00:00 2001 From: mayeut Date: Mon, 25 May 2026 07:12:00 +0200 Subject: [PATCH 2/2] chore: move `TypeGuard` import in TYPE_CHECKING block --- src/packaging/specifiers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/packaging/specifiers.py b/src/packaging/specifiers.py index 09ab94f86..e4d06dbb8 100644 --- a/src/packaging/specifiers.py +++ b/src/packaging/specifiers.py @@ -12,7 +12,6 @@ import abc import re -import sys import typing from typing import ( TYPE_CHECKING, @@ -35,14 +34,15 @@ from .utils import canonicalize_version from .version import InvalidVersion, Version -if sys.version_info >= (3, 10): - from typing import TypeGuard # pragma: no cover -elif TYPE_CHECKING: - from typing_extensions import TypeGuard - if TYPE_CHECKING: + import sys from collections.abc import Iterable, Iterator, Sequence + if sys.version_info >= (3, 10): + from typing import TypeGuard + else: + from typing_extensions import TypeGuard + from ._ranges import VersionRange