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

fix(search): fallback to find packages for PyPI #10055

Merged
merged 2 commits into from
Jan 16, 2025

Conversation

abn
Copy link
Member

@abn abn commented Jan 15, 2025

If not results are found when searching PyPI fallback to using the find package mechanism to at the minimum list any matches for valid PEP 508 specifications.

Resolves: #9884

Also includes a minor cleanup commit.

Summary by Sourcery

Bug Fixes:

  • Fix an issue where searches against PyPI would return no results if the search functionality was unavailable.

@abn abn requested a review from a team January 15, 2025 21:09
Copy link

sourcery-ai bot commented Jan 15, 2025

Reviewer's Guide by Sourcery

This pull request implements a fallback mechanism for PyPI searches. If the initial search yields no results, the code now attempts to find packages using find_packages to handle cases where PyPI search is unavailable. It also includes a minor cleanup of unused imports in the tests.

Sequence diagram for PyPI package search with fallback

sequenceDiagram
    participant Client
    participant PyPIRepository
    participant PyPI

    Client->>PyPIRepository: search(query)
    PyPIRepository->>PyPI: Initial search request
    alt Search successful
        PyPI-->>PyPIRepository: Return search results
        PyPIRepository-->>Client: Return packages
    else No results found
        PyPI-->>PyPIRepository: Empty results
        Note over PyPIRepository: Convert query to PEP 508
        PyPIRepository->>PyPI: find_packages(dependency)
        PyPI-->>PyPIRepository: Return matching packages
        PyPIRepository-->>Client: Return packages
    end
Loading

Flow diagram for PyPI search logic

flowchart TD
    A[Start Search] --> B{Search PyPI}
    B -->|Results found| C[Return Results]
    B -->|No results| D[Split query into tokens]
    D --> E[Process each token]
    E --> F{Valid PEP 508?}
    F -->|Yes| G[Find packages for dependency]
    F -->|No| H[Skip token]
    G --> I[Add to results]
    H --> J{More tokens?}
    I --> J
    J -->|Yes| E
    J -->|No| K[Return combined results]
Loading

File-Level Changes

Change Details Files
Implement fallback to find packages if PyPI search returns no results
  • Added a fallback mechanism to use find_packages if the initial PyPI search returns no results.
  • Handles cases where PyPI search is unavailable, improving user experience.
src/poetry/repositories/pypi_repository.py
Add tests for fallback search functionality
  • Added tests to verify the fallback mechanism using various search queries.
  • Tests cover scenarios with no matches, exact matches, range matches, partial matches, and invalid tokens.
  • Parametrized tests to cover different search query types and expected result counts.
tests/repositories/test_pypi_repository.py
Add fixture for simulating unavailable PyPI search
  • Created a fixture with_disallowed_pypi_search_html to simulate scenarios where PyPI search is unavailable.
  • Fixture intercepts search requests and returns a custom HTML response indicating search is disallowed.
  • Allows testing the fallback mechanism without affecting actual PyPI searches.
tests/repositories/fixtures/pypi.py
Remove unused imports and variables
  • Removed unused imports and variables as part of a cleanup effort.
  • Improved code readability and maintainability.
tests/console/commands/test_search.py
Add HTML file for simulating unavailable PyPI search
  • Added an HTML file that simulates a response from PyPI when search is unavailable.
  • Used by the with_disallowed_pypi_search_html fixture to simulate the unavailable search scenario.
tests/repositories/fixtures/pypi.org/search/search-disallowed.html

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @abn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

abn added 2 commits January 16, 2025 10:17
If not results are found when searching PyPI fallback to using the
find package mechanism to at the minimum list any matches for valid
PEP 508 specifications.

Resolves: python-poetry#9884
@abn abn force-pushed the fix/search-pypi branch from f3bc1de to acbfc43 Compare January 16, 2025 09:17
@abn abn enabled auto-merge (rebase) January 16, 2025 09:17
@abn abn merged commit 18b09de into python-poetry:main Jan 16, 2025
73 checks passed
@abn abn deleted the fix/search-pypi branch January 16, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

poetry init Unable to Find Package During Interactive Dependency Addition
2 participants