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

configure isort to add from __future__ import annotations #1695

Merged
merged 7 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html
"""Configuration file for the Sphinx documentation builder."""

from __future__ import annotations

from functools import partial
from pathlib import Path

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

import locale

from click import secho
Expand Down
2 changes: 2 additions & 0 deletions piptools/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import click

from piptools.scripts import compile, sync
Expand Down
2 changes: 2 additions & 0 deletions piptools/_compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .click import IS_CLICK_VER_8_PLUS
from .pip_compat import PIP_VERSION, parse_requirements

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

import click

CLICK_MAJOR_VERSION = int(
Expand Down
10 changes: 6 additions & 4 deletions piptools/_compat/pip_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import optparse
from typing import Callable, Iterable, Iterator, Optional, cast
from typing import Callable, Iterable, Iterator, cast

import pip
from pip._internal.index.package_finder import PackageFinder
Expand All @@ -25,8 +27,8 @@
def parse_requirements(
filename: str,
session: PipSession,
finder: Optional[PackageFinder] = None,
options: Optional[optparse.Values] = None,
finder: PackageFinder | None = None,
options: optparse.Values | None = None,
constraint: bool = False,
isolated: bool = False,
) -> Iterator[InstallRequirement]:
Expand Down Expand Up @@ -80,7 +82,7 @@ def _uses_pkg_resources() -> bool:

Distribution = select_backend().Distribution

def dist_requires(dist: "Distribution") -> Iterable[Requirement]:
def dist_requires(dist: Distribution) -> Iterable[Requirement]:
"""Mimics pkg_resources.Distribution.requires for the case of no
extras. This doesn't fulfill that API's `extras` parameter but
satisfies the needs of pip-tools."""
Expand Down
16 changes: 9 additions & 7 deletions piptools/cache.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import json
import os
import platform
import sys
from typing import Dict, Iterable, List, Optional, Set, Tuple, cast
from typing import Dict, Iterable, List, Tuple, cast

from pip._internal.req import InstallRequirement
from pip._vendor.packaging.requirements import Requirement
Expand Down Expand Up @@ -70,7 +72,7 @@ def __init__(self, cache_dir: str):
cache_filename = f"depcache-{_implementation_name()}.json"

self._cache_file = os.path.join(cache_dir, cache_filename)
self._cache: Optional[CacheDict] = None
self._cache: CacheDict | None = None

@property
def cache(self) -> CacheDict:
Expand Down Expand Up @@ -119,19 +121,19 @@ def __contains__(self, ireq: InstallRequirement) -> bool:
pkgname, pkgversion_and_extras = self.as_cache_key(ireq)
return pkgversion_and_extras in self.cache.get(pkgname, {})

def __getitem__(self, ireq: InstallRequirement) -> List[str]:
def __getitem__(self, ireq: InstallRequirement) -> list[str]:
pkgname, pkgversion_and_extras = self.as_cache_key(ireq)
return self.cache[pkgname][pkgversion_and_extras]

def __setitem__(self, ireq: InstallRequirement, values: List[str]) -> None:
def __setitem__(self, ireq: InstallRequirement, values: list[str]) -> None:
pkgname, pkgversion_and_extras = self.as_cache_key(ireq)
self.cache.setdefault(pkgname, {})
self.cache[pkgname][pkgversion_and_extras] = values
self.write_cache()

def reverse_dependencies(
self, ireqs: Iterable[InstallRequirement]
) -> Dict[str, Set[str]]:
) -> dict[str, set[str]]:
"""
Returns a lookup table of reverse dependencies for all the given ireqs.

Expand All @@ -144,8 +146,8 @@ def reverse_dependencies(
return self._reverse_dependencies(ireqs_as_cache_values)

def _reverse_dependencies(
self, cache_keys: Iterable[Tuple[str, str]]
) -> Dict[str, Set[str]]:
self, cache_keys: Iterable[tuple[str, str]]
) -> dict[str, set[str]]:
"""
Returns a lookup table of reverse dependencies for all the given cache keys.

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

from typing import Iterable

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

from pip._internal.utils.appdirs import user_cache_dir

# The user_cache_dir helper comes straight from pip itself
Expand Down
2 changes: 2 additions & 0 deletions piptools/logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import contextlib
import logging
import sys
Expand Down
2 changes: 2 additions & 0 deletions piptools/repositories/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .local import LocalRequirementsRepository
from .pypi import PyPIRepository

