Skip to content

Commit

Permalink
Adhere to PEP 685 when evaluating markers with extras
Browse files Browse the repository at this point in the history
  • Loading branch information
hroncok committed May 10, 2022
1 parent ba12432 commit 029088f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packaging/markers.py
Expand Up @@ -5,6 +5,7 @@
import operator
import os
import platform
import re
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -219,6 +220,16 @@ def _get_env(environment: Dict[str, str], name: str) -> str:
return value


def _normalize(*values: str, key: str) -> Tuple[str, ...]:
# PEP 685 – Comparison of extra names for optional distribution dependencies
# https://peps.python.org/pep-0685/
if key == "extra":
return tuple(re.sub(r"[-_.]+", "-", v).lower() for v in values)

# other environment markes don't have such standards
return values


def _evaluate_markers(markers: List[Any], environment: Dict[str, str]) -> bool:
groups: List[List[bool]] = [[]]

Expand All @@ -231,12 +242,15 @@ def _evaluate_markers(markers: List[Any], environment: Dict[str, str]) -> bool:
lhs, op, rhs = marker

if isinstance(lhs, Variable):
lhs_value = _get_env(environment, lhs.value)
environment_key = lhs.value
lhs_value = _get_env(environment, environment_key)
rhs_value = rhs.value
else:
lhs_value = lhs.value
rhs_value = _get_env(environment, rhs.value)
environment_key = rhs.value
rhs_value = _get_env(environment, environment_key)

lhs_value, rhs_value = _normalize(lhs_value, rhs_value, key=environment_key)
groups[-1].append(_eval_op(lhs_value, op, rhs_value))
else:
assert marker in ["and", "or"]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_markers.py
Expand Up @@ -292,6 +292,14 @@ def test_extra_with_no_extra_in_environment(self):
),
("extra == 'security'", {"extra": "quux"}, False),
("extra == 'security'", {"extra": "security"}, True),
("extra == 'SECURITY'", {"extra": "security"}, True),
("extra == 'security'", {"extra": "SECURITY"}, True),
("extra == 'pep-685-norm'", {"extra": "PEP_685...norm"}, True),
(
"extra == 'Different.punctuation..is...equal'",
{"extra": "different__punctuation_is_EQUAL"},
True,
),
],
)
def test_evaluates(self, marker_string, environment, expected):
Expand Down

0 comments on commit 029088f

Please sign in to comment.