Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard Type Hint Imports Used Only Inside Annotations #12488

Closed
1 change: 1 addition & 0 deletions docs/html/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sphinx configuration file for pip's documentation."""
from __future__ import annotations

import glob
import os
Expand Down
7 changes: 5 additions & 2 deletions docs/pip_sphinxext.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""pip sphinx extensions"""
from __future__ import annotations

import optparse
import pathlib
import re
import sys
from textwrap import dedent
from typing import Dict, Iterable, Iterator, List, Optional, Union
from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, Optional, Union

from docutils import nodes, statemachine
from docutils.parsers import rst
from docutils.statemachine import StringList, ViewList
from sphinx.application import Sphinx

from pip._internal.cli import cmdoptions
from pip._internal.commands import commands_dict, create_command
from pip._internal.req.req_file import SUPPORTED_OPTIONS

if TYPE_CHECKING:
from sphinx.application import Sphinx


class PipNewsInclude(rst.Directive):
required_arguments = 1
Expand Down
1 change: 1 addition & 0 deletions news/12488.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No longer import type hint only objects at runtime
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Automation using nox.
"""
from __future__ import annotations

import argparse
import glob
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ select = [
"W",
"RUF100",
"UP032",
"TCH",
]

[tool.ruff.isort]
# We need to explicitly make pip "first party" as it's imported by code in
# the docs and tests directories.
known-first-party = ["pip"]
known-third-party = ["pip._vendor"]
required-imports = ["from __future__ import annotations"]

[tool.ruff.mccabe]
max-complexity = 33 # default is 10
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import sys

from setuptools import setup
Expand Down
2 changes: 2 additions & 0 deletions src/pip/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import List, Optional

__version__ = "24.0.dev0"
Expand Down
2 changes: 2 additions & 0 deletions src/pip/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import sys

Expand Down
1 change: 1 addition & 0 deletions src/pip/__pip-runner__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

# /!\ This version compatibility check section must be Python 2 compatible. /!\
from __future__ import annotations

import sys

Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import List, Optional

from pip._internal.utils import _log
Expand Down
4 changes: 3 additions & 1 deletion src/pip/_internal/build_env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Build Environment used for isolation during sdist building
"""
from __future__ import annotations

import logging
import os
Expand All @@ -8,7 +9,6 @@
import sys
import textwrap
from collections import OrderedDict
from types import TracebackType
from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Type, Union

from pip._vendor.certifi import where
Expand All @@ -23,6 +23,8 @@
from pip._internal.utils.temp_dir import TempDirectory, tempdir_kinds

if TYPE_CHECKING:
from types import TracebackType

from pip._internal.index.package_finder import PackageFinder

logger = logging.getLogger(__name__)
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Cache Management
"""
from __future__ import annotations

import hashlib
import json
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/autocompletion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Logic that powers autocompletion installed by ``pip completion``.
"""
from __future__ import annotations

import optparse
import os
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/base_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Base Command class, and related routines"""
from __future__ import annotations

import functools
import logging
Expand Down
7 changes: 5 additions & 2 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# The following comment should be removed at some point in the future.
# mypy: strict-optional=False
from __future__ import annotations

import importlib.util
import logging
Expand All @@ -17,11 +18,10 @@
from functools import partial
from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values
from textwrap import dedent
from typing import Any, Callable, Dict, Optional, Tuple
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple

from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.cli.parser import ConfigOptionParser
from pip._internal.exceptions import CommandError
from pip._internal.locations import USER_CACHE_DIR, get_src_prefix
from pip._internal.models.format_control import FormatControl
Expand All @@ -30,6 +30,9 @@
from pip._internal.utils.hashes import STRONG_HASHES
from pip._internal.utils.misc import strtobool

if TYPE_CHECKING:
from pip._internal.cli.parser import ConfigOptionParser

logger = logging.getLogger(__name__)


Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/command_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import ExitStack, contextmanager
from typing import ContextManager, Generator, TypeVar

Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Primary application entrypoint.
"""
from __future__ import annotations

import locale
import logging
import os
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/main_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A single place for constructing and exposing the main parser
"""
from __future__ import annotations

import os
import subprocess
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Base option parser setup"""
from __future__ import annotations

import logging
import optparse
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/progress_bars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import functools
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple

Expand Down
14 changes: 8 additions & 6 deletions src/pip/_internal/cli/req_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
needing download / PackageFinder capability don't unnecessarily import the
PackageFinder machinery and all its vendored dependencies, etc.
"""
from __future__ import annotations

