Skip to content

Commit c6a2d6e

Browse files
authored
Improve the add and init commands (#1221)
* Add the ability to specify path and git dependencies directly in add * Add the ability to add git and path dependencies via the init command * Automatically select latest prereleases if only prereleases are available * Update documentation for the add command * Add the ability to add complete dependencies in one go
1 parent 6f4aa21 commit c6a2d6e

File tree

8 files changed

+891
-144
lines changed

8 files changed

+891
-144
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
- Added the ability to specify packages on a per-format basis.
1616
- Added support for custom urls in metadata.
1717
- Full environment markers are now supported for dependencies via the `markers` property.
18+
- Added the ability to specify git dependencies directly in `add`, it no longer requires the `--git` option.
19+
- Added the ability to specify path dependencies directly in `add`, it no longer requires the `--path` option.
20+
- Added the ability to add git and path dependencies via the `init` command.
1821

1922
### Changed
2023

@@ -24,6 +27,9 @@
2427
- The `debug:resolve` command has been renamed to `debug resolve`.
2528
- The `self:update` command has been renamed to `self update`.
2629
- Changed the way virtualenvs are stored (names now depend on the project's path).
30+
- The `--git` option of the `add` command has been removed.
31+
- The `--path` option of the `add` command has been removed.
32+
- The `add` command will now automatically select the latest prerelease if only prereleases are available.
2733

2834
### Fixed
2935

docs/docs/cli.md

+38-7
Original file line numberDiff line numberDiff line change
@@ -182,36 +182,67 @@ poetry will choose a suitable one based on the available package versions.
182182
poetry add requests pendulum
183183
```
184184

185+
You also can specify a constraint when adding a package, like so:
186+
187+
```bash
188+
poetry add pendulum@^2.0.5
189+
poetry add "pendulum>=2.0.5"
190+
```
191+
192+
If you try to add a package that is already present, you will get an error.
193+
However, if you specify a constraint, like above, the dependency will be updated
194+
by using the specified constraint. If you want to get the latest version of an already
195+
present dependency you can use the special `latest` constraint:
196+
197+
```bash
198+
poetry add pendulum@latest
199+
```
200+
185201
You can also add `git` dependencies:
186202

187203
```bash
188-
poetry add pendulum --git https://github.com/sdispater/pendulum.git
204+
poetry add git+https://github.com/sdispater/pendulum.git
205+
```
206+
207+
If you need to checkout a specific branch, tag or revision,
208+
you can specify it when using `add`:
209+
210+
```bash
211+
poetry add git+https://github.com/sdispater/pendulum.git@develop
212+
poetry add git+https://github.com/sdispater/[email protected]
189213
```
190214

191215
or make them point to a local directory or file:
192216

193217
```bash
194-
poetry add my-package --path ../my-package/
195-
poetry add my-package --path ../my-package/dist/my-package-0.1.0.tar.gz
196-
poetry add my-package --path ../my-package/dist/my_package-0.1.0.whl
218+
poetry add ./my-package/
219+
poetry add ../my-package/dist/my-package-0.1.0.tar.gz
220+
poetry add ../my-package/dist/my_package-0.1.0.whl
197221
```
198222

199223
Path dependencies pointing to a local directory will be installed in editable mode (i.e. setuptools "develop mode").
200224
It means that changes in the local directory will be reflected directly in environment.
201225

202226
If you don't want the dependency to be installed in editable mode you can specify it in the `pyproject.toml` file:
203227

204-
```
228+
```toml
205229
[tool.poetry.dependencies]
206230
my-package = {path = "../my/path", develop = false}
207231
```
208232

233+
If the package(s) you want to install provide extras, you can specify them
234+
when adding the package:
235+
236+
```bash
237+
poetry add requests[security,socks]
238+
poetry add "requests[security,socks]~=2.22.0"
239+
poetry add "git+https://github.com/pallets/[email protected][dotenv,dev]"
240+
```
241+
209242
### Options
210243

211244
* `--dev (-D)`: Add package as development dependency.
212-
* `--git`: The url of the Git repository.
213245
* `--path`: The path to a dependency.
214-
* `--extras (-E)`: Extras to activate for the dependency.
215246
* `--optional` : Add as an optional dependency.
216247
* `--dry-run` : Outputs the operations but will not execute anything (implicitly enables --verbose).
217248

poetry/console/commands/add.py

+19-32
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class AddCommand(EnvCommand, InitCommand):
1313
arguments = [argument("name", "Packages to add.", multiple=True)]
1414
options = [
1515
option("dev", "D", "Add package as development dependency."),
16-
option("git", None, "The url of the Git repository.", flag=False),
17-
option("path", None, "The path to a dependency.", flag=False),
1816
option(
1917
"extras",
2018
"E",
@@ -58,17 +56,11 @@ def handle(self):
5856
packages = self.argument("name")
5957
is_dev = self.option("dev")
6058

61-
if (self.option("git") or self.option("path") or self.option("extras")) and len(
62-
packages
63-
) > 1:
59+
if self.option("extras") and len(packages) > 1:
6460
raise ValueError(
65-
"You can only specify one package "
66-
"when using the --git or --path options"
61+
"You can only specify one package " "when using the --extras option"
6762
)
6863

69-
if self.option("git") and self.option("path"):
70-
raise RuntimeError("--git and --path cannot be used at the same time")
71-
7264
section = "dependencies"
7365
if is_dev:
7466
section = "dev-dependencies"
@@ -83,32 +75,27 @@ def handle(self):
8375
for name in packages:
8476
for key in poetry_content[section]:
8577
if key.lower() == name.lower():
78+
pair = self._parse_requirements([name])[0]
79+
if "git" in pair or pair.get("version") == "latest":
80+
continue
81+
8682
raise ValueError("Package {} is already present".format(name))
8783

88-
if self.option("git") or self.option("path"):
89-
requirements = {packages[0]: ""}
90-
else:
91-
requirements = self._determine_requirements(
92-
packages, allow_prereleases=self.option("allow-prereleases")
93-
)
94-
requirements = self._format_requirements(requirements)
84+
requirements = self._determine_requirements(
85+
packages, allow_prereleases=self.option("allow-prereleases")
86+
)
9587

96-
# validate requirements format
97-
for constraint in requirements.values():
98-
parse_constraint(constraint)
88+
for _constraint in requirements:
89+
if "version" in _constraint:
90+
# Validate version constraint
91+
parse_constraint(_constraint["version"])
9992

100-
for name, _constraint in requirements.items():
10193
constraint = inline_table()
102-
constraint["version"] = _constraint
103-
104-
if self.option("git"):
105-
del constraint["version"]
106-
107-
constraint["git"] = self.option("git")
108-
elif self.option("path"):
109-
del constraint["version"]
94+
for name, value in _constraint.items():
95+
if name == "name":
96+
continue
11097

111-
constraint["path"] = self.option("path")
98+
constraint[name] = value
11299

113100
if self.option("optional"):
114101
constraint["optional"] = True
@@ -135,7 +122,7 @@ def handle(self):
135122
if len(constraint) == 1 and "version" in constraint:
136123
constraint = constraint["version"]
137124

138-
poetry_content[section][name] = constraint
125+
poetry_content[section][_constraint["name"]] = constraint
139126

140127
# Write new content
141128
self.poetry.file.write(content)
@@ -152,7 +139,7 @@ def handle(self):
152139

153140
installer.dry_run(self.option("dry-run"))
154141
installer.update(True)
155-
installer.whitelist(requirements)
142+
installer.whitelist([r["name"] for r in requirements])
156143

157144
try:
158145
status = installer.run()

0 commit comments

Comments
 (0)