Error when calling the metaclass bases: function() argument 1 must be code, not str
Asked Answered
U

4

61

I tried to subclass threading.Condition earlier today but it didn't work out. Here is the output of the Python interpreter when I try to subclass the threading.Condition class:

>>> import threading
>>> class ThisWontWork(threading.Condition):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str

Can someone explain this error? Thanks!

Untried answered 9/2, 2010 at 18:24 Comment(0)
H
96

You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions.

>>> type(threading.Condition)
<type 'function'>

This not-very-helpful error message has been raised on the Python bugtracker, but it has been marked "won't fix."

Hyperborean answered 9/2, 2010 at 18:31 Comment(5)
Strange, I didn't think to check its type. The docs seemt o be a bit misleading then because they say (docs.python.org/library/threading.html) "class threading.Condition([lock])" which seems a bit misleading. Anyway, thanks for clearing this up :).Untried
I see it as well when mistakenly using a module as a baseclass: class Command(main.cmdroot.list.hosts) rather than the correct Command(main.cmdroot.list.hosts.Command)Mafia
Another common cause is when deriving from a base class which has a decorator. The decorator may change the type of the base class to type <'function'> as well.Waitress
Sometimes I accidentally write def MyClass instead of class MyClass, which then leads to the same error (among others).Orient
"you cannot subclass functions" - thanks for a definitive answer on this. I wish the error message said THAT.Moina
W
31

Different problem than OP had, but you can also get this error if you try to subclass from a module instead of a class (e.g. you try to inherit My.Module instead of My.Module.Class). Kudos to this post for helping me figure this out.

TypeError: Error when calling the metaclass bases

For this one, the answer is that you probably named a python class the same thing as the module (i.e., the file) that it's in. You then imported the module and attempted to use it like a class. You did this because you, like me, were probably a Java programmer not that long ago :-). The way to fix it is to import the module.class instead of just the module. Or, for sanity's sake, change the name of the class or the module so that it's more obvious what's being imported.

Winkelman answered 17/8, 2010 at 22:32 Comment(1)
also when you try to subclass from def MyClass(object) instead of from class MyClass(object)Obellia
M
29

With respect to subclassing a module, this is a really easy mistake to make if you have, for example, class Foo defined in a file Foo.py.

When you create a subclass of Foo in a different file, you might accidentally do the following (this is an attempt to subclass a module and will result in an error):

import Foo
class SubclassOfFoo(Foo):

when you really need to do either:

from Foo import Foo
class SubclassOfFoo(Foo):

or:

import Foo
class SubclassofFoo(Foo.Foo):
Michaeline answered 12/6, 2013 at 14:2 Comment(3)
this is a great reason not to name your modules like classes :)Union
Yup this did it for me, i had the model and the class name the same.Baseler
Can also happen when you type def (when you mean class) when subclassing, and you're too tired to see it.Hunnicutt
H
2

Gotten into the same problem. Finally solved by taking a keen look at the code and this is where the TypeError that alarms about a string instead of code comes about..

Class Class_name(models.model): //(gives a TypeError of 'str' type) 

"And"

Class Class_name(models.Model): // is the correct one. 

Notice that specific error comes about because of a single lowercase letter to the code "Model" which in turn makes it a string

Hophead answered 21/10, 2016 at 8:6 Comment(1)
"which in turn makes it a string", lowercase words are not automatically strings in pythonOr

© 2022 - 2024 — McMap. All rights reserved.