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 when missing CTE #257

Closed
edublancas opened this issue Mar 16, 2023 · 4 comments · Fixed by #290
Closed

better error when missing CTE #257

edublancas opened this issue Mar 16, 2023 · 4 comments · Fixed by #290
Assignees

Comments

@edublancas
Copy link

edublancas commented Mar 16, 2023

when using the --with argument, this runs; however, if the passed key does not exist, the error traceback is pretty long (see attached notebook).

Instead, we should raise an UsageError:

from IPython.core.error import UsageError

raise UsageError(f"{key!r} is not a valid snippet identifier")

To provide a more helpful message, we can also use difflib to check if there are any closes matches:

"subsett" is not a valid snippet identifier. Did you mean "subset"?

If there are no close matches, we can display all available keys:

"subsett" is not a valid snippet identifier. Valid identifiers are "one", "another", and "something"

@edublancas
Copy link
Author

sample notebook: sample.ipynb.zip

@anupam-tiwari
Copy link

I can look into it!

@anupam-tiwari
Copy link

regarding this: "To provide a more helpful message, we can also use difflib to check if there are any closes matches:"

The function difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6) takes parameter possibilities as a list of sequences against which to match word, since key can be user defined example %%sql --save anupam I was wondering how should I pass on the possibilities list as there can be wide range of strings to compare from! am I doing something wrong here?

@anupam-tiwari
Copy link

I think I will return the best match if it exist or else return the entire keys:

if key not in self._data: 
            matches = difflib.get_close_matches(key, self._data)
            if matches: 
                raise UsageError(f"{key!r} is not a valid snippet identifier. Did you mean {matches[0]!r}?")
            else: 
                valid = ", ".join(f'"{key}"' for key in self._data.keys())
                raise UsageError(f"{key!r} is not a valid snippet identifier. Valid identifiers are {valid}.")

is this is a sound approach?

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 a pull request may close this issue.

2 participants