Issue 6829: Frendly error message when inheriting from function (original) (raw)

Created on 2009-09-03 07:20 by techtonik, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (9)
msg92191 - (view) Author: anatoly techtonik (techtonik) Date: 2009-09-03 07:20
It is an error to try to inherit from function and the error message in this case is: {{{ Traceback (most recent call last): File "", line 1, in File "m:\p\pb.py", line 4, in class PostgreSQLConnection(DatabaseConnection): TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str }}} Something like 'Impossible to inherit from function' will clear confusion state from users unfamiliar with metaclasses. {{{ def DatabaseConnection(object): pass class PostgreSQLConnection(DatabaseConnection): pass }}}
msg92244 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-09-04 08:31
You'll get a similar message when trying to inherit from e.g. a string or an int. I see no compelling reason to special-case functions here.
msg92255 - (view) Author: anatoly techtonik (techtonik) Date: 2009-09-04 14:07
Ok, but why not to change the message to less cryptic "Impossible to inherit class from function(), string or int"?
msg92256 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-09-04 15:22
That would still be special-casing, you'd just be special casing three things instead of one, nor does that exhaust the list of things for which you might get this kind of error. However, the code that generates the error message contains the following comment: /* A type error here likely means that the user passed in a base that was not a class (such the random module instead of the random.random type). Help them out with by augmenting the error message with more information.*/ So, it seems to me that the author (I think it was Raymond, so I'm setting him nosy) was trying to give the requested clue, but it sounds like the error message isn't quite achieving that goal. I've tried several rephrasings, but I haven't come up with one I'm completely happy with. It seems that the most common problem is trying to use an instance as a base, and if that is correct the best I've come up with so far is: "Error when calling the metaclass bases (specified base may be an instance instead of a class)"
msg92356 - (view) Author: anatoly techtonik (techtonik) Date: 2009-09-07 09:59
How about: Error running metaclass bases (attempt to inherit from object that is not a class)
msg92357 - (view) Author: anatoly techtonik (techtonik) Date: 2009-09-07 10:00
Or just: Error inheriting from object that is not a class
msg92358 - (view) Author: anatoly techtonik (techtonik) Date: 2009-09-07 10:02
Or with more info if possible: Error when inheriting class "%s" - parent is not a class.
msg92366 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-09-07 14:31
It is possible that a TypeError could arise during the execution of the metaclass bases that is not the result of inheriting from something other than a class/type. It might, however, be possible to enhnace the message with the name of the metaclass or first base class...but someone who knows more about the interpreter internals than I do would have to be willing to take that on, as it isn't clear to me from reading the code what the right thing would be to put in the message.
msg112190 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-07-31 19:50
In Python 3, due to the new class creation the somewhat confusing message augmentation has been removed altogether, and for me there seems to be no way to add a better message about base class types somewhere without making too many assumptions.
History
Date User Action Args
2022-04-11 14:56:52 admin set github: 51078
2012-10-31 12🔞45 fossilet set nosy: + fossilet
2010-07-31 19:50:11 georg.brandl set status: open -> closedmessages: +
2009-09-16 09:53:56 ggenellina set nosy: + ggenellina
2009-09-07 14:31:00 r.david.murray set messages: +
2009-09-07 10:02:07 techtonik set messages: +
2009-09-07 10:00:27 techtonik set messages: +
2009-09-07 09:59:17 techtonik set messages: +
2009-09-04 15:22:04 r.david.murray set status: pending -> openpriority: lownosy: + rhettinger, r.david.murraymessages: + keywords: + easy
2009-09-04 14:08:39 techtonik set status: closed -> pending
2009-09-04 14:07:43 techtonik set messages: +
2009-09-04 08:31:10 georg.brandl set status: open -> closednosy: + georg.brandlmessages: + resolution: wont fix
2009-09-03 07:20:18 techtonik create