Commit a408cb14 by Sébastien Eustace Committed by GitHub

Add support for multi-constraints dependencies (#406)

parent 68c39f8f
......@@ -128,3 +128,26 @@ pathlib2 = { version = "^2.2", python = "~2.7" }
[tool.poetry.dependencies]
pathlib2 = { version = "^2.2", python = ["~2.7", "^3.2"] }
```
### Multiple constraints dependencies
Sometimes, one of your dependency may have different version ranges depending
on the target Python versions.
Let's say you have a dependency on the package `foo` which is only compatible
with Python <3.0 up to version 1.9 and compatible with Python 3.4+ from version 2.0:
you would declare it like so:
```toml
[tool.poetry.dependencies]
foo = [
{version = "<=1.9", python = "^2.7"},
{version = "^2.0", python = "^3.4"}
]
```
!!!note
The constraints **must** have different requirements (like `python`)
otherwise it will cause an error when resolving dependencies.
......@@ -183,6 +183,9 @@
},
{
"$ref": "#/definitions/path-dependency"
},
{
"$ref": "#/definitions/multiple-constraints-dependency"
}
]
}
......@@ -345,6 +348,29 @@
}
}
},
"multiple-constraints-dependency": {
"type": "array",
"minItems": 1,
"items": {
"oneOf": [
{
"$ref": "#/definitions/dependency"
},
{
"$ref": "#/definitions/long-dependency"
},
{
"$ref": "#/definitions/git-dependency"
},
{
"$ref": "#/definitions/file-dependency"
},
{
"$ref": "#/definitions/path-dependency"
}
]
}
},
"repository": {
"type": "object",
"properties": {
......
......@@ -120,10 +120,22 @@ class Poetry:
package.python_versions = constraint
continue
if isinstance(constraint, list):
for _constraint in constraint:
package.add_dependency(name, _constraint)
continue
package.add_dependency(name, constraint)
if "dev-dependencies" in local_config:
for name, constraint in local_config["dev-dependencies"].items():
if isinstance(constraint, list):
for _constraint in constraint:
package.add_dependency(name, _constraint)
continue
package.add_dependency(name, constraint, category="dev")
extras = local_config.get("extras", {})
......
[tool.poetry]
name = "project-with-multi-constraints-dependency"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <you@example.com>"]
license = "MIT"
packages = [
{include = "project"}
]
[tool.poetry.dependencies]
python = "*"
pendulum = [
{ version = "^1.5", python = "<3.4" },
{ version = "^2.0", python = "^3.4" }
]
[tool.poetry.dev-dependencies]
......@@ -197,6 +197,45 @@ def test_find_files_to_add():
)
def test_make_pkg_info_multi_constraints_dependency():
poetry = Poetry.create(
Path(__file__).parent.parent.parent
/ "fixtures"
/ "project_with_multi_constraints_dependency"
)
builder = SdistBuilder(poetry, NullEnv(), NullIO())
pkg_info = builder.build_pkg_info()
p = Parser()
parsed = p.parsestr(to_str(pkg_info))
requires = parsed.get_all("Requires-Dist")
assert requires == [
'pendulum (>=1.5,<2.0); python_version < "3.4"',
'pendulum (>=2.0,<3.0); python_version >= "3.4" and python_version < "4.0"',
]
def test_find_files_to_add():
poetry = Poetry.create(project("complete"))
builder = SdistBuilder(poetry, NullEnv(), NullIO())
result = builder.find_files_to_add()
assert sorted(result) == sorted(
[
Path("LICENSE"),
Path("README.rst"),
Path("my_package/__init__.py"),
Path("my_package/data1/test.json"),
Path("my_package/sub_pkg1/__init__.py"),
Path("my_package/sub_pkg2/__init__.py"),
Path("my_package/sub_pkg2/data2/data.json"),
Path("pyproject.toml"),
]
)
def test_find_packages():
poetry = Poetry.create(project("complete"))
......
......@@ -123,6 +123,16 @@ def test_poetry_with_packages_and_includes():
assert package.include == ["extra_dir/vcs_excluded.txt", "notes.txt"]
def test_poetry_with_multi_constraints_dependency():
poetry = Poetry.create(
str(fixtures_dir / "project_with_multi_constraints_dependency")
)
package = poetry.package
assert len(package.requires) == 2
def test_check():
complete = TomlFile(fixtures_dir / "complete.toml")
content = complete.read()["tool"]["poetry"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment