Opened 6 years ago

Last modified 4 years ago

#16969 new PLEASE CHANGE

Missing error message for WordMorphism

Reported by: nadialafreniere Owned by:
Priority: major Milestone: sage-6.4
Component: PLEASE CHANGE Keywords: error, Words
Cc: slabbe, tmonteil Merged in:
Authors: Nadia Lafrenière Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

The error message should be better:

sage: m=WordMorphism('0->1,1->0')
sage: m([0])
Traceback (most recent call last):
...
KeyError: 0

Change History (3)

comment:1 Changed 6 years ago by jdemeyer

The error message is consistent with what Python gives:

sage: D = {}
sage: D[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-b088d10b34b5> in <module>()
----> 1 D[Integer(0)]

KeyError: 0

so I would close it as "invalid" (not a bug).

Interestingly, KeyError uses the repr() of its argument to show the exception, while other exceptions use the str(), note the extra ' around the last 0:

sage: raise ValueError("0")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-eae8cf826a55> in <module>()
----> 1 raise ValueError("0")

ValueError: 0
sage: raise KeyError("0")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-6-97c3d5eafb80> in <module>()
----> 1 raise KeyError("0")

KeyError: '0'

So I really think that KeyError is supposed to take as argument the exact key which failed.

comment:2 Changed 6 years ago by slabbe

I suggested Nadia to create the ticket. For some user the difference between the str '0' and the int 0 is not clear especially since both of the following have the same repr :

sage: m = WordMorphism('0->01,1->10'); m
WordMorphism: 0->01, 1->10
sage: m = WordMorphism({0:[0,1],1:[1,0]}); m
WordMorphism: 0->01, 1->10

The following is fine and I think any user is okay with it:

sage: m
WordMorphism: 0->01, 1->10
sage: m([4])
Traceback (most recent call last):
...
KeyError: 4

But suppose you don't remember how m was created. You print it and then you use it:

sage: m
WordMorphism: 0->01, 1->10
sage: m([0])
Traceback (most recent call last):
...
KeyError: 0

The first time you get the error, it can be confusing and not easy to find what is wrong.

I was thinking about a better error message like the following:

sage: m
WordMorphism: 0->01, 1->10
sage: m([0])
Traceback (most recent call last):
...
KeyError: 0. Valid keys are ['0', '1']. 

comment:3 Changed 4 years ago by tmonteil

  • Cc tmonteil added
Note: See TracTickets for help on using tickets.