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

add .get() to the PackageMetadata protocol #444

Merged
merged 2 commits into from Apr 7, 2023
Merged

add .get() to the PackageMetadata protocol #444

merged 2 commits into from Apr 7, 2023

Conversation

dimbleby
Copy link
Contributor

@dimbleby dimbleby commented Apr 7, 2023

__getitem__ warns that it will raise KeyError rather than returning None (I see #371 etc)

that's all very well but there's currently no comfortable way for users who anticipate missing values to write their code in a way that doesn't trigger that warning. We seem to be steered towards

import warnings
try:
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", "Implicit None .* KeyError", DeprecationWarning)
        value = metadata[key]
except KeyError:
    value = None

which sure seems like a lot of ceremony.

The natural way to do it would be just value = metadata.get(key). That method exists, but is not exposed by the protocol so if we try to use it then mypy shouts at us

Therefore add get() to that protocol (and testcases showing that it works).

@dimbleby
Copy link
Contributor Author

dimbleby commented Apr 7, 2023

Just spotted previous discussion in #384. Conversation there doesn't sound too opposed to this.

IMO it's a very natural thing to have on the API, perhaps the new annoyance with the hard-to-avoid warning, alongside an actual merge request, is enough to settle the matter...

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

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

Looks good. I'll just add a changelog entry and we can roll this out.

@jaraco jaraco linked an issue Apr 7, 2023 that may be closed by this pull request
@jaraco jaraco merged commit 4df89f1 into python:main Apr 7, 2023
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.

Should PackageMetadata.get be supported?
2 participants