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!

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.”

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.

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):

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