Created on 2012-07-04 05:28 by o11c, last changed 2012-07-07 01:45 by terry.reedy.
|msg164637 - (view)||Author: Ben Longbons (o11c)||Date: 2012-07-04 05:28|
I frequently construct lists of tuples, such as: [ (1, 2, 3) # oops, missing comma! (4, 5, 6) ] It would be nice if the error message gave a hint on what was *actually* wrong. Although I always use homogeneous containers, the type that's not callable could be something other than 'tuple'. You could possibly cut down on false positives (at the risk of false negatives) by checking that the not-callable object is newly constructed. A better way to cut down on false positives would be to check that a list, tuple, or set is being constructed from a literal, but this might be more complex.
|msg164711 - (view)||Author: Steven D'Aprano (stevenjd)||Date: 2012-07-06 01:58|
I think this suggested enhancement is unreasonable and of limited usefulness, and even if it were useful, too specialised to bother with. The obvious message is badly misleading. When I read this: TypeError: 'tuple' object is not callable, a comma may be missing I look inside the tuple for a missing comma. But that's not the problem, and the error message sends me on a wild goose chase wondering how on earth a missing comma causes Python to try calling my tuple. I've been programming in Python for 15+ years and it mislead me -- what do you think it will do to beginners? The problem is that the tuple is inside a list, and the LIST is missing a comma. What about this example? result = function( "the error has nothing to do with tuples" # oops missed a comma (2, 3, 4), None ) Should the error message say something like: TypeError: 'str' object is not callable, perhaps it is embedded in a list, tuple, function call or some other place you need a comma, and you forgot one or more commas? I don't think so. Besides, most of the time when you get this TypeError, it will be because you genuinely tried to call what you thought was a function but wasn't, and the hint is pointless. You tried to call a tuple as if it were a function. The reason for that is that you left out a comma in a list, but there are many other reasons that could happen, and "I frequently forget to add commas to lists" is a far too-specialised failure mode to single it out in the error message. It is unreasonable to expect Python to debug your code for you. It tells you what you did wrong -- you called a tuple as a function -- and it is up to you to determine why.
|msg164716 - (view)||Author: Ben Longbons (o11c)||Date: 2012-07-06 04:46|
This kind of "debug your code" is the kind of thing I've gotten used to from the Clang C/C++ compiler. Granted, compiled languages have an advantage here, but enough residual information remains for the interpreter at runtime. And I am in no way suggesting that *every* attempt to call a non-function have the extra information. For the cases where the error message is given, something like: TypeError: 'tuple' object is not callable (missing preceding comma?) The case of a homogenous container is the most important case. I've offered two different ways to figure out whether it's a typo or an attempt to call an object that you honestly think is callable: 1. Is the called object a newly-constructed (refcount=1) tuple literal? (Also works for list, set, and dictionary literals; probably wouldn't work for string literals due to interning) 2. Does the false call occur within a container literal or function call? I'm not intimately familiar with python bytecode or interpreter, but I'm sure anyone who is could extract this information.
|msg164741 - (view)||Author: Ezio Melotti (ezio.melotti) *||Date: 2012-07-06 17:36|
A FAQ entry could be added to explain this error, like we already do for the UnboundLocalError: http://docs.python.org/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value
|msg164774 - (view)||Author: Terry J. Reedy (terry.reedy) *||Date: 2012-07-07 01:44|
Stephen is right; this sort of guess-the-bug heuristic is out of scope for the CPython interpreter. I believe you grossly under estimate the difficulty of such a this. Consider that idea rejected. What *is* disconcerting is the exact form of the error message for this particular code pattern: Traceback (most recent call last): File "<pyshell#0>", line 3, in <module> (4, 5, 6) TypeError: 'tuple' object is not callable Uh... There was no attempt to call the tuple (4,5,6), which was misinterpreted as a () call operator with 3 args. What might be a reasonable request is to print a size-limited representation of the object that is not callable but was attempted to be called. Truncated representations would be generally useful for traceback messages. I believe we already have them for unittest error messages. A useful document for someone (or some people) to write would be 'How to interpret exception messages'. That could start with a catalog of messages and possible causes. 'type' would be used to stand for any specific type. The section on TypeErrors would have an heading 'type' object is not callable followed by an explanation There are two general reasons for this message. The first is that you intentionally write "expression(args)" with the intent that expression evaluate to a callable, but it does not. The second is that you accidentally omit a comma in a sequence and write "expression (tuple_members)". Note that the error is triggered by the "(args)" call operator and its line is the one printed. So if "expression" is on the preceeding line, it will not appear in the traceback. This can happen with either type of error. In the meanwhile, the above is a start for a faq entry.
|2012-07-07 01:45:58||terry.reedy||set||title: In "TypeError: 'tuple' object is not callable", explain that a comma may be missing -> Better explain "TypeError: 'tuple' object is not callable"|
stage: needs patch
versions: + Python 3.4
messages: + msg164774
messages: + msg164741
|2012-07-06 04:46:15||o11c||set||messages: + msg164716|
messages: + msg164711
title: In "TypeError: 'tuple' object is not callable", suggest a comma. -> In "TypeError: 'tuple' object is not callable", explain that a comma may be missing