How To Solve The Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” in Python

The Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” in Python happens when you assign the value to the second parameter of the isinstance() function incorrectly. To solve this Error, make sure you assign the value to the second parameter correctly: a type, tuple of types, or a union in Python. Let’s learn more about it with the explanation and example below.

How Does This Error Occur?

Let’s take a look at the syntax of the isinstance() function before learning how this Error occurs.

Syntax

isinstance(__obj, __class_or_tuple)

Parameters

  • __obj: The object.
  • __class_or_tuple: a type, tuple of types, or a union

Return value

True if the type of the object is the specified type or one of types of the tuple.

The Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” in Python happens when you assign the value to the second parameter named ‘__class_or_tuple’ of the isinstance() function incorrectly. The value of its second parameter must be one of type: a type, tuple of types, or a union. Another reason you can easily encounter is setting the name of the variable shadowing a type built-in in Python.

Look at the example below to learn more about this Error.

# List of types
types = [str, list, int, bool]

# Create an object
obj = 'LearnShareIT'

# Check if the type of this object is in the 'types' list or not
typeOfObj = isinstance(obj, types) # The Error will occur here

if typeOfObj == True:
    print('The type of this object is in types.')
else:
    print('The type of this object is not in types')

Output

TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

Solve The Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” in Python

To solve the Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” in Python, you can convert the list of types to the tuple. And make sure you do not shadow the name of a variable as a type built-in in Python.

Convert the list of types to tuple

In this solution, we will convert the list of types to tuples of types with the tuple() function. After doing that, the Error will be fixed.

Look at the example below.

# List of types
types = [str, list, int, bool]

# Create an object
obj = 'LearnShareIT'

# Convert the 'types' list to tuple
types = tuple(types)

# Check if the type of this object is in the 'types' list or not
typeOfObj = isinstance(obj, types)

if typeOfObj == True:
    print('The type of this object is in types.')
else:
    print('The type of this object is not in types')

Output

The type of this object is in types.

Fix the Error if the name of a variable shadows a type in Python

If the name of the variable shadows a type in Python, you can not perform the isinstance() function as usual. So, you should use the type() function with a type of the ‘__class_or_tuple’ parameter to prevent an Error.

Look at the example below.

# Create an object whose name shadows the 'list' type
list = ['LearnShareIT', 'GeeksForGeeks', 'W3schools']

# Check if the type of this object is a list or not
typeOfObj = isinstance(list, type(list))
print(typeOfObj)

Output

True

Summary

We have shown you why the Error “TypeError: isinstance() arg 2 must be a type, tuple of types, or a union” happens and how to fix it. To prevent getting this Error, make sure you assign the value to the second parameter of the isinstance() function one of the values: a type, tuple of types, or a union. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *