Skip to content

Commit e16d0cd

Browse files
committed
add support for PEP 621: explain the difference between project.dependencies and tool.poetry.dependencies, update examples (#9135)
1 parent 8bb0c63 commit e16d0cd

6 files changed

+235
-42
lines changed

docs/basic-usage.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ The `pyproject.toml` file is what is the most important here. This will orchestr
3838
your project and its dependencies. For now, it looks like this:
3939

4040
```toml
41-
[tool.poetry]
41+
[project]
4242
name = "poetry-demo"
4343
version = "0.1.0"
4444
description = ""
45-
authors = ["Sébastien Eustace <[email protected]>"]
45+
authors = [
46+
{name = "Sébastien Eustace", email = "[email protected]"}
47+
]
4648
readme = "README.md"
47-
packages = [{include = "poetry_demo"}]
49+
requires-python = ">=3.8"
4850

49-
[tool.poetry.dependencies]
50-
python = "^3.7"
51+
[tool.poetry]
52+
packages = [{include = "poetry_demo"}]
5153

5254

5355
[build-system]
@@ -122,7 +124,20 @@ In the [pyproject section]({{< relref "pyproject" >}}) you can see which fields
122124

123125
### Specifying dependencies
124126

125-
If you want to add dependencies to your project, you can specify them in the `tool.poetry.dependencies` section.
127+
If you want to add dependencies to your project, you can specify them in the
128+
`project` or `tool.poetry.dependencies` section.
129+
See the [Dependency specification]({{< relref "dependency-specification" >}})
130+
for more information.
131+
132+
```toml
133+
[project]
134+
# ...
135+
dependencies = [
136+
"pendulum (>=2.1,<3.0)"
137+
]
138+
```
139+
140+
or
126141

127142
```toml
128143
[tool.poetry.dependencies]

docs/dependency-specification.md

+170-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,87 @@ menu:
1414
Dependencies for a project can be specified in various forms, which depend on the type
1515
of the dependency and on the optional constraints that might be needed for it to be installed.
1616

17+
## `project.dependencies` and `tool.poetry.dependencies`
18+
19+
Prior Poetry 2.0, dependencies had to be declared in the `tool.poetry.dependencies`
20+
section of the `pyproject.toml` file.
21+
22+
```toml
23+
[tool.poetry.dependencies]
24+
requests = "^2.13.0"
25+
```
26+
27+
With Poetry 2.0, you should consider using the `project.dependencies` section instead.
28+
29+
```toml
30+
[project]
31+
# ...
32+
dependencies = [
33+
"requests (>=2.23.0,<3.0.0)"
34+
]
35+
```
36+
37+
While dependencies in `tool.poetry.dependencies` are specified using toml tables,
38+
dependencies in `project.dependencies` are specified as strings according
39+
to [PEP 508](https://peps.python.org/pep-0508/).
40+
41+
In many cases, `tool.poetry.dependencies` can be replaced with `project.dependencies`.
42+
However, there are some cases where you might still need to use `tool.poetry.dependencies`.
43+
For example, if you want to define additional information that is not required for building
44+
but only for locking (for example an explicit source), you can enrich dependency
45+
information in the `tool.poetry` section.
46+
47+
```toml
48+
[project]
49+
# ...
50+
dependencies = [
51+
"requests>=2.13.0",
52+
]
53+
54+
[tool.poetry.dependencies]
55+
requests = { source = "private-source" }
56+
```
57+
58+
When both are specified, `project.dependencies` are used for metadata when building the project,
59+
`tool.poetry.dependencies` is only used to enrich `project.dependencies` for locking.
60+
61+
Alternatively, you can add `dependencies` to `dynamic` and define your dependencies
62+
completely in the `tool.poetry` section. Using only the `tool.poetry` section might
63+
make sense in non-package mode when you will not build an sdist or a wheel.
64+
65+
```toml
66+
[project]
67+
# ...
68+
dynamic = [ "dependencies" ]
69+
70+
[tool.poetry.dependencies]
71+
requests = { version = ">=2.13.0", source = "private-source" }
72+
```
73+
74+
{{% note %}}
75+
Another use case for `tool.poetry.dependencies` are relative path dependencies
76+
since `project.dependencies` only support absolute paths.
77+
{{% /note %}}
78+
79+
{{% note %}}
80+
Only main dependencies can be specified in the `project` section.
81+
Other [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}})
82+
must still be specified in the `tool.poetry` section.
83+
{{% /note %}}
84+
1785
## Version constraints
1886

87+
{{% warning %}}
88+
Some of the following constraints can only be used in `tool.poetry.dependencies` and not in `project.dependencies`.
89+
When using `poetry add` such constraints are automatically converted into an equivalent constraint.
90+
{{% /warning %}}
91+
1992
### Caret requirements
2093

