Skip to content

Commit

Permalink
Added py.typed file and enabled implicit_optional
Browse files Browse the repository at this point in the history
  • Loading branch information
abrisco authored and Spencer Putt committed Jan 16, 2024
1 parent c72a941 commit 8ec9332
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 193 deletions.
41 changes: 34 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,54 @@ on:

jobs:
build:
strategy:
fail-fast: false
matrix:
platform: ["ubuntu-latest"]
python_version: ["3.7"]

runs-on: ubuntu-latest
runs-on: ${{ matrix.platform }}

name: test ${{ matrix.platform }} (py${{ matrix.python_version }})

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: "${{ matrix.python_version }}"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt --user
- name: Run mypy
run: |
export MYPYPATH=req-compile/stubs
mypy req_compile
python -m mypy req_compile
env:
MYPYPATH: req-compile/stubs
- name: Run pylint
run: |
pylint req_compile
python -m pylint req_compile
- name: Test with pytest
run: |
pytest
python -m pytest -v
formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt --user
- name: Run black
run: |
black --check --diff req_compile
- name: Run isort
run: |
isort --check-only req_compile
11 changes: 0 additions & 11 deletions pylintrc

This file was deleted.

19 changes: 17 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
[build-system]
requires = [ "setuptools", "wheel" ]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.mypy]
implicit_optional = true
namespace_packages = false

[tool.pylint.main]
max-line-length = 120

[tool.pylint."messages control"]
disable = [
"R",
"missing-docstring",
"too-few-public-methods",
"too-many-arguments",
"raise-missing-from",
"consider-using-f-string",
]

[tool.isort]
profile = "black"
18 changes: 9 additions & 9 deletions req_compile/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import OrderedDict
from io import StringIO
from itertools import repeat
from typing import IO, Any, Iterable, List, Mapping, Sequence, Set, Union
from typing import IO, Any, Iterable, List, Mapping, Optional, Sequence, Set, Union

import pkg_resources

Expand Down Expand Up @@ -53,7 +53,7 @@
from req_compile.versions import is_possible

# Blacklist of requirements that will be filtered out of the output
BLACKLIST = [] # type: Iterable[str]
BLACKLIST: Iterable[str] = []


