From e89675ee7ea705d65920f62eff2c887d6042e8c5 Mon Sep 17 00:00:00 2001 From: Arun Babu Neelicattu Date: Tue, 7 Jan 2025 01:42:05 +0100 Subject: [PATCH] feat(source/show): notify when PyPI is implicit Add a notice when all sources are listed and PyPI is implicitly enabled. --- src/poetry/console/commands/source/show.py | 22 +++++++++++++++++++++- tests/console/commands/source/test_show.py | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/poetry/console/commands/source/show.py b/src/poetry/console/commands/source/show.py index 61b3375ba53..97172e8cb90 100644 --- a/src/poetry/console/commands/source/show.py +++ b/src/poetry/console/commands/source/show.py @@ -26,13 +26,25 @@ class SourceShowCommand(Command): ), ] + def notify_implicit_pypi(self) -> None: + if not self.poetry.pool.has_repository("pypi"): + return + + self.line( + "PyPI is implicitly enabled as a primary source. " + "If you wish to disable it, or alter its priority please refer to " + "https://python-poetry.org/docs/repositories/#package-sources." + ) + self.line("") + def handle(self) -> int: sources = self.poetry.get_sources() names = self.argument("source") lower_names = [name.lower() for name in names] if not sources: - self.line("No sources configured for this project.") + self.line("No sources configured for this project.\n") + self.notify_implicit_pypi() return 0 if names and not any(s.name.lower() in lower_names for s in sources): @@ -42,10 +54,15 @@ def handle(self) -> int: ) return 1 + is_pypi_implicit = True + for source in sources: if names and source.name.lower() not in lower_names: continue + if source.name.lower() == "pypi": + is_pypi_implicit = False + table = self.table(style="compact") rows: Rows = [["name", f" : {source.name}"]] if source.url: @@ -55,4 +72,7 @@ def handle(self) -> int: table.render() self.line("") + if not names and is_pypi_implicit: + self.notify_implicit_pypi() + return 0 diff --git a/tests/console/commands/source/test_show.py b/tests/console/commands/source/test_show.py index bee08603d5c..d598034b636 100644 --- a/tests/console/commands/source/test_show.py +++ b/tests/console/commands/source/test_show.py @@ -4,6 +4,8 @@ import pytest +from poetry.repositories.pypi_repository import PyPiRepository + if TYPE_CHECKING: from cleo.testers.command_tester import CommandTester @@ -178,6 +180,20 @@ def test_source_show_no_sources(tester_no_sources: CommandTester) -> None: assert tester_no_sources.status_code == 0 +def test_source_show_no_sources_implicit_pypi( + tester_no_sources: CommandTester, poetry_without_source: Poetry +) -> None: + poetry_without_source.pool.add_repository(PyPiRepository()) + tester_no_sources.execute("") + + output = tester_no_sources.io.fetch_output().strip() + + assert "No sources configured for this project." in output + assert "PyPI is implicitly enabled as a primary source." in output + + assert tester_no_sources.status_code == 0 + + def test_source_show_error(tester: CommandTester) -> None: tester.execute("error") assert tester.io.fetch_error().strip() == "No source found with name(s): error"