import logging
import os
import sys
from functools import partial
from optparse import Values
from typing import TYPE_CHECKING, Any, List, Optional, Tuple

from pip._internal.cache import WheelCache
from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.command_context import CommandContextMixIn
from pip._internal.exceptions import CommandError, PreviousBuildDirError
from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.models.target_python import TargetPython
from pip._internal.network.session import PipSession
from pip._internal.operations.build.build_tracker import BuildTracker
from pip._internal.operations.prepare import RequirementPreparer
from pip._internal.req.constructors import (
install_req_from_editable,
Expand All @@ -31,8 +28,6 @@
install_req_from_req_string,
)
from pip._internal.req.req_file import parse_requirements
from pip._internal.req.req_install import InstallRequirement
from pip._internal.resolution.base import BaseResolver
from pip._internal.self_outdated_check import pip_self_version_check
from pip._internal.utils.temp_dir import (
TempDirectory,
Expand All @@ -42,8 +37,15 @@
from pip._internal.utils.virtualenv import running_under_virtualenv

if TYPE_CHECKING:
from optparse import Values
from ssl import SSLContext

from pip._internal.cache import WheelCache
from pip._internal.models.target_python import TargetPython
from pip._internal.operations.build.build_tracker import BuildTracker
from pip._internal.req.req_install import InstallRequirement
from pip._internal.resolution.base import BaseResolver

logger = logging.getLogger(__name__)


Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/spinners.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import contextlib
import itertools
import logging
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/cli/status_codes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

SUCCESS = 0
ERROR = 1
UNKNOWN_ERROR = 2
Expand Down
6 changes: 4 additions & 2 deletions src/pip/_internal/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Package containing all pip commands
"""
from __future__ import annotations

import importlib
from collections import namedtuple
from typing import Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Dict, Optional

from pip._internal.cli.base_command import Command
if TYPE_CHECKING:
from pip._internal.cli.base_command import Command

CommandInfo = namedtuple("CommandInfo", "module_path, class_name, summary")

Expand Down
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/cache.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

import os
import textwrap
from optparse import Values
from typing import Any, List
from typing import TYPE_CHECKING, Any, List

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.exceptions import CommandError, PipError
from pip._internal.utils import filesystem
from pip._internal.utils.logging import getLogger

if TYPE_CHECKING:
from optparse import Values

logger = getLogger(__name__)


Expand Down
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
from optparse import Values
from typing import List
from typing import TYPE_CHECKING, List

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
Expand All @@ -11,6 +12,9 @@
)
from pip._internal.utils.misc import write_output

if TYPE_CHECKING:
from optparse import Values

logger = logging.getLogger(__name__)


Expand Down
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

import sys
import textwrap
from optparse import Values
from typing import List
from typing import TYPE_CHECKING, List

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.utils.misc import get_prog

if TYPE_CHECKING:
from optparse import Values

BASE_COMPLETION = """
# pip {shell} completion start{script}# pip {shell} completion end
"""
Expand Down
8 changes: 6 additions & 2 deletions src/pip/_internal/commands/configuration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

import logging
import os
import subprocess
from optparse import Values
from typing import Any, List, Optional
from typing import TYPE_CHECKING, Any, List, Optional

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
Expand All @@ -16,6 +17,9 @@
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import get_prog, write_output

if TYPE_CHECKING:
from optparse import Values

logger = logging.getLogger(__name__)


Expand Down
13 changes: 9 additions & 4 deletions src/pip/_internal/commands/debug.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import importlib.resources
import locale
import logging
import os
import sys
from optparse import Values
from types import ModuleType
from typing import Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional

import pip._vendor
from pip._vendor.certifi import where
Expand All @@ -15,11 +15,16 @@
from pip._internal.cli.base_command import Command
from pip._internal.cli.cmdoptions import make_target_python
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.configuration import Configuration
from pip._internal.metadata import get_environment
from pip._internal.utils.logging import indent_log
from pip._internal.utils.misc import get_pip_version

if TYPE_CHECKING:
from optparse import Values
from types import ModuleType

from pip._internal.configuration import Configuration

logger = logging.getLogger(__name__)


Expand Down
Loading
Loading