diff --git a/src/poetry/core/factory.py b/src/poetry/core/factory.py index cefc2ff9e..c110e7872 100644 --- a/src/poetry/core/factory.py +++ b/src/poetry/core/factory.py @@ -21,7 +21,6 @@ from poetry.core.packages.types import DependencyTypes from poetry.core.poetry import Poetry from poetry.core.spdx.license import License - from poetry.core.version.markers import BaseMarker DependencyConstraint = Union[str, Dict[str, Any]] DependencyConfig = Mapping[ @@ -335,27 +334,25 @@ def create_dependency( extras=constraint.get("extras", []), ) - if not markers: - marker: BaseMarker = AnyMarker() - if python_versions: - marker = marker.intersect( - parse_marker( - create_nested_marker( - "python_version", parse_constraint(python_versions) - ) + marker = parse_marker(markers) if markers else AnyMarker() + + if python_versions: + marker = marker.intersect( + parse_marker( + create_nested_marker( + "python_version", parse_constraint(python_versions) ) ) + ) - if platform: - marker = marker.intersect( - parse_marker( - create_nested_marker( - "sys_platform", parse_generic_constraint(platform) - ) + if platform: + marker = marker.intersect( + parse_marker( + create_nested_marker( + "sys_platform", parse_generic_constraint(platform) ) ) - else: - marker = parse_marker(markers) + ) if not marker.is_any(): dependency.marker = marker diff --git a/tests/test_factory.py b/tests/test_factory.py index 4834a8e44..ffd828528 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -1,13 +1,19 @@ from __future__ import annotations from pathlib import Path +from typing import TYPE_CHECKING import pytest from poetry.core.factory import Factory +from poetry.core.semver.helpers import parse_constraint from poetry.core.toml import TOMLFile +if TYPE_CHECKING: + from poetry.core.factory import DependencyConstraint + from poetry.core.version.markers import BaseMarker + fixtures_dir = Path(__file__).parent / "fixtures" @@ -261,3 +267,56 @@ def test_create_poetry_with_groups_and_explicit_default(): assert {dependency.name for dependency in dependencies} == { "aiohttp", } + + +@pytest.mark.parametrize( + "constraint, exp_python, exp_marker", + [ + ({"python": "3.7"}, "~3.7", 'python_version == "3.7"'), + ({"platform": "linux"}, "*", 'sys_platform == "linux"'), + ({"markers": 'python_version == "3.7"'}, "~3.7", 'python_version == "3.7"'), + ( + {"markers": 'platform_machine == "x86_64"'}, + "*", + 'platform_machine == "x86_64"', + ), + ( + {"python": "3.7", "markers": 'platform_machine == "x86_64"'}, + "~3.7", + 'platform_machine == "x86_64" and python_version == "3.7"', + ), + ( + {"platform": "linux", "markers": 'platform_machine == "x86_64"'}, + "*", + 'platform_machine == "x86_64" and sys_platform == "linux"', + ), + ( + { + "python": "3.7", + "platform": "linux", + "markers": 'platform_machine == "x86_64"', + }, + "~3.7", + 'platform_machine == "x86_64" and python_version == "3.7" and sys_platform' + ' == "linux"', + ), + ( + {"python": ">=3.7", "markers": 'python_version < "4.0"'}, + "<4.0 >=3.7", + 'python_version < "4.0" and python_version >= "3.7"', + ), + ( + {"platform": "linux", "markers": 'sys_platform == "win32"'}, + "*", + "", + ), + ], +) +def test_create_dependency_marker_variants( + constraint: DependencyConstraint, exp_python: str, exp_marker: BaseMarker +): + constraint["version"] = "1.0.0" + dep = Factory.create_dependency("foo", constraint) + assert dep.python_versions == exp_python + assert dep.python_constraint == parse_constraint(exp_python) + assert str(dep.marker) == exp_marker