94+
{{% warning %}}
95+
Not supported in `project.dependencies`.
96+
{{% /warning %}}
97+
2198
**Caret requirements** allow [SemVer](https://semver.org/) compatible updates to a specified version. An update is allowed if the new version number does not modify the left-most non-zero digit in the major, minor, patch grouping. For instance, if we previously ran `poetry add requests@^2.13.0` and wanted to update the library and ran `poetry update requests`, poetry would update us to version `2.14.0` if it was available, but would not update us to `3.0.0`. If instead we had specified the version string as `^0.1.13`, poetry would update to `0.1.14` but not `0.2.0`. `0.0.x` is not considered compatible with any other version.
2299

23100
Here are some more examples of caret requirements and the versions that would be allowed with them:
@@ -34,6 +111,10 @@ Here are some more examples of caret requirements and the versions that would be
34111

35112
### Tilde requirements
36113

114+
{{% warning %}}
115+
Not supported in `project.dependencies`.
116+
{{% /warning %}}
117+
37118
**Tilde requirements** specify a minimal version with some ability to update.
38119
If you specify a major, minor, and patch version or only a major and minor version, only patch-level changes are allowed.
39120
If you only specify a major version, then minor- and patch-level changes are allowed.
@@ -131,6 +212,16 @@ the minimum information you need to specify is the location of the repository wi
131212
requests = { git = "https://github.com/requests/requests.git" }
132213
```
133214

215+
or in the `project` section:
216+
217+
```toml
218+
[project]
219+
# ...
220+
dependencies = [
221+
"requests @ git+https://github.com/requests/requests.git"
222+
]
223+
```
224+
134225
Since we haven’t specified any other information,
135226
Poetry assumes that we intend to use the latest commit on the `main` branch
136227
to build our project.
@@ -149,6 +240,18 @@ flask = { git = "https://github.com/pallets/flask.git", rev = "38eb5d3b" }
149240
numpy = { git = "https://github.com/numpy/numpy.git", tag = "v0.13.2" }
150241
```
151242

243+
or in the `project` section:
244+
245+
```toml
246+
[project]
247+
# ...
248+
dependencies = [
249+
"requests @ git+https://github.com/requests/requests.git@next",
250+
"flask @ git+https://github.com/pallets/flask.git@38eb5d3b",
251+
"numpy @ git+https://github.com/numpy/[email protected]",
252+
]
253+
```
254+
152255
In cases where the package you want to install is located in a subdirectory of the VCS repository, you can use the `subdirectory` option, similarly to what [pip](https://pip.pypa.io/en/stable/topics/vcs-support/#url-fragments) provides:
153256

154257
```toml
@@ -212,6 +315,16 @@ my-package = { path = "../my-package/", develop = false }
212315
my-package = { path = "../my-package/dist/my-package-0.1.0.tar.gz" }
213316
```
214317

318+
In the `project` section, you can only use absolute paths:
319+
320+
```toml
321+
[project]
322+
# ...
323+
dependencies = [
324+
"my-package @ file:///absolute/path/to/my-package/dist/my-package-0.1.0.tar.gz"
325+
]
326+
```
327+
215328
{{% note %}}
216329
Before poetry 1.1 directory path dependencies were installed in editable mode by default. You should set the `develop` attribute explicitly,
217330
to make sure the behavior is the same for all poetry versions.
@@ -228,6 +341,16 @@ you can use the `url` property:
228341
my-package = { url = "https://example.com/my-package-0.1.0.tar.gz" }
229342
```
230343

344+
or in the `project` section:
345+
346+
```toml
347+
[project]
348+
# ...
349+
dependencies = [
350+
"my-package @ https://example.com/my-package-0.1.0.tar.gz"
351+
]
352+
```
353+
231354
with the corresponding `add` call:
232355

233356
```bash
@@ -244,6 +367,16 @@ for a dependency as shown here.
244367
gunicorn = { version = "^20.1", extras = ["gevent"] }
245368
```
246369

370+
or in the `project` section:
371+
372+
```toml
373+
[project]
374+
# ...
375+
dependencies = [
376+
"gunicorn[gevent] (>=20.1,<21.0)"
377+
]
378+
```
379+
247380
{{% note %}}
248381
These activate extra defined for the dependency, to configure an optional dependency
249382
for extras in your project refer to [`extras`]({{< relref "pyproject#extras" >}}).
@@ -275,6 +408,10 @@ In this example, we expect `foo` to be configured correctly. See [using a privat
275408
for further information.
276409
{{% /note %}}
277410

411+
{{% note %}}
412+
It is not possible to define source dependencies in the `project` section.
413+
{{% /note %}}
414+
278415
## Python restricted dependencies
279416

280417
You can also specify that a dependency should be installed only for specific Python versions:
@@ -286,7 +423,18 @@ tomli = { version = "^2.0.1", python = "<3.11" }
286423

287424
```toml
288425
[tool.poetry.dependencies]
289-
pathlib2 = { version = "^2.2", python = "^3.2" }
426+
pathlib2 = { version = "^2.2", python = "^3.9" }
427+
```
428+
429+
or in the `project` section:
430+
431+
```toml
432+
[project]
433+
# ...
434+
dependencies = [
435+
"tomli (>=2.0.1,<3.11) ; python_version < '3.11'",
436+
"pathlib2 (>=2.2,<3.0) ; python_version >= '3.9' and python_version < '4.0'"
437+
]
290438
```
291439

292440
## Using environment markers
@@ -300,6 +448,16 @@ via the `markers` property:
300448
pathlib2 = { version = "^2.2", markers = "python_version <= '3.4' or sys_platform == 'win32'" }
301449
```
302450

451+
or in the `project` section:
452+
453+
```toml
454+
[project]
455+
# ...
456+
dependencies = [
457+
"pathlib2 (>=2.2,<3.0) ; python_version <= '3.4' or sys_platform == 'win32'"
458+
]
459+
```
460+
303461
## Multiple constraints dependencies
304462

305463
Sometimes, one of your dependency may have different version ranges depending
@@ -317,6 +475,17 @@ foo = [
317475
]
318476
```
319477

478+
or in the `project` section:
479+
480+
```toml
481+
[project]
482+
# ...
483+
dependencies = [
484+
"foo (<=1.9) ; python_version >= '3.6' and python_version < '3.8'",
485+
"foo (>=2.0,<3.0) ; python_version >= '3.8'"
486+
]
487+
```
488+
320489
{{% note %}}
321490
The constraints **must** have different requirements (like `python`)
322491
otherwise it will cause an error when resolving dependencies.

docs/faq.md

+12
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ Usually you will want to match the supported Python range of your project with t
211211
Alternatively you can tell Poetry to install this dependency [only for a specific range of Python versions]({{< relref "dependency-specification#multiple-constraints-dependencies" >}}),
212212
if you know that it's not needed in all versions.
213213

214+
If you do not want to set an upper bound in the metadata when building your project,
215+
you can omit it in the `project` section and only set it in `tool.poetry.dependencies`:
216+
217+
```toml
218+
[project]
219+
# ...
220+
requires-python = ">=3.7" # used for metadata when building the project
221+
222+
[tool.poetry.dependencies]
223+
python = ">=3.7,<3.11" # used for locking dependencies
224+
```
225+
214226

215227
### Why does Poetry enforce PEP 440 versions?
216228

docs/managing-dependencies.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ type: docs
1111

1212
# Managing dependencies
1313

14+
{{% note %}}
15+
Since Poetry 2.0, main dependencies can be specified in `project.dependencies`
16+
instead of `tool.poetry.dependencies`.
17+
See [Dependency specification]({{< relref "dependency-specification" >}}) for more information.
18+
Only main dependencies can be specified in the `project` section.
19+
Other groups must still be specified in the `tool.poetry` section.
20+
{{% /note %}}
21+
1422
## Dependency groups
1523

1624
Poetry provides a way to **organize** your dependencies by **groups**. For instance, you might have
@@ -37,7 +45,22 @@ the dependencies logically.
3745
{{% /note %}}
3846

3947
{{% note %}}
40-
The dependencies declared in `tool.poetry.dependencies` are part of an implicit `main` group.
48+
The dependencies declared in `project.dependencies` respectively `tool.poetry.dependencies`
49+
are part of an implicit `main` group.
50+
{{% /note %}}
51+
52+
```toml
53+
[project]
54+
# ...
55+
dependencies = [ # main dependency group
56+
"httpx",
57+
"pendulum",
58+
]
59+
60+
[tool.poetry.group.test.dependencies]
61+
pytest = "^6.0.0"
62+
pytest-mock = "*"
63+
```
4164

4265
```toml
4366
[tool.poetry.dependencies] # main dependency group
@@ -48,7 +71,6 @@ pendulum = "*"
4871
pytest = "^6.0.0"
4972
pytest-mock = "*"
5073
```
51-
{{% /note %}}
5274

5375
{{% note %}}
5476
Dependency groups, other than the implicit `main` group, must only contain dependencies you need in your development

docs/plugins.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ The plugin package must depend on Poetry
3232
and declare a proper [plugin]({{< relref "pyproject#plugins" >}}) in the `pyproject.toml` file.
3333

3434
```toml
35-
[tool.poetry]
35+
[project]
3636
name = "my-poetry-plugin"
3737
version = "1.0.0"
38-
3938
# ...
40-
[tool.poetry.dependencies]
41-
python = "^3.7"
42-
poetry = "^1.2"
39+
requires-python = ">=3.7"
40+
dependencies = [
41+
"poetry (>=1.2,<2.0)",
42+
]
4343

44-
[tool.poetry.plugins."poetry.plugin"]
44+
[project.entry-points."poetry.plugin"]
4545
demo = "poetry_demo_plugin.plugin:MyPlugin"
4646
```
4747

0 commit comments

Comments
 (0)