classification
Title: TypeError: __bool__ should return bool or int, returned int
Type: behavior Stage: patch review
Components: Documentation, Interpreter Core Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, amaury.forgeotdarc, eric.smith, georg.brandl, pitrou
Priority: normal Keywords: patch

Created on 2009-07-06 14:45 by SilentGhost, last changed 2009-07-07 00:45 by amaury.forgeotdarc. This issue is now closed.

Files
File name Uploaded Description Edit
bool.patch amaury.forgeotdarc, 2009-07-06 22:50
Messages (5)
msg90181 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2009-07-06 14:45
According to docs
(http://docs.python.org/3.1/reference/datamodel.html#object.__bool__)
__bool__ can return 1 or 0 instead of True or False.
However, when I ran the following code:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
>>> class Spam():
	def __bool__(self):
		return 1

	
>>> if Spam():
	print('ham')

I got the following error:
	
Traceback (most recent call last):
  File "<pyshell#72>", line 1, in <module>
    if Spam():
TypeError: __bool__ should return bool or int, returned int

So, do I misunderstand the docs or is it an error in them?
msg90189 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-07-06 19:40
It's not only the docs, the error message is self-contradictory as well.
msg90195 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-07-06 22:50
Probably a misguided merge. Here is a patch that updates the error 
message, and the documentation.
msg90200 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-07-06 23:42
The patch looks good to me. In particular, removing the test for
using_len looks correct.

The assignment of "result = -1" after PyErr_Format isn't needed, but
doesn't hurt (and it was already there, anyway).
msg90206 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-07-07 00:45
fixed in r73868 (py3k) and r73869 (3.1)
Thanks for the report!
History
Date User Action Args
2009-07-09 21:40:27r.david.murraylinkissue6453 superseder
2009-07-07 00:45:49amaury.forgeotdarcsetstatus: open -> closed
resolution: fixed
messages: + msg90206
2009-07-06 23:42:25eric.smithsetkeywords: - needs review
nosy: + eric.smith
messages: + msg90200

2009-07-06 22:50:26amaury.forgeotdarcsetstage: needs patch -> patch review
2009-07-06 22:50:08amaury.forgeotdarcsetfiles: + bool.patch

nosy: + amaury.forgeotdarc
messages: + msg90195

keywords: + needs review, patch
2009-07-06 19:40:01pitrousetpriority: normal

assignee: georg.brandl ->
versions: + Python 3.2
nosy: + pitrou

messages: + msg90189
stage: needs patch
2009-07-06 14:45:33SilentGhostcreate