Skip to content

Commit

Permalink
Normalize extra values in markers
Browse files Browse the repository at this point in the history
  • Loading branch information
hrnciar committed Jul 19, 2022
1 parent 32a3f32 commit c30f323
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from ._parser import MarkerAtom, MarkerList, Op, Variable, parse_marker_expr
from ._parser import MarkerAtom, MarkerList, Op, Value, Variable, parse_marker_expr
from ._tokenizer import ParseExceptionError, Tokenizer
from .specifiers import InvalidSpecifier, Specifier
from .utils import canonicalize_name
Expand Down Expand Up @@ -43,6 +43,22 @@ class UndefinedEnvironmentName(ValueError):
"""


def _normalize_extra_values(results: Any) -> Any:
"""
Normalize extra values.
"""
if isinstance(results[0], tuple):
lhs, op, rhs = results[0]
if isinstance(lhs, Variable) and lhs.value == "extra":
normalized_extra = canonicalize_name(rhs.value)
rhs = Value(normalized_extra)
elif isinstance(rhs, Variable) and rhs.value == "extra":
normalized_extra = canonicalize_name(lhs.value)
lhs = Value(normalized_extra)
results[0] = lhs, op, rhs
return results


def _format_marker(
marker: Union[List[str], MarkerAtom, str], first: Optional[bool] = True
) -> str:
Expand Down Expand Up @@ -170,7 +186,9 @@ def default_environment() -> Dict[str, str]:
class Marker:
def __init__(self, marker: str) -> None:
try:
self._markers = parse_marker_expr(Tokenizer(marker))
self._markers = _normalize_extra_values(
parse_marker_expr(Tokenizer(marker))
)
# The attribute `_markers` can be described in terms of a recursive type:
# MarkerList = List[Union[Tuple[Node, ...], str, MarkerList]]
#
Expand Down

0 comments on commit c30f323

Please sign in to comment.