typeError: isinstance() arg 2 must be a type or tuple of types >>>

Question:

>>> names=['jill','jack']
>>> isinstance(names,list)
Traceback (most recent call last):
  File "<pyshell#291>", line 1, in <module>
    isinstance(names,list)
TypeError: isinstance() arg 2 must be a type or tuple of types
>>> 

Am I missing something here?

Asked By: Monkey2code

||

Answers:

You’ve stomped on list by assigning to a local variable of the same name. Don’t do that.

Apply this one :

if isinstance(names, type(list)):
Answered By: Siva Kumar

But this works in Python (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32:

>>> names=['jill', 'jack']
>>> isinstance(names, list)
True
Answered By: Keelung

This can also happen if you’ve accidentally messed up the foreign key field syntax in your model. When writing a foreign key field, you can have:

ModelName

or:

'app_name.ModelName'

but you cannot have:

'ModelName'

Learnt that one the hard way.

Answered By: thms

If you are using Jupyter notebook, then do this:

del str

Answered By: BetterCallMe

This situation happens when you refer to model which is not actual existed and you are referencing it in a fk relation with ‘app_name.model_name’ syntax.

The excerpt is, in lieu of app_name.model_name in fk, first remove all those field with the syntax mentioned and apply migrations.

==> like the one below:

company = models.ForeignKey('core_company.Company', 
null=True, 
blank=True, 
verbose_name="Company", 
on_delete=models.CASCADE,related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss")

***** SOLUTION *****

Remove this field from the model and first make sure that core_company.Company model actually exists. if it doesn’t first create migration for Company model and apply that migration. After that you can refer it like above.

Answered By: Raja Atif Rasheed
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.