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

Normalize extra comparison in markers for output #549

Merged
merged 2 commits into from May 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions packaging/markers.py
Expand Up @@ -139,11 +139,24 @@ def serialize(self) -> str:
MARKER = stringStart + MARKER_EXPR + stringEnd


def _coerce_parse_result(results: Union[ParseResults, List[Any]]) -> List[Any]:
def _coerce_parse_result(results: Any) -> Any:
"""
Flatten the parse results into a list of results.

Also normalize extra values.
"""
if isinstance(results, ParseResults):
return [_coerce_parse_result(i) for i in results]
else:
return results
elif isinstance(results, tuple):
lhs, op, rhs = results
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 = lhs, op, rhs
return results


def _format_marker(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_markers.py
Expand Up @@ -362,3 +362,12 @@ def test_evaluate_setuptools_legacy_markers(self):
marker_string = "python_implementation=='Jython'"
args = [{"platform_python_implementation": "CPython"}]
assert Marker(marker_string).evaluate(*args) is False

def test_extra_str_normalization(self):
raw_name = "S_P__A_M"
normalized_name = "s-p-a-m"
lhs = f"{raw_name!r} == extra"
rhs = f"extra == {raw_name!r}"

assert str(Marker(lhs)) == f'"{normalized_name}" == extra'
assert str(Marker(rhs)) == f'extra == "{normalized_name}"'