Skip to content

Commit

Permalink
fix docstring
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Jan 4, 2023
1 parent 7ff1bef commit a1a56ce
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/tox/tox_env/python/virtual_env/package/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from copy import deepcopy
from typing import Optional, Set, cast

from packaging.markers import Variable # type: ignore[attr-defined]
from packaging.requirements import Requirement
Expand Down Expand Up @@ -29,37 +30,41 @@ def dependencies_with_extras(deps: list[Requirement], extras: set[str], package_


def extract_extra_markers(deps: list[Requirement]) -> list[tuple[Requirement, set[str | None]]]:
# extras might show up as markers, move them into extras property
"""
Extract extra markers from dependencies.
:param deps: the dependencies
:return: a list of requirement, extras set
"""
result: list[tuple[Requirement, set[str | None]]] = []
for req in deps:
req = deepcopy(req)
markers: list[str | tuple[Variable, Variable, Variable]] = getattr(req.marker, "_markers", []) or []
new_markers: list[str | tuple[Variable, Variable, Variable]] = []
result.append((req, _extract_extra_markers(req)))
return result

def _is_extra_marker(_marker: str | tuple[Variable, Variable, Variable]) -> bool:
return (
isinstance(_marker, tuple)
and len(_marker) == 3
and _marker[0].value == "extra"
and _marker[1].value == "=="
)

extra_markers = set()
marker = markers.pop(0) if markers else None
while marker:
if _is_extra_marker(marker):
extra_markers.add(marker[2].value) # type: ignore
if new_markers and new_markers[-1] in ("and", "or"):
del new_markers[-1]
marker = markers.pop(0) if markers else None
if marker in ("and", "or"):
marker = markers.pop(0) if markers else None
else:
new_markers.append(marker)
def _extract_extra_markers(req: Requirement) -> set[str | None]:
req = deepcopy(req)
markers: list[str | tuple[Variable, Variable, Variable]] = getattr(req.marker, "_markers", []) or []
new_markers: list[str | tuple[Variable, Variable, Variable]] = []
extra_markers: set[str] = set() # markers that have a key of extra
marker = markers.pop(0) if markers else None
while marker:
if _is_extra_marker(marker):
extra_markers.add(marker[2].value) # type: ignore
if new_markers and new_markers[-1] in ("and", "or"):
del new_markers[-1]
marker = markers.pop(0) if markers else None
if marker in ("and", "or"):
marker = markers.pop(0) if markers else None
if new_markers:
req.marker._markers = new_markers # type: ignore
else:
req.marker = None
result.append((req, extra_markers or {None}))
return result
new_markers.append(marker)
marker = markers.pop(0) if markers else None
if new_markers:
req.marker._markers = new_markers # type: ignore
else:
req.marker = None
return cast(Set[Optional[str]], extra_markers) or {None}


def _is_extra_marker(_marker: str | tuple[Variable, Variable, Variable]) -> bool:
return isinstance(_marker, tuple) and len(_marker) == 3 and _marker[0].value == "extra" and _marker[1].value == "=="

0 comments on commit a1a56ce

Please sign in to comment.