Skip to content

Commit

Permalink
sources: deprecate priority 'default'
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Jan 27, 2024
1 parent 8904766 commit 59e2ebe
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
21 changes: 15 additions & 6 deletions docs/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ priority = "primary"
If `priority` is undefined, the source is considered a primary source that takes precedence over PyPI, secondary, supplemental and explicit sources.

Package sources are considered in the following order:
1. [default source](#default-package-source),
1. [default source](#default-package-source-deprecated) (DEPRECATED),
2. [primary sources](#primary-package-sources),
3. implicit PyPI (unless disabled by another [primary source](#primary-package-sources), [default source](#default-package-source) or configured explicitly),
3. implicit PyPI (unless disabled by another [primary source](#primary-package-sources), [default source](#default-package-source-deprecated) or configured explicitly),
4. [secondary sources](#secondary-package-sources-deprecated) (DEPRECATED),
5. [supplemental sources](#supplemental-package-sources).

Expand All @@ -145,20 +145,29 @@ poetry source add --priority=primary PyPI
```

If you prefer to disable PyPI completely,
you may choose to set one of your package sources to be the [default](#default-package-source),
just add a [primary source](#primary-package-sources)
or configure PyPI as [explicit source](#explicit-package-sources).

{{% /note %}}


#### Default Package Source
#### Default Package Source (DEPRECATED)

*Deprecated in 1.8.0*

{{% warning %}}

Configuring a default package source is deprecated because it is the same
as the topmost [primary source](#primary-package-sources).
Just configure a primary package source and put it first in the list of package sources.

{{% /warning %}}

By default, if you have not configured any primary source,
Poetry will configure [PyPI](https://pypi.org) as the package source for your project.
You can alter this behaviour and exclusively look up packages only from the configured
package sources by adding at least one primary source
or a **single** source with `priority = "default"`.
package sources by adding at least one primary source (recommended)
or a **single** source with `priority = "default"` (deprecated).

```bash
poetry source add --priority=default foo https://foo.bar/simple/
Expand Down
10 changes: 9 additions & 1 deletion src/poetry/console/commands/source/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,20 @@ def handle(self) -> int:
priority = Priority[priority_str.upper()]

if priority is Priority.SECONDARY:
allowed_prios = (p for p in Priority if p is not Priority.SECONDARY)
allowed_prios = (
p for p in Priority if p not in {Priority.DEFAULT, Priority.SECONDARY}
)
self.line_error(
"<warning>Warning: Priority 'secondary' is deprecated. Consider"
" changing the priority to one of the non-deprecated values:"
f" {', '.join(repr(p.name.lower()) for p in allowed_prios)}.</warning>"
)
if priority is Priority.DEFAULT:
self.line_error(
"<warning>Warning: Priority 'default' is deprecated. You can achieve"
" the same effect by changing the priority to 'primary' and putting"
" the source first.</warning>"
)

sources = AoT([])
new_source = Source(name=name, url=url, priority=priority)
Expand Down
8 changes: 8 additions & 0 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def create_pool(
f" {', '.join(repr(p.name.lower()) for p in allowed_prios)}."
)
io.write_error_line(f"<warning>Warning: {warning}</warning>")
elif priority is Priority.DEFAULT:
warning = (
"Found deprecated priority 'default' for source"
f" '{source.get('name')}' in pyproject.toml. You can achieve"
" the same effect by changing the priority to 'primary' and putting"
" the source first."
)
io.write_error_line(f"<warning>Warning: {warning}</warning>")

if io.is_debug():
message = f"Adding repository {repository.name} ({repository.url})"
Expand Down
51 changes: 30 additions & 21 deletions tests/console/commands/source/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,34 @@ def tester(
return command_tester_factory("source add", poetry=poetry_with_source)


def _get_source_warning(priority: Priority) -> str:
if priority is Priority.SECONDARY:
return (
"Warning: Priority 'secondary' is deprecated. Consider changing the"
" priority to one of the non-deprecated values: 'primary',"
" 'supplemental', 'explicit'."
)
elif priority is Priority.DEFAULT:
return (
"Warning: Priority 'default' is deprecated. You can achieve"
" the same effect by changing the priority to 'primary' and putting"
" the source first."
)
return ""


def assert_source_added_legacy(
tester: CommandTester,
poetry: Poetry,
source_existing: Source,
source_added: Source,
) -> None:
secondary_deprecated_str = (
""
if source_added.priority is not Priority.SECONDARY
else (
"\nWarning: Priority 'secondary' is deprecated. Consider changing the"
" priority to one of the non-deprecated values: 'default', 'primary',"
" 'supplemental', 'explicit'."
)
)
assert (
tester.io.fetch_error().strip()
== "Warning: Priority was set through a deprecated flag (--default or"
" --secondary). Consider using --priority next time."
+ secondary_deprecated_str
warning = (
"Warning: Priority was set through a deprecated flag (--default or"
" --secondary). Consider using --priority next time.\n"
+ _get_source_warning(source_added.priority)
)
assert tester.io.fetch_error().strip() == warning
assert (
tester.io.fetch_output().strip()
== f"Adding source with name {source_added.name}."
Expand All @@ -59,6 +66,7 @@ def assert_source_added(
source_existing: Source,
source_added: Source,
) -> None:
assert tester.io.fetch_error().strip() == _get_source_warning(source_added.priority)
assert (
tester.io.fetch_output().strip()
== f"Adding source with name {source_added.name}."
Expand Down Expand Up @@ -126,9 +134,9 @@ def test_source_add_second_default_fails(
tester.execute(f"--priority=default {source_default.name}1 {source_default.url}")
assert (
tester.io.fetch_error().strip()
== f"Source with name {source_default.name} is already set to"
" default. Only one default source can be configured at a"
" time."
== f"{_get_source_warning(source_default.priority)}\n"
f"Source with name {source_default.name} is already set to default."
" Only one default source can be configured at a time."
)
assert tester.status_code == 1

Expand Down Expand Up @@ -238,7 +246,7 @@ def test_source_add_existing_legacy(
tester.io.fetch_error().strip()
== "Warning: Priority was set through a deprecated flag"
" (--default or --secondary). Consider using --priority next"
" time."
f" time.\n{_get_source_warning(Priority.DEFAULT)}"
)
assert (
tester.io.fetch_output().strip()
Expand Down Expand Up @@ -313,16 +321,17 @@ def test_source_add_existing_fails_due_to_other_default(
poetry_with_source: Poetry,
) -> None:
tester.execute(f"--priority=default {source_default.name} {source_default.url}")
tester.io.fetch_error()
tester.io.fetch_output()

name = getattr(source_existing.name, modifier)()
tester.execute(f"--priority=default {name} {source_existing.url}")

assert (
tester.io.fetch_error().strip()
== f"Source with name {source_default.name} is already set to"
" default. Only one default source can be configured at a"
" time."
== f"{_get_source_warning(source_default.priority)}\n"
f"Source with name {source_default.name} is already set to default."
" Only one default source can be configured at a time."
)
assert tester.io.fetch_output().strip() == ""
assert tester.status_code == 1
7 changes: 6 additions & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ def test_poetry_with_default_source(
poetry = Factory().create_poetry(fixture_dir("with_default_source"), io=io)

assert len(poetry.pool.repositories) == 1
assert io.fetch_error() == ""
assert (
io.fetch_error().strip()
== "<warning>Warning: Found deprecated priority 'default' for source 'foo' in"
" pyproject.toml. You can achieve the same effect by changing the priority"
" to 'primary' and putting the source first."
)


def test_poetry_with_default_source_and_pypi(
Expand Down

0 comments on commit 59e2ebe

Please sign in to comment.