Expand Down
10 changes: 6 additions & 4 deletions piptools/repositories/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import optparse
from abc import ABCMeta, abstractmethod
from contextlib import contextmanager
from typing import Iterator, Optional, Set
from typing import Iterator

from pip._internal.commands.install import InstallCommand
from pip._internal.index.package_finder import PackageFinder
Expand All @@ -18,23 +20,23 @@ def clear_caches(self) -> None:

@abstractmethod
def find_best_match(
self, ireq: InstallRequirement, prereleases: Optional[bool]
self, ireq: InstallRequirement, prereleases: bool | None
) -> InstallRequirement:
"""
Returns a pinned InstallRequirement object that indicates the best match
for the given InstallRequirement according to the external repository.
"""

@abstractmethod
def get_dependencies(self, ireq: InstallRequirement) -> Set[InstallRequirement]:
def get_dependencies(self, ireq: InstallRequirement) -> set[InstallRequirement]:
"""
Given a pinned, URL, or editable InstallRequirement, returns a set of
dependencies (also InstallRequirements, but not necessarily pinned).
They indicate the secondary dependencies for the given requirement.
"""

@abstractmethod
def get_hashes(self, ireq: InstallRequirement) -> Set[str]:
def get_hashes(self, ireq: InstallRequirement) -> set[str]:
"""
Given a pinned InstallRequirement, returns a set of hashes that represent
all of the files for a given requirement. It is not acceptable for an
Expand Down
10 changes: 6 additions & 4 deletions piptools/repositories/local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import optparse
from contextlib import contextmanager
from typing import Iterator, Mapping, Optional, Set, cast
from typing import Iterator, Mapping, cast

from pip._internal.commands.install import InstallCommand
from pip._internal.index.package_finder import PackageFinder
Expand Down Expand Up @@ -71,7 +73,7 @@ def clear_caches(self) -> None:
self.repository.clear_caches()

def find_best_match(
self, ireq: InstallRequirement, prereleases: Optional[bool] = None
self, ireq: InstallRequirement, prereleases: bool | None = None
) -> InstallationCandidate:
key = key_from_ireq(ireq)
existing_pin = self.existing_pins.get(key)
Expand All @@ -81,10 +83,10 @@ def find_best_match(
else:
return self.repository.find_best_match(ireq, prereleases)

def get_dependencies(self, ireq: InstallRequirement) -> Set[InstallRequirement]:
def get_dependencies(self, ireq: InstallRequirement) -> set[InstallRequirement]:
return self.repository.get_dependencies(ireq)

def get_hashes(self, ireq: InstallRequirement) -> Set[str]:
def get_hashes(self, ireq: InstallRequirement) -> set[str]:
existing_pin = self._reuse_hashes and self.existing_pins.get(
key_from_ireq(ireq)
)
Expand Down
44 changes: 18 additions & 26 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import contextlib
import hashlib
import itertools
Expand All @@ -6,17 +8,7 @@
import os
from contextlib import contextmanager
from shutil import rmtree
from typing import (
Any,
BinaryIO,
ContextManager,
Dict,
Iterator,
List,
NamedTuple,
Optional,
Set,
)
from typing import Any, BinaryIO, ContextManager, Iterator, NamedTuple

from click import progressbar
from pip._internal.cache import WheelCache
Expand Down Expand Up @@ -56,7 +48,7 @@

class FileStream(NamedTuple):
stream: BinaryIO
size: Optional[float]
size: float | None


class PyPIRepository(BaseRepository):
Expand All @@ -69,7 +61,7 @@ class PyPIRepository(BaseRepository):
changed/configured on the Finder.
"""

def __init__(self, pip_args: List[str], cache_dir: str):
def __init__(self, pip_args: list[str], cache_dir: str):
# Use pip's parser for pip.conf management and defaults.
# General options (find_links, index_url, extra_index_url, trusted_host,
# and pre) are deferred to pip.
Expand All @@ -91,12 +83,12 @@ def __init__(self, pip_args: List[str], cache_dir: str):
# stores project_name => InstallationCandidate mappings for all
# versions reported by PyPI, so we only have to ask once for each
# project
self._available_candidates_cache: Dict[str, List[InstallationCandidate]] = {}
self._available_candidates_cache: dict[str, list[InstallationCandidate]] = {}

