Skip to content

Commit

Permalink
Allow "extra" to be None in the marker environment
Browse files Browse the repository at this point in the history
This is for compatibility with older behaviour of this API.
  • Loading branch information
pradyunsg committed Dec 26, 2022
1 parent 97db717 commit 077d52e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,9 @@ def evaluate(self, environment: Optional[Dict[str, str]] = None) -> bool:
current_environment["extra"] = ""
if environment is not None:
current_environment.update(environment)
# The API used to allow setting extra to None. We need to handle this
# case for backwards compatibility.
if current_environment["extra"] is None:
current_environment["extra"] = ""

return _evaluate_markers(self._markers, current_environment)
15 changes: 15 additions & 0 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import platform
import sys
from typing import cast

import pytest

Expand Down Expand Up @@ -256,6 +257,20 @@ def test_compare_markers_to_other_objects(self):
def test_environment_assumes_empty_extra(self):
assert Marker('extra == "im_valid"').evaluate() is False

def test_environment_with_extra_none(self):
# GIVEN
marker_str = 'extra == "im_valid"'

# Pretend that this is dict[str, str], even though it's not. This is a
# test for being bug-for-bug compatible with the older implementation.
environment = cast("dict[str, str]", {"extra": None})

# WHEN
marker = Marker(marker_str)

# THEN
assert marker.evaluate(environment) is False

@pytest.mark.parametrize(
("marker_string", "environment", "expected"),
[
Expand Down

0 comments on commit 077d52e

Please sign in to comment.