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

PythonClassInheritanceMetaclass

Python Problem Overview


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!

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid UnderhillView Question on Stackoverflow
Solution 1 - PythonWill McCutchenView Answer on Stackoverflow
Solution 2 - PythonVonView Answer on Stackoverflow
Solution 3 - PythonSteve LeibmanView Answer on Stackoverflow
Solution 4 - PythonPaulView Answer on Stackoverflow