# stores InstallRequirement => list(InstallRequirement) mappings
# of all secondary dependencies for the given requirement, so we
# only have to go to disk once for each requirement
self._dependencies_cache: Dict[InstallRequirement, Set[InstallRequirement]] = {}
self._dependencies_cache: dict[InstallRequirement, set[InstallRequirement]] = {}

# Setup file paths
self._cache_dir = normalize_path(str(cache_dir))
Expand Down Expand Up @@ -124,14 +116,14 @@ def command(self) -> InstallCommand:
"""Return an install command instance."""
return self._command

def find_all_candidates(self, req_name: str) -> List[InstallationCandidate]:
def find_all_candidates(self, req_name: str) -> list[InstallationCandidate]:
if req_name not in self._available_candidates_cache:
candidates = self.finder.find_all_candidates(req_name)
self._available_candidates_cache[req_name] = candidates
return self._available_candidates_cache[req_name]

def find_best_match(
self, ireq: InstallRequirement, prereleases: Optional[bool] = None
self, ireq: InstallRequirement, prereleases: bool | None = None
) -> InstallRequirement:
"""
Returns a pinned InstallRequirement object that indicates the best match
Expand Down Expand Up @@ -167,10 +159,10 @@ def find_best_match(

def resolve_reqs(
self,
download_dir: Optional[str],
download_dir: str | None,
ireq: InstallRequirement,
wheel_cache: WheelCache,
) -> Set[InstallationCandidate]:
) -> set[InstallationCandidate]:
with get_build_tracker() as build_tracker, TempDirectory(
kind="resolver"
) as temp_dir, indent_log():
Expand Down Expand Up @@ -218,7 +210,7 @@ def resolve_reqs(

return set(results)

def get_dependencies(self, ireq: InstallRequirement) -> Set[InstallRequirement]:
def get_dependencies(self, ireq: InstallRequirement) -> set[InstallRequirement]:
"""
Given a pinned, URL, or editable InstallRequirement, returns a set of
dependencies (also InstallRequirements, but not necessarily pinned).
Expand Down Expand Up @@ -301,7 +293,7 @@ def _get_download_path(self, ireq: InstallRequirement) -> str:
else:
return self._download_dir

def get_hashes(self, ireq: InstallRequirement) -> Set[str]:
def get_hashes(self, ireq: InstallRequirement) -> set[str]:
"""
Given an InstallRequirement, return a set of hashes that represent all
of the files for a given requirement. Unhashable requirements return an
Expand Down Expand Up @@ -341,7 +333,7 @@ def get_hashes(self, ireq: InstallRequirement) -> Set[str]:

return hashes

def _get_hashes_from_pypi(self, ireq: InstallRequirement) -> Optional[Set[str]]:
def _get_hashes_from_pypi(self, ireq: InstallRequirement) -> set[str] | None:
"""
Return a set of hashes from PyPI JSON API for a given InstallRequirement.
Return None if fetching data is failed or missing digests.
Expand Down Expand Up @@ -370,7 +362,7 @@ def _get_hashes_from_pypi(self, ireq: InstallRequirement) -> Optional[Set[str]]:

return hashes

def _get_hashes_from_files(self, ireq: InstallRequirement) -> Set[str]:
def _get_hashes_from_files(self, ireq: InstallRequirement) -> set[str]:
"""
Return a set of hashes for all release files of a given InstallRequirement.
"""
Expand Down Expand Up @@ -427,11 +419,11 @@ def allow_all_wheels(self) -> Iterator[None]:
the previous non-patched calls will interfere.
"""

def _wheel_supported(self: Wheel, tags: List[Tag]) -> bool:
def _wheel_supported(self: Wheel, tags: list[Tag]) -> bool:
# Ignore current platform. Support everything.
return True

def _wheel_support_index_min(self: Wheel, tags: List[Tag]) -> int:
def _wheel_support_index_min(self: Wheel, tags: list[Tag]) -> int:
# All wheels are equal priority for sorting.
return 0

Expand Down Expand Up @@ -518,7 +510,7 @@ def open_local_or_remote_file(link: Link, session: Session) -> Iterator[FileStre
response = session.get(url, headers=headers, stream=True)

# Content length must be int or None
content_length: Optional[int]
content_length: int | None
try:
content_length = int(response.headers["content-length"])
except (ValueError, KeyError, TypeError):
Expand Down