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

pip_api: type hints #97

Merged
merged 7 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pip_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import sys

from pip_api._vendor.packaging import version as packaging_version
from pip_api._vendor.packaging.version import Version

# Import this now because we need it below
from pip_api._version import version

PIP_VERSION = packaging_version.parse(version())
PIP_VERSION: Version = packaging_version.parse(version()) # type: ignore
PYTHON_VERSION = sys.version_info

# Import these because they depend on the above
Expand Down
6 changes: 4 additions & 2 deletions pip_api/_hash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pip_api._vendor.packaging.version import Version
import os

from pip_api._vendor.packaging.version import Version # type: ignore

import pip_api
from pip_api._call import call
Expand All @@ -7,7 +9,7 @@
incompatible = pip_api.PIP_VERSION < Version("8.0.0")


def hash(filename, algorithm="sha256"):
def hash(filename: os.PathLike, algorithm: str = "sha256") -> str:
"""
Hash the given filename. Unavailable in `pip<8.0.0`
"""
Expand Down
7 changes: 4 additions & 3 deletions pip_api/_installed_distributions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import json
import re
from typing import Dict, Optional

import pip_api
from pip_api._call import call

from pip_api._vendor.packaging.version import parse
from pip_api._vendor.packaging.version import parse # type: ignore


class Distribution:
def __init__(self, name, version, location=None):
def __init__(self, name: str, version: str, location: Optional[str] = None):
self.name = name
self.version = parse(version)
self.location = location
Expand Down Expand Up @@ -70,7 +71,7 @@ def _new_installed_distributions():
return ret


def installed_distributions():
def installed_distributions() -> Dict[str, Distribution]:
if pip_api.PIP_VERSION < parse("9.0.0"):
return _old_installed_distributions()
return _new_installed_distributions()
11 changes: 7 additions & 4 deletions pip_api/_parse_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import os
import re
import traceback
from typing import Any, Dict, Optional, Union

from urllib.parse import urljoin
from urllib.request import pathname2url

from pip_api._vendor.packaging import requirements, specifiers
from pip_api._vendor.packaging import requirements, specifiers # type: ignore

from pip_api.exceptions import PipError

Expand Down Expand Up @@ -164,7 +165,9 @@ def _ignore_comments(lines_enum):
yield line_number, line


def parse_requirements(filename, options=None, include_invalid=False):
def parse_requirements(
filename: os.PathLike, options: Optional[Any] = None, include_invalid: bool = False
) -> Dict[str, Union[requirements.Requirement, UnparsedRequirement]]:
to_parse = {filename}
parsed = set()
name_to_req = {}
Expand All @@ -181,7 +184,7 @@ def parse_requirements(filename, options=None, include_invalid=False):
lines_enum = _skip_regex(lines_enum, options)

for lineno, line in lines_enum:
req = None
req: Optional[Union[requirements.Requirement, UnparsedRequirement]] = None
known, _ = parser.parse_known_args(line.strip().split())
if known.req:
try: # Try to parse this as a requirement specification
Expand Down Expand Up @@ -210,7 +213,7 @@ def parse_requirements(filename, options=None, include_invalid=False):
# If we've found a requirement, add it
if req:
if not isinstance(req, UnparsedRequirement):
req.comes_from = "-r {} (line {})".format(filename, lineno)
req.comes_from = "-r {} (line {})".format(filename, lineno) # type: ignore

if req.name not in name_to_req:
name_to_req[req.name.lower()] = req
Expand Down
2 changes: 1 addition & 1 deletion pip_api/_vendor/packaging/_manylinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _is_compatible(name: str, arch: str, version: _GLibCVersion) -> bool:
return False
# Check for presence of _manylinux module.
try:
import _manylinux # noqa
import _manylinux # type: ignore # noqa
except ImportError:
return True
if hasattr(_manylinux, "manylinux_compatible"):
Expand Down
2 changes: 1 addition & 1 deletion pip_api/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pip_api._call import call


def version():
def version() -> str:
result = call("--version")

# result is of the form:
Expand Down
Empty file added pip_api/py.typed
Empty file.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
long_description_content_type="text/markdown",
name="pip-api",
packages=find_packages(),
package_data={
"pip_api": ["py.typed"],
},
python_requires=">=3.6",
url="http://github.com/di/pip-api",
summary="An unofficial, importable pip API",
Expand Down