Skip to content

Commit

Permalink
pool: ensure sources are prioritised over PyPI
Browse files Browse the repository at this point in the history
When a project specifies non default sources, PyPI gets added as the
default source. This will prioritise packages available in PyPI when
the package exists in both index. This change ensures that PyPI is
only used as a default when no other sources are provided.

Resolves: python-poetry#1677 python-poetry#2564 python-poetry#3238
  • Loading branch information
abn committed Oct 19, 2020
1 parent 66d29f6 commit e7cc2d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
8 changes: 5 additions & 3 deletions poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def create_poetry(
)

# Configuring sources
for source in poetry.local_config.get("source", []):
sources = poetry.local_config.get("source", [])
for source in sources:
repository = self.create_legacy_repository(source, config)
is_default = source.get("default", False)
is_secondary = source.get("secondary", False)
Expand All @@ -88,9 +89,10 @@ def create_poetry(
poetry.pool.add_repository(repository, is_default, secondary=is_secondary)

# Always put PyPI last to prefer private repositories
# but only if we have no other default source
# but only if we have no other default sourced
if not poetry.pool.has_default():
poetry.pool.add_repository(PyPiRepository(), True)
has_sources = bool(sources)
poetry.pool.add_repository(PyPiRepository(), not has_sources, has_sources)
else:
if io.is_debug():
io.write_line("Deactivating the PyPI repository")
Expand Down
27 changes: 27 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from poetry.core.toml.file import TOMLFile
from poetry.factory import Factory
from poetry.repositories.legacy_repository import LegacyRepository
from poetry.repositories.pypi_repository import PyPiRepository
from poetry.utils._compat import PY2
from poetry.utils._compat import Path

Expand Down Expand Up @@ -150,6 +152,31 @@ def test_poetry_with_default_source():
assert 1 == len(poetry.pool.repositories)


def test_poetry_with_non_default_source():
poetry = Factory().create_poetry(fixtures_dir / "with_non_default_source")

assert len(poetry.pool.repositories) == 2

assert not poetry.pool.has_default()

assert poetry.pool.repositories[0].name == "foo"
assert isinstance(poetry.pool.repositories[0], LegacyRepository)

assert poetry.pool.repositories[1].name == "PyPI"
assert isinstance(poetry.pool.repositories[1], PyPiRepository)


def test_poetry_with_no_default_source():
poetry = Factory().create_poetry(fixtures_dir / "sample_project")

assert len(poetry.pool.repositories) == 1

assert poetry.pool.has_default()

assert poetry.pool.repositories[0].name == "PyPI"
assert isinstance(poetry.pool.repositories[0], PyPiRepository)


def test_poetry_with_two_default_sources():
with pytest.raises(ValueError) as e:
Factory().create_poetry(fixtures_dir / "with_two_default_sources")
Expand Down

0 comments on commit e7cc2d9

Please sign in to comment.