classification
Title: A better error message for float()
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Jonathan.Livni, ezio.melotti, kushal.das, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-01-30 08:33 by Jonathan.Livni, last changed 2013-11-07 17:19 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
issue17080.diff ezio.melotti, 2013-01-30 08:47 review
issue17080-2.diff ezio.melotti, 2013-02-01 02:24 review
Messages (9)
msg180967 - (view) Author: Jonathan Livni (Jonathan.Livni) Date: 2013-01-30 08:33
These lines of Python (2.7):

    y = float(x)

gives the error:

    TypeError: float() argument must be a string or a number.

In various cases such as:

    x = [0]
    x = None
    x = SomeClass()

In addition to the information given in the error message, it could help, for debugging purposes, to state the type of the object given.
msg180968 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-30 08:35
Sounds reasonable to me.
msg180969 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-30 08:47
Here's an initial patch, still needs tests.
The same should be done for complex() too.
Maybe it could be applied to older branches too.
msg180971 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-01-30 09:49
You should put single quotes around the type name, i.e.:

"float() argument must be a string or a number, not '%.200s'"
msg180972 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-30 09:59
I was thinking about that but in other places they are not used.  Adding them is OK though.
msg181053 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-01 02:24
Attached a new patch with tests.  I added the quotes around the type and fixed complex() too.
msg181073 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-02-01 10:09
LGTM.
msg202370 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-11-07 17:18
New changeset a73c47c1d374 by Ezio Melotti in branch 'default':
#17080: improve error message of float/complex when the wrong type is passed.
http://hg.python.org/cpython/rev/a73c47c1d374
msg202371 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-11-07 17:19
Fixed, thanks for the review.
History
Date User Action Args
2013-12-24 14:57:18r.david.murraylinkissue20060 superseder
2013-11-07 17:19:23ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg202371

stage: patch review -> resolved
2013-11-07 17:18:52python-devsetnosy: + python-dev
messages: + msg202370
2013-02-01 10:09:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg181073
2013-02-01 02:24:14ezio.melottisetfiles: + issue17080-2.diff

messages: + msg181053
2013-01-30 09:59:01ezio.melottisetmessages: + msg180972
2013-01-30 09:49:35pitrousetnosy: + pitrou

messages: + msg180971
stage: needs patch -> patch review
2013-01-30 08:47:54ezio.melottisetfiles: + issue17080.diff
assignee: ezio.melotti
messages: + msg180969

keywords: + patch
2013-01-30 08:41:26kushal.dassetnosy: + kushal.das
2013-01-30 08:35:56ezio.melottisetversions: + Python 3.4, - Python 2.7
nosy: + ezio.melotti

messages: + msg180968

stage: needs patch
2013-01-30 08:33:13Jonathan.Livnicreate