classification
Title: Empty representation of AssertionError
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Ilya Kamenshchikov, remi.lapeyre, terry.reedy
Priority: normal Keywords:

Created on 2020-08-04 16:47 by Ilya Kamenshchikov, last changed 2020-12-16 23:56 by iritkatriel. This issue is now closed.

Messages (7)
msg374831 - (view) Author: Ilya Kamenshchikov (Ilya Kamenshchikov) * Date: 2020-08-04 16:47
I have a high level wrapper where I am catching expection and present  it  in (more) user-friendly format with a message. 


try:
    raise ValueError
except Exception as e:
    print(f"Following happened: {e}")

>>> prints "Following happened: "

Can an exception print it's class when it has no message?
msg374832 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-08-04 16:59
Hi, can you not use its repr:


>>> try: raise ValueError
... except Exception as e: print(f"Following happened: {e!r}")
...
Following happened: ValueError()

?
msg374934 - (view) Author: Ilya Kamenshchikov (Ilya Kamenshchikov) * Date: 2020-08-06 13:08
That's a solution, except you must know ahead of time this issue exists.

Best Regards,
--
Ilya Kamen

On Tue, Aug 4, 2020 at 6:59 PM Rémi Lapeyre <report@bugs.python.org> wrote:

>
> Rémi Lapeyre <remi.lapeyre@henki.fr> added the comment:
>
> Hi, can you not use its repr:
>
>
> >>> try: raise ValueError
> ... except Exception as e: print(f"Following happened: {e!r}")
> ...
> Following happened: ValueError()
>
> ?
>
> ----------
> nosy: +remi.lapeyre
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue41478>
> _______________________________________
>
msg374941 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-08-06 16:01
> That's a solution, except you must know ahead of time this issue exists.

If we changed str(e) to make it the same as repr(e), there would be no way to get only the message. str(e) returns the message so if the message given was empty (or no message was given) you get an empty string. This cannot be changed without breaking a lot of code.
msg374957 - (view) Author: Ilya Kamenshchikov (Ilya Kamenshchikov) * Date: 2020-08-06 18:26
Changing behavior and it's impact on existing code is, without a doubt, a
big deal here. Maybe it's a reason not to do anything about it.

Just to understand guiding design principle, what is expected from __str__
in more general case? I thought about it as "user-friendly string
representation", therefore expected an Exception without a message to
identify itself by it's class.

Best Regards,
--
Ilya Kamen

On Thu, Aug 6, 2020 at 6:05 PM Rémi Lapeyre <report@bugs.python.org> wrote:

>
> Rémi Lapeyre <remi.lapeyre@henki.fr> added the comment:
>
> > That's a solution, except you must know ahead of time this issue exists.
>
> If we changed str(e) to make it the same as repr(e), there would be no way
> to get only the message. str(e) returns the message so if the message given
> was empty (or no message was given) you get an empty string. This cannot be
> changed without breaking a lot of code.
>
> ----------
> versions: +Python 3.10, Python 3.9
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue41478>
> _______________________________________
>
msg375017 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-08-07 19:14
Ilya, in the future, when responding by email, please delete the message you are responding to.  It is already present on the web page.
msg375018 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-08-07 19:29
Since you should print the exception class anyway, I think that using repr should be sufficient and that this issue should be closed.  In any case, the current behavior does not seem like a bug.
History
Date User Action Args
2020-12-16 23:56:47iritkatrielsetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-08-07 19:29:22terry.reedysetmessages: + msg375018
2020-08-07 19:14:15terry.reedysetnosy: + terry.reedy
messages: + msg375017
2020-08-06 18:26:24Ilya Kamenshchikovsetmessages: + msg374957
2020-08-06 16:01:47remi.lapeyresetmessages: + msg374941
versions: + Python 3.9, Python 3.10
2020-08-06 13:08:43Ilya Kamenshchikovsetmessages: + msg374934
2020-08-04 16:59:04remi.lapeyresetnosy: + remi.lapeyre
messages: + msg374832
2020-08-04 16:47:24Ilya Kamenshchikovcreate