Skip to content

Commit

Permalink
factory: respect python/platform constraints
Browse files Browse the repository at this point in the history
This change ensures that when markers are specified, explicit python 
and platform constraints are not ignored.
  • Loading branch information
radoering authored Apr 26, 2022
1 parent 524de0b commit 26f2f5d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down Expand Up @@ -339,27 +338,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
Expand Down
59 changes: 59 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -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"


Expand Down Expand Up @@ -261,3 +267,56 @@ def test_create_poetry_with_groups_and_explicit_main():
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"'},
"*",
"<empty>",
),
],
)
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

0 comments on commit 26f2f5d

Please sign in to comment.