Skip to content

[WIP] Heuristic for better error messages - #9073

Closed
Carreau wants to merge 6 commits into
ipython:masterfrom
Carreau:error-messages
Closed

Carreau wants to merge 6 commits into
ipython:masterfrom
Carreau:error-messages

Conversation

@Carreau

@Carreau Carreau commented Dec 16, 2015

Copy link
Copy Markdown
Member

Proof of concept:

In [1]: from numpy import ones

In [2]: def stuff(**kwargs):
   ...:     return ones(**kwargs)
   ...:

In [3]: stuff(size=18)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-ee3c5cbdb6f2> in <module>()
----> 1 stuff(size=18)

<ipython-input-2-542e3f8f2f33> in stuff(**kwargs)
      1 def stuff(**kwargs):
----> 2     return ones(**kwargs)
      3

TypeError: ones() got an unexpected keyword argument 'size'. Did you meant one of 'shape' ?

See the did you mean at the end ?
English need to be better of course, and here the one of is extra as it finds only 1 match.

Anyway I think this would be tremendously useful for beginners to have a framework where before displaying a traceback we could spice it up with potential "fixes".

@Carreau

Carreau commented Dec 16, 2015

Copy link
Copy Markdown
Member Author

Inspired by https://github.com/dutc/didyoumean, which is in C, and likely too intusive, and a recent tweet.

@Carreau

Carreau commented Dec 16, 2015

Copy link
Copy Markdown
Member Author

Does not work (yet) is the function itself is not in the local or global namespace. eg:

def foo():
   np.ones()

@Carreau

Carreau commented Dec 17, 2015

Copy link
Copy Markdown
Member Author

Now work also (a bit) when accessing an (wrong) attribute/methods of an object.
Should do smth similar

  • with dict KeyErrors,
  • Out of range access.
  • SyntaxError unexpected Token, (check for colon on line before)

@dsblank

dsblank commented Dec 17, 2015

Copy link
Copy Markdown
Contributor

There is a lot of research on what makes good error messages. I would hate for this to proceed too far without anyone looking at that research.

@Carreau

Carreau commented Dec 17, 2015

Copy link
Copy Markdown
Member Author

There is a lot of research on what makes good error messages. I would hate for this to proceed too far without anyone looking at that research.

I'm looking at what is doable. If you have any insight of regular user issues, and better error message that would be great.

For now I'm still focusing on technically how we could make these heuristics easy to write. We are definitively far from actually changing how things works in Python itself.

@SylvainDe

Copy link
Copy Markdown

Hi,
I stumbled upon this PR via the following Twitter thread : https://twitter.com/cfbolz/status/783313503230844929 . A couple of years ago, I started working on some small project to have such suggestions that might interest you. There is still quite a lot of work to be done (to make it pip installable among other things) but everything can be found here : https://github.com/SylvainDe/DidYouMean-Python .
Any feedback is welcome.

@Carreau

Carreau commented Oct 5, 2016

Copy link
Copy Markdown
Member Author

Sweet, that looks great ! I have to test that.

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.

5 participants