Skip to content
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
17 changes: 9 additions & 8 deletions src/poetry/repositories/link_sources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ class SimpleRepositoryRootPage:
"""

def search(self, query: str | list[str]) -> list[str]:
results: list[str] = []
tokens = query if isinstance(query, list) else [query]

for name in self.package_names:
if any(token in name for token in tokens):
results.append(name)

return results
if isinstance(query, str):
# performance shortcut
# We could also create a list from query and use the more general code below,
# but this is a common case that we can optimize for.
return [name for name in self.package_names if query in name]

return [
name for name in self.package_names if any(token in name for token in query)
]

@cached_property
def package_names(self) -> list[str]:
Expand Down
17 changes: 10 additions & 7 deletions src/poetry/repositories/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ def add_package(self, package: Package) -> None:
self._packages.append(package)

def search(self, query: str | list[str]) -> list[Package]:
results: list[Package] = []
tokens = query if isinstance(query, list) else [query]
if isinstance(query, str):
# performance shortcut
# We could also create a list from query and use the more general code below,
# but this is a common case that we can optimize for.
return [package for package in self.packages if query in package.name]

for package in self.packages:
if any(token in package.name for token in tokens):
results.append(package)

return results
return [
package
for package in self.packages
if any(token in package.name for token in query)
]

def _find_packages(
self, name: NormalizedName, constraint: VersionConstraint
Expand Down
1 change: 1 addition & 0 deletions tests/repositories/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ def test_search() -> None:

assert repo.search("foo") == [package_foo1, package_foo2, package_foobar]
assert repo.search("bar") == [package_foobar]
assert repo.search(["foo", "bar"]) == [package_foo1, package_foo2, package_foobar]
assert repo.search("nothing") == []
Loading