Skip to content

Commit 393cbc6

Browse files
committed
Accept all PEP 508 operators in python_version markers #5309
The list of operators accepted by get_python_version_os() contained '<' twice and was missing '>', so a dependency marker such as "python_version > '3.8'" never matched and was silently dropped from the DependentPackage extra_data, while ">=" was reported correctly. The '!=' and '~=' operators were missing for the same reason. Replace the duplicated '<' with the full set of comparison operators allowed for a marker in PEP 508, and add a regression test covering each of them. Signed-off-by: Daksha1611 <mehtadaksha1611@gmail.com>
1 parent 5ebebf2 commit 393cbc6

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Next release
1010

1111
- Improve copyright detection for statements with parens or trailing "authors"
1212

13+
- Fix ``python_version`` environment markers using ``>``, ``!=`` or ``~=`` being
14+
silently dropped from the dependency ``extra_data``.
15+
https://github.com/aboutcode-org/scancode-toolkit/issues/5309
16+
1317

1418
v33.0.0rc1 - 2026-05-14
1519
------------------------

src/packagedcode/pypi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,10 @@ def get_python_version_os(marker):
22782278
requirement Marker or None.
22792279
"""
22802280
platform_data = {}
2281-
python_version_operators = ['<', '>=', '==', '<=', '<']
2281+
# all comparison operators allowed for a marker in PEP 508
2282+
python_version_operators = [
2283+
'<', '<=', '!=', '==', '>=', '>', '~=', '===',
2284+
]
22822285

22832286
if not marker or not isinstance(marker, markers.Marker):
22842287
return platform_data

tests/packagedcode/test_pypi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,3 +806,21 @@ def test_parse_setup_py(test_loc):
806806
def test_parse_more_setup_py(test_loc):
807807
check_setup_py_parsing(test_loc)
808808

809+
810+
@pytest.mark.parametrize(
811+
('requirement', 'expected'),
812+
[
813+
('requests; python_version < "3.8"', {'python_version': '< 3.8'}),
814+
('requests; python_version <= "3.8"', {'python_version': '<= 3.8'}),
815+
('requests; python_version != "3.8"', {'python_version': '!= 3.8'}),
816+
('requests; python_version == "3.8"', {'python_version': '== 3.8'}),
817+
('requests; python_version >= "3.8"', {'python_version': '>= 3.8'}),
818+
('requests; python_version > "3.8"', {'python_version': '> 3.8'}),
819+
('requests; python_version ~= "3.8"', {'python_version': '~= 3.8'}),
820+
],
821+
)
822+
def test_get_python_version_os_handles_all_comparison_operators(requirement, expected):
823+
from packvers.requirements import Requirement
824+
825+
marker = Requirement(requirement).marker
826+
assert pypi.get_python_version_os(marker) == expected

0 commit comments

Comments
 (0)