Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect --no-index from the requirements file #11277

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/11276.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``--no-index`` when ``--index-url`` or ``--extra-index-url`` is specified
inside a requirements file.
1 change: 1 addition & 0 deletions src/pip/_internal/index/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ def create(
search_scope = SearchScope.create(
find_links=find_links,
index_urls=index_urls,
no_index=options.no_index,
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
)
link_collector = LinkCollector(
session=session,
Expand Down
6 changes: 5 additions & 1 deletion src/pip/_internal/models/search_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class SearchScope:
Encapsulates the locations that pip is configured to search.
"""

__slots__ = ["find_links", "index_urls"]
__slots__ = ["find_links", "index_urls", "no_index"]

@classmethod
def create(
cls,
find_links: List[str],
index_urls: List[str],
no_index: bool,
) -> "SearchScope":
"""
Create a SearchScope object after normalizing the `find_links`.
Expand Down Expand Up @@ -60,15 +61,18 @@ def create(
return cls(
find_links=built_find_links,
index_urls=index_urls,
no_index=no_index,
)

def __init__(
self,
find_links: List[str],
index_urls: List[str],
no_index: bool,
) -> None:
self.find_links = find_links
self.index_urls = index_urls
self.no_index = no_index

def get_formatted_locations(self) -> str:
lines = []
Expand Down
9 changes: 6 additions & 3 deletions src/pip/_internal/req/req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,13 @@ def handle_option_line(
if finder:
find_links = finder.find_links
index_urls = finder.index_urls
if opts.index_url:
index_urls = [opts.index_url]
no_index = finder.search_scope.no_index
if opts.no_index is True:
no_index = True
index_urls = []
if opts.extra_index_urls:
if opts.index_url and not no_index:
index_urls = [opts.index_url]
if opts.extra_index_urls and not no_index:
index_urls.extend(opts.extra_index_urls)
if opts.find_links:
# FIXME: it would be nice to keep track of the source
Expand All @@ -253,6 +255,7 @@ def handle_option_line(
search_scope = SearchScope(
find_links=find_links,
index_urls=index_urls,
no_index=no_index,
)
finder.search_scope = search_scope

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def run_with_build_env(

link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope.create([{scratch!r}], []),
search_scope=SearchScope.create([{scratch!r}], [], False),
)
selection_prefs = SelectionPreferences(
allow_yanked=True,
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def make_test_search_scope(
if index_urls is None:
index_urls = []

return SearchScope.create(find_links=find_links, index_urls=index_urls)
return SearchScope.create(
find_links=find_links,
index_urls=index_urls,
no_index=False,
)


def make_test_link_collector(
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/resolution_resolvelib/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@pytest.fixture
def finder(data: TestData) -> Iterator[PackageFinder]:
session = PipSession()
scope = SearchScope([str(data.packages)], [])
scope = SearchScope([str(data.packages)], [], False)
collector = LinkCollector(session, scope)
prefs = SelectionPreferences(allow_yanked=False)
finder = PackageFinder.create(collector, prefs)
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def test_create__candidate_prefs(
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
selection_prefs = SelectionPreferences(
allow_yanked=True,
Expand All @@ -614,7 +614,7 @@ def test_create__link_collector(self) -> None:
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
finder = PackageFinder.create(
link_collector=link_collector,
Expand All @@ -629,7 +629,7 @@ def test_create__target_python(self) -> None:
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
target_python = TargetPython(py_version_info=(3, 7, 3))
finder = PackageFinder.create(
Expand All @@ -649,7 +649,7 @@ def test_create__target_python_none(self) -> None:
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
finder = PackageFinder.create(
link_collector=link_collector,
Expand All @@ -668,7 +668,7 @@ def test_create__allow_yanked(self, allow_yanked: bool) -> None:
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
selection_prefs = SelectionPreferences(allow_yanked=allow_yanked)
finder = PackageFinder.create(
Expand All @@ -684,7 +684,7 @@ def test_create__ignore_requires_python(self, ignore_requires_python: bool) -> N
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
selection_prefs = SelectionPreferences(
allow_yanked=True,
Expand All @@ -702,7 +702,7 @@ def test_create__format_control(self) -> None:
"""
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
format_control = FormatControl(set(), {":all:"})
selection_prefs = SelectionPreferences(
Expand Down Expand Up @@ -743,7 +743,7 @@ def test_make_link_evaluator(

link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)

finder = PackageFinder(
Expand Down Expand Up @@ -793,7 +793,7 @@ def test_make_candidate_evaluator(
)
link_collector = LinkCollector(
session=PipSession(),
search_scope=SearchScope([], []),
search_scope=SearchScope([], [], False),
)
finder = PackageFinder(
link_collector=link_collector,
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ def test_set_finder_no_index(
line_processor("--no-index", "file", 1, finder=finder)
assert finder.index_urls == []

def test_set_finder_no_index_is_remembered_for_later_invocations(
self, line_processor: LineProcessor, finder: PackageFinder
) -> None:
line_processor("--no-index", "file", 1, finder=finder)
line_processor("--index-url=url", "file", 1, finder=finder)
assert finder.index_urls == []

def test_set_finder_index_url(
self, line_processor: LineProcessor, finder: PackageFinder, session: PipSession
) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_search_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_get_formatted_locations_basic_auth(self) -> None:
search_scope = SearchScope(
find_links=find_links,
index_urls=index_urls,
no_index=False,
)

result = search_scope.get_formatted_locations()
Expand All @@ -29,6 +30,7 @@ def test_get_index_urls_locations(self) -> None:
search_scope = SearchScope(
find_links=[],
index_urls=["file://index1/", "file://index2"],
no_index=False,
)
req = install_req_from_line("Complex_Name")
assert req.name is not None
Expand Down