Skip to content

Include close matches in error message when key not found #30001

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

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

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented May 2, 2025

This is an attempt to resurrect #28115, but make it more general so we can emit helpful error messages when keys aren't found.

@dstansby dstansby changed the title Include close matches when key not found Include close matches in error message when key not found May 2, 2025
@dstansby dstansby force-pushed the close-matches branch 3 times, most recently from 3f98b36 to 144b9d6 Compare May 3, 2025 17:37
@dstansby dstansby marked this pull request as ready for review May 3, 2025 18:53
@tacaswell
Copy link
Member

Where the discussion in #28115 got stalled is over using difflib or vendoring a copy of Levenstien distance with a question of how different the results are (and then I did not have time to answer that question). Do we want to resume that discussion or just use difflib and move on?

I think either way getting close matches is better than the current state of things and contra my previous comments I'm in favor of using difflib and changing the algorithm later if it is a problem (I'm not going to consider the guessed "did you mean..." list to be something we need to maintain API stability on!).

@dstansby dstansby marked this pull request as draft May 5, 2025 17:18
@dstansby dstansby marked this pull request as ready for review July 6, 2025 20:54
Comment on lines +95 to +97
cmap = _api.check_getitem(
self._cmaps, colormap=item, _error_cls=KeyError
)
Copy link
Member

Choose a reason for hiding this comment

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

This fits in a line.

Suggested change
cmap = _api.check_getitem(
self._cmaps, colormap=item, _error_cls=KeyError
)
cmap = _api.check_getitem(self._cmaps, colormap=item, _error_cls=KeyError)

Copy link
Member

Choose a reason for hiding this comment

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

And if you are talking about this as style, black would have put it on one line.

Copy link
Member Author

Choose a reason for hiding this comment

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

My objection is mainly to bikeshedding style one comment at a time in PR review, instead of having a tool automate it for us.

Comment on lines +178 to +180
def check_getitem(
mapping, /, _error_cls=ValueError, **kwargs
):
Copy link
Member

Choose a reason for hiding this comment

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

This fits in a line.

Suggested change
def check_getitem(
mapping, /, _error_cls=ValueError, **kwargs
):
def check_getitem(mapping, /, _error_cls=ValueError, **kwargs):

Copy link
Member

Choose a reason for hiding this comment

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

And this violates E125, which is currently missing from ruff, but we shouldn't introduce more linting issues for the future.

@dstansby dstansby mentioned this pull request Jul 15, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants