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

Missing error message for WordMorphism #16969

Open
nadialafreniere opened this issue Sep 11, 2014 · 2 comments
Open

Missing error message for WordMorphism #16969

nadialafreniere opened this issue Sep 11, 2014 · 2 comments

Comments

@nadialafreniere
Copy link
Contributor

The error message should be better:

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

CC: @seblabbe @sagetrac-tmonteil

Component: PLEASE CHANGE

Keywords: error, Words

Author: Nadia Lafrenière

Issue created by migration from https://trac.sagemath.org/ticket/16969

@jdemeyer
Copy link

comment:1

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.

@seblabbe
Copy link
Contributor

comment:2

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']. 

@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants