This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Frendly error message when inheriting from function
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: fossilet, georg.brandl, ggenellina, r.david.murray, rhettinger, techtonik
Priority: low Keywords: easy

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 "<stdin>", line 1, in <module>
  File "m:\p\", line 4, in <module>
    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):

class PostgreSQLConnection(DatabaseConnection):
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.
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51078
2012-10-31 12:18:45fossiletsetnosy: + fossilet
2010-07-31 19:50:11georg.brandlsetstatus: open -> closed

messages: + msg112190
2009-09-16 09:53:56ggenellinasetnosy: + ggenellina
2009-09-07 14:31:00r.david.murraysetmessages: + msg92366
2009-09-07 10:02:07techtoniksetmessages: + msg92358
2009-09-07 10:00:27techtoniksetmessages: + msg92357
2009-09-07 09:59:17techtoniksetmessages: + msg92356
2009-09-04 15:22:04r.david.murraysetstatus: pending -> open
priority: low

nosy: + rhettinger, r.david.murray
messages: + msg92256

keywords: + easy
2009-09-04 14:08:39techtoniksetstatus: closed -> pending
2009-09-04 14:07:43techtoniksetmessages: + msg92255
2009-09-04 08:31:10georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg92244

resolution: wont fix
2009-09-03 07:20:18techtonikcreate