def _cantusereason_to_text(
Expand All @@ -79,7 +79,7 @@ def _cantusereason_to_text(


def _find_paths_to_root(
failing_node: DependencyNode, visited: Set[DependencyNode] = None
failing_node: DependencyNode, visited: Optional[Set[DependencyNode]] = None
) -> Sequence[Sequence[DependencyNode]]:
if visited is None:
visited = set()
Expand All @@ -105,7 +105,7 @@ def _generate_no_candidate_display(
repo: Repository,
dists: DistributionCollection,
failure: Exception,
only_binary: Set[NormName] = None,
only_binary: Optional[Set[NormName]] = None,
) -> None:
"""Print a human friendly display to stderr when compilation fails"""
failing_node = dists[req.name]
Expand Down Expand Up @@ -211,7 +211,7 @@ def _print_paths_to_root(
def _dump_repo_candidates(
req: pkg_resources.Requirement,
repos: Iterable[Repository],
only_binary: Set[NormName] = None,
only_binary: Optional[Set[NormName]] = None,
) -> None:
"""
Args:
Expand Down Expand Up @@ -368,7 +368,7 @@ def write_requirements_file(
repo: Repository,
annotate_source: bool = False,
urls: bool = False,
input_reqs: Iterable[RequirementContainer] = None,
input_reqs: Optional[Iterable[RequirementContainer]] = None,
remove_non_source: bool = False,
remove_source: bool = False,
no_pins: bool = False,
Expand Down Expand Up @@ -572,7 +572,7 @@ def build_repo(
find_links: Iterable[str],
index_urls: Iterable[str],
wheeldir: str,
extra_index_urls: Iterable[str] = None,
extra_index_urls: Optional[Iterable[str]] = None,
no_index: bool = False,
allow_prerelease: bool = False,
) -> Repository:
Expand Down Expand Up @@ -659,7 +659,7 @@ def __call__(
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Union[str, Sequence[Any], None],
option_string: str = None,
option_string: Optional[str] = None,
) -> None:
"""Parse the string into a set, checking for special cases."""
# Set the AllOnlyBinarySet to ensure all projects match the set.
Expand All @@ -674,7 +674,7 @@ def __call__(
)


def compile_main(raw_args: Sequence[str] = None) -> None:
def compile_main(raw_args: Optional[Sequence[str]] = None) -> None:
parser = argparse.ArgumentParser(
description="Req-Compile: Python requirements compiler"
)
Expand Down
17 changes: 10 additions & 7 deletions req_compile/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def compile_roots(
options: CompileOptions,
depth: int = 1,
max_downgrade: int = MAX_DOWNGRADE,
_path: Set[DependencyNode] = None,
_path: Optional[Set[DependencyNode]] = None,
) -> None: # pylint: disable=too-many-statements,too-many-locals,too-many-branches
"""
Args:
Expand Down Expand Up @@ -190,7 +190,7 @@ def compile_roots(

nodes = sorted(node.reverse_deps)

violate_score = defaultdict(int) # type: Dict[DependencyNode, int]
violate_score: Dict[DependencyNode, int] = defaultdict(int)
for idx, revnode in enumerate(nodes):
for next_node in nodes[idx + 1 :]:
if not is_possible(
Expand Down Expand Up @@ -264,10 +264,10 @@ def compile_roots(
def perform_compile(
input_reqs: Iterable[RequirementContainer],
repo: Repository,
constraint_reqs: Iterable[RequirementContainer] = None,
extras: Iterable[str] = None,
constraint_reqs: Optional[Iterable[RequirementContainer]] = None,
extras: Optional[Iterable[str]] = None,
allow_circular_dependencies: bool = True,
only_binary: Set[NormName] = None,
only_binary: Optional[Set[NormName]] = None,
) -> Tuple[DistributionCollection, Set[DependencyNode]]:
"""Perform a compilation using the given inputs and constraints.
Expand Down Expand Up @@ -335,8 +335,11 @@ def perform_compile(
return results, roots


def _add_constraints(all_pinned, constraint_reqs, results):
# type: (bool, Optional[Iterable[RequirementContainer]], DistributionCollection) -> None
def _add_constraints(
all_pinned: bool,
constraint_reqs: Optional[Iterable[RequirementContainer]],
results: DistributionCollection,
) -> None:
if all_pinned and constraint_reqs is not None:
for constraint_source in constraint_reqs:
results.add_dist(constraint_source, None, None)
44 changes: 25 additions & 19 deletions req_compile/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def __init__(
def __iter__(self) -> Iterator[pkg_resources.Requirement]:
return iter(self.reqs)

def requires(self, extra=None):
# type: (str) -> Iterable[pkg_resources.Requirement]
def requires(
self, extra: Optional[str] = None
) -> Iterable[pkg_resources.Requirement]:
return reduce_requirements(
req for req in self.reqs if req_uses_extra(req, extra)
)
Expand All @@ -61,7 +62,7 @@ def to_definition(


def reqs_from_files(
requirements_files: Iterable[str], parameters: List[str] = None
requirements_files: Iterable[str], parameters: Optional[List[str]] = None
) -> Iterable[pkg_resources.Requirement]:
"""Produce a list of requirements from multiple requirements files.
Expand Down Expand Up @@ -90,19 +91,17 @@ def __init__(
self,
filename: str,
reqs: Iterable[pkg_resources.Requirement],
parameters: List[str] = None,
parameters: Optional[List[str]] = None,
**_kwargs: Any
) -> None:
super(RequirementsFile, self).__init__(filename, reqs, meta=True)
self.parameters = parameters

def __repr__(self):
# type: () -> str
def __repr__(self) -> str:
return "RequirementsFile({})".format(self.name)

@classmethod
def from_file(cls, full_path, **kwargs):
# type: (str, **Any) -> RequirementsFile
def from_file(cls, full_path: str, **kwargs: Any) -> "RequirementsFile":
"""Load requirements from a file and build a RequirementsFile
Args:
Expand All @@ -118,16 +117,22 @@ def from_file(cls, full_path, **kwargs):
def __str__(self) -> str:
return self.name

def to_definition(self, extras):
# type: (Optional[Iterable[str]]) -> Tuple[str, Optional[packaging.version.Version]]
def to_definition(
self, extras: Optional[Iterable[str]]
) -> Tuple[str, Optional[packaging.version.Version]]:
return self.name, None


class DistInfo(RequirementContainer):
"""Metadata describing a distribution of a project"""

def __init__(self, name, version, reqs, meta=False):
# type: (str, Optional[packaging.version.Version], Iterable[pkg_resources.Requirement], bool) -> None
def __init__(
self,
name: str,
version: Optional[packaging.version.Version],
reqs: Iterable[pkg_resources.Requirement],
meta: bool = False,
) -> None:
"""
Args:
name: The project name
Expand All @@ -139,19 +144,18 @@ def __init__(self, name, version, reqs, meta=False):
self.version = version
self.source = None

def __str__(self):
# type: () -> str
def __str__(self) -> str:
return "{}=={}".format(*self.to_definition(None))

def to_definition(self, extras):
# type: (Optional[Iterable[str]]) -> Tuple[str, Optional[packaging.version.Version]]
def to_definition(
self, extras: Optional[Iterable[str]]
) -> Tuple[str, Optional[packaging.version.Version]]:
req_expr = "{}{}".format(
self.name, ("[" + ",".join(sorted(extras)) + "]") if extras else ""
)
return req_expr, self.version

def __repr__(self):
# type: () -> str
def __repr__(self) -> str:
return (
self.name
+ " "
Expand All @@ -174,7 +178,9 @@ def __init__(self, dist: pkg_resources.Distribution) -> None:
def __str__(self) -> str:
return "{}=={}".format(*self.to_definition(None))

def requires(self, extra: str = None) -> Iterable[pkg_resources.Requirement]:
def requires(
self, extra: Optional[str] = None
) -> Iterable[pkg_resources.Requirement]:
return self.dist.requires(extras=(extra,) if extra else ())

def to_definition(
Expand Down
Loading

0 comments on commit 8ec9332

Please sign in to comment.