Skip to content

markers: recognize empty python_version markers #849

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,12 +1231,20 @@ def _merge_single_markers(
elif marker1.name == "python_version":
from poetry.core.packages.utils.utils import get_python_constraint_from_marker

if isinstance(result_constraint, VersionRange) and result_constraint.min:
# Convert 'python_version >= "3.8" and python_version < "3.9"'
# to 'python_version == "3.8"'
candidate = parse_marker(f'{marker1.name} == "{result_constraint.min}"')
if get_python_constraint_from_marker(candidate) == result_constraint:
result_marker = candidate
if isinstance(result_constraint, VersionRange) and merge_class == MultiMarker:
if result_constraint.min:
# Convert 'python_version >= "3.8" and python_version < "3.9"'
# to 'python_version == "3.8"'.
candidate = parse_marker(f'{marker1.name} == "{result_constraint.min}"')
if get_python_constraint_from_marker(candidate) == result_constraint:
result_marker = candidate
if result_marker is None:
# Detect 'python_version > "3.8" and python_version < "3.9"' as empty.
result_constraint = get_python_constraint_from_marker(
marker1
).intersect(get_python_constraint_from_marker(marker2))
if result_constraint.is_empty():
result_marker = EmptyMarker()

elif isinstance(result_constraint, VersionUnion) and merge_class == MarkerUnion:
# Convert 'python_version == "3.8" or python_version >= "3.9"'
Expand Down
27 changes: 19 additions & 8 deletions tests/version/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ def test_single_marker_intersect() -> None:
assert str(intersection) == 'python_version >= "3.4" and python_version < "3.6"'


@pytest.mark.parametrize(
("marker1", "marker2"),
[
('sys_platform == "win32"', 'sys_platform == "linux"'),
('sys_platform != "win32"', 'sys_platform == "win32"'),
('python_version >= "3.6"', 'python_version < "3.6"'),
('python_version == "3.6"', 'python_version == "3.7"'),
('python_version > "3.6"', 'python_version <= "3.6"'),
('python_version >= "3.6"', 'python_version < "3.6"'),
('python_version > "3.6"', 'python_version < "3.7"'),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last test is the relevant one but since I did not find such a basic test function that tests for empty single marker intersections, I just added some additional tests.

],
)
def test_single_marker_intersect_is_empty(marker1: str, marker2: str) -> None:
assert parse_marker(marker1).intersect(parse_marker(marker2)).is_empty()


@pytest.mark.parametrize(
("marker1", "marker2", "expected"),
[
Expand Down Expand Up @@ -852,11 +868,6 @@ def test_multi_marker_union_multi_is_multi(
'python_version >= "3.6" and python_version < "3.8"',
'python_version >= "3.6" and python_version < "3.8"',
),
(
'python_version > "3.6" and python_full_version < "3.6.2"',
'python_version > "3.6" and python_version < "3.7"',
'python_version > "3.6" and python_version < "3.7"',
),
Comment on lines -855 to -859
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test does not make sense because both markers are empty. Similar (but correct) tests are above and below.

(
'python_version > "3.6" and python_full_version < "3.7.2"',
'python_version > "3.6" and python_version < "3.8"',
Expand Down Expand Up @@ -2260,16 +2271,16 @@ def test_intersection_no_endless_recursion() -> None:
' and extra != "postgis"'
)
m2 = parse_marker(
'python_version > "3.12" and python_version < "3.13" or extra != "databricks"'
'python_version >= "3.12" and python_version < "3.13" or extra != "databricks"'
Copy link
Member Author

@radoering radoering Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original marker is actually empty and does not trigger a potential endless recursion anymore. I checked that the changed marker triggers the recursion error without the fix from #832.

)
expected = (
'(python_version < "3.9" or extra != "bigquery" and extra != "parquet"'
' and extra != "motherduck" and extra != "athena" and extra != "synapse"'
' and extra != "clickhouse" and extra != "dremio" and extra != "lancedb"'
' and extra != "deltalake" and extra != "pyiceberg")'
' and python_version < "3.13" and extra != "postgres" and extra != "redshift"'
' and extra != "postgis" and (python_version > "3.12"'
' and python_version < "3.13" or extra != "databricks")'
' and extra != "postgis"'
' and (python_version == "3.12" or extra != "databricks")'
)
assert str(m1.intersect(m2)) == expected

Expand Down