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

Better error messaging #806

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

RobbertDM
Copy link

@RobbertDM RobbertDM commented Jan 6, 2025

Resolves: python-poetry#

  • Added tests for changed code.
  • Updated documentation for changed code.

I just had a Gitlab pipeline fail with

$ poetry run some_script_that_we_defined_in_pyproject_toml
'format'

And that's it for the error message.

It failed because of this formats=package["format"] KeyError, but in the error message it didn't even mention that it was a KeyError. We didn't know it was poetry related.
I hope to save a lot of people some time by including this try/catch.

Summary by Sourcery

Bug Fixes:

  • Clarify the error message when a KeyError occurs during package inclusion to help users diagnose the issue faster.

Copy link

sourcery-ai bot commented Jan 6, 2025

Reviewer's Guide by Sourcery

This pull request improves error messaging when a KeyError occurs during package inclusion. It adds a try/except block around the inclusion logic and appends a helpful note to the KeyError exception, suggesting that the issue might stem from using a Poetry v1 pyproject.toml with Poetry v2.

Sequence diagram for improved error handling in package inclusion

sequenceDiagram
    participant User
    participant Poetry
    participant PackageInclude

    User->>Poetry: Run poetry command
    activate Poetry
    Note over Poetry: Try to process package includes
    alt Success
        Poetry->>PackageInclude: Create package include
        PackageInclude-->>Poetry: Package include created
    else KeyError
        Poetry->>Poetry: Add note about v1/v2 compatibility
        Poetry-->>User: Error with helpful message
    end
    deactivate Poetry
Loading

Class diagram showing updated error handling in Module class

classDiagram
    class Module {
        -_package_includes: List
        -_explicit_includes: List
        -_path: Path
        +__init__(path, packages, includes)
    }
    note for Module "Added try/catch block for KeyError
with improved error message"
Loading

File-Level Changes

Change Details Files
Improved KeyError exception handling
  • Added a try/except block to catch KeyErrors during package inclusion.
  • Included a note about potential v1/v2 incompatibility in the KeyError message to aid in debugging.
src/poetry/core/masonry/utils/module.py

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 @RobbertDM - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The try-catch block is too broad and could catch unrelated KeyErrors. Consider wrapping only the specific package["format"] access if that's the intended target.
  • Please add tests to verify the error message behavior, particularly for the v1/v2 version mismatch scenario.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 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.

Comment on lines 84 to 86
except KeyError as e:
e.add_note("It might be that you are trying to run poetry v2 with a v1 pyproject.toml")
raise e
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider making the error message more specific by including which key was missing

The current message assumes version mismatch, but a KeyError could occur for other reasons. Including the missing key name (available via str(e)) would make debugging easier.

Suggested change
except KeyError as e:
e.add_note("It might be that you are trying to run poetry v2 with a v1 pyproject.toml")
raise e
except KeyError as e:
e.add_note(f"Missing required key '{str(e)}'. This might occur if you are trying to run poetry v2 with a v1 pyproject.toml")
raise e

Copy link
Author

Choose a reason for hiding this comment

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

But it calls add_note, so I believe the whole KeyError message is still there when raising e further?

Copy link

Choose a reason for hiding this comment

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

You are absolutely right. My apologies! I mistakenly focused on improving the clarity regarding which key was missing and overlooked that the original exception message, containing the key, is preserved. The suggested change is therefore redundant. Thanks for pointing that out! 👍

@abn
Copy link
Member

abn commented Jan 6, 2025

@RobbertDM this might be superseded by #805

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.

2 participants