Skip to content

Conversation

@radoering
Copy link
Member

@radoering radoering commented Dec 28, 2025

It is a matter of weighing things up but I believe checking the keys is worth more than checking immutability.

See python-poetry/poetry#10673 for issues that the improved typing uncovered.

Summary by Sourcery

Improve typing of package file metadata on packages.

Enhancements:

  • Introduce a PackageFile TypedDict to describe package file metadata.
  • Tighten the Package.files attribute type to use the new PackageFile TypedDict instead of a generic mapping.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 28, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Tightens the type hint for Package.files by introducing a dedicated TypedDict (PackageFile) describing the expected file and hash keys, and updating the Package.files attribute to use this more precise type.

Class diagram for tightened typing of Package.files

classDiagram
    class PackageFile {
        +str file
        +str hash
    }

    class Package {
        +Sequence~PackageFile~ files
    }

    Package "1" o-- "*" PackageFile
Loading

File-Level Changes

Change Details Files
Introduce a dedicated TypedDict describing the structure of package file entries and use it to improve the type hint of Package.files.
  • Add a PackageFile TypedDict with required string fields file and hash
  • Change the type annotation of Package.files from a generic mapping sequence to Sequence[PackageFile] to enforce key presence and structure
src/poetry/core/packages/package.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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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 - I've left some high level feedback:

  • Consider whether PackageFile should declare any optional keys (e.g. using TypedDict(total=False) or NotRequired[...]) if real-world self.files entries sometimes omit file or hash, otherwise this stricter type may not accurately reflect existing usage.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider whether `PackageFile` should declare any optional keys (e.g. using `TypedDict(total=False)` or `NotRequired[...]`) if real-world `self.files` entries sometimes omit `file` or `hash`, otherwise this stricter type may not accurately reflect existing usage.

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.

@dimbleby
Copy link
Contributor

checking the keys is worth more than checking immutability

typing.ReadOnly (backported to typing_extensions) should allow you to do both

@radoering
Copy link
Member Author

Yeah, but since it is poetry-core we had to vendor it. I tend to think that vendoring is not worth it just for this.

@radoering radoering force-pushed the package-files-typing branch from e55ecb0 to 5482e60 Compare December 31, 2025 06:26
@radoering radoering merged commit 2548a4c into python-poetry:main Dec 31, 2025
19 checks passed
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