-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
PackageMetadata.__getitem__ returns None #371
Comments
Hi @saaketp. Thanks for the report. I do believe this issue is a bug. I'm yet unsure if the proper fix is to update the protocol to match the implementation or to update the implementation to match the protocol. Historically, the metadata handling relied primarily on the interface of email.message.EmailMessage. That documentation indicates that |
Honestly, I'm conflicted. My instinct is that the current protocol definition is right and that |
Separately, I'm disappointed that Perhaps the issue is that typeshed underdefines the return type as Any. Perhaps someone would like to fix that. |
Looking at the issue that resulted in the change of the return type of My vote would be to change the implementation to match the Protocol, I don't like |
I've just filed |
Spurred by considerations in that bpo issue, I'm inclined to say the implementation should be limited to raise a KeyError when an item is not defined in the metadata. I would like to confirm that this library itself does not rely heavily on that expectation. This backward-incompatible change will likely cause some disruption, so an incremental approach is probably needed to deprecate the existing behavior (warn when |
In 614a522, I confirmed that at least internally, there's no issue with raising KeyError on missing keys. |
Since this change is likely to create disruption, I'd like to reserve it for a new backward-incompatible release to only go into new versions of CPython (3.12 or later), but since it's a refinement of behavior toward the specified behavior, I don't think it needs a deprecation period in CPython's implementation. |
Before the release could even hit PyPI, the backward incompatibility struck. The release process failed because This finding leads me to believe there may need to be a deprecation period for this change. |
In rare cases, `importlib.metadata` values may contain `None`, see python/cpython#91216 and python/importlib_metadata#371 The fix skips all distributions with incomplete metadata.
In rare cases, `importlib.metadata` values may contain `None`, see python/cpython#91216 and python/importlib_metadata#371 The fix skips all distributions with incomplete metadata.
In rare cases, `importlib.metadata` values may contain `None`, see python/cpython#91216 and python/importlib_metadata#371 Co-authored-by: Ivana Kellyerova <[email protected]> Co-authored-by: Anton Pirker <[email protected]>
What's the proper way to write code that can handle a missing item? cc @hroncok |
I would also like some advice on how code should be structured for cases where one needs to query for metadata that may or may not be there, without triggering the warning. |
How about Here's a helper you could use: def future_getitem(metadata: PackageMetadata, key: str) -> str:
"""retrieve a key from metadata similar to a future implementation of PackageMetadata.__getitem__"""
val = metadata.get(key, None)
if val is None:
raise KeyError(key)
return val Should importlib_metadata provide this helper? |
Finally, importlib_metadata 8 provides the desired behavior, raising KeyError on a missing key. If you want the KeyError behavior, depend on and use importlib_metadata 8 or later (to be incorporated in Python 3.14). |
The first problem is almost certainly a bug, the annotation for
__getitem__
says it returns astr
but it returnsNone
if the metadata doesn't exist. https://github.com/python/importlib_metadata/blob/v4.11.2/importlib_metadata/_meta.py#L15For example:
(second problem moved to #374).
The text was updated successfully, but these errors were encountered: