Skip to content

Commit 5c70ed4

Browse files
committed
Allow "extra" to be None in the marker environment
This is for compatibility with older behaviour of this API.
1 parent 30554f5 commit 5c70ed4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/packaging/markers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,5 +237,9 @@ def evaluate(self, environment: Optional[Dict[str, str]] = None) -> bool:
237237
current_environment["extra"] = ""
238238
if environment is not None:
239239
current_environment.update(environment)
240+
# The API used to allow setting extra to None. We need to handle this
241+
# case for backwards compatibility.
242+
if current_environment["extra"] is None:
243+
current_environment["extra"] = ""
240244

241245
return _evaluate_markers(self._markers, current_environment)

tests/test_markers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import platform
99
import sys
10+
from typing import cast
1011

1112
import pytest
1213

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

260+
def test_environment_with_extra_none(self):
261+
# GIVEN
262+
marker_str = 'extra == "im_valid"'
263+
264+
# Pretend that this is dict[str, str], even though it's not. This is a
265+
# test for being bug-for-bug compatible with the older implementation.
266+
environment = cast("dict[str, str]", {"extra": None})
267+
268+
# WHEN
269+
marker = Marker(marker_str)
270+
271+
# THEN
272+
assert marker.evaluate(environment) is False
273+
259274
@pytest.mark.parametrize(
260275
("marker_string", "environment", "expected"),
261276
[

0 commit comments

Comments
 (0)