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

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

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

The "TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union" occurs for 2 main reasons:

  1. Passing a list instead of a tuple when checking if an object is a subclass of one of multiple classes.
  2. Shadowing a built-in class by declaring a variable with the same name, e.g. list.

typeerror isinstance arg 2 must be type tuple or union

# Passing a list instead of a tuple to isinstance()

Here is an example of how the error occurs.

main.py
a_list = [str, int, bool] # โ›”๏ธ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union result = isinstance('bobbyhadz.com', a_list)

We passed a list as the second argument to the isinstance() function which caused the error.

The isinstance function can either be passed a class or a tuple of classes.

# Pass a tuple to the isinstance() function

You can use the tuple() class to convert the list to a tuple if you are trying to check if an object is an instance of one of multiple classes.

main.py
a_list = [str, int, bool] result = isinstance('bobbyhadz.com', tuple(a_list)) print(result) # ๐Ÿ‘‰๏ธ True

pass tuple to isinstance function

The isinstance() function returns True if the passed-in object is an instance or a subclass of the passed-in class or at least one of the classes in a tuple.

# Shadowing built-in classes

The error is also caused if you shadow a built-in class by declaring a variable with the same name.

main.py
# ๐Ÿ‘‡๏ธ This shadows the built-in list class list = ['bobby', 'hadz', 'com'] numbers = [1, 2, 3] # โ›”๏ธ TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union result = isinstance(numbers, list)

We declared a variable named list which shadows the built-in list class.

We ended up passing our own list as an argument to the isinstance() function rather than the built-in list class.

# Rename the variable to solve the error

To solve the error, rename the variable in your code.

main.py
a_list = ['bobby', 'hadz', 'com'] numbers = [1, 2, 3] result = isinstance(numbers, list) print(result) # ๐Ÿ‘‰๏ธ True

rename the variable to solve the error

We renamed the variable to a_list, so it no longer shadows the built-in list class.

# Use the type() class to get around the error

If you aren't able to rename the variable, use the type() class.

main.py
list = ['bobby', 'hadz', 'com'] numbers = [1, 2, 3] result = isinstance(numbers, type(list)) print(result) # ๐Ÿ‘‰๏ธ True

The type class returns the type of an object.

main.py
print(type(['bobby', 'hadz', 'com'])) # ๐Ÿ‘‰๏ธ <class 'list'> print(type('bobbyhadz.com')) # ๐Ÿ‘‰๏ธ <class 'str'>

Most commonly the return value is the same as accessing the __class__ attribute on the object.

You can also pass an object of the correct type to the type() class, it doesn't have to be the variable that shadows the built-in class.

main.py
list = ['bobby', 'hadz', 'com'] numbers = [1, 2, 3] result = isinstance(numbers, type([])) print(result) # ๐Ÿ‘‰๏ธ True

However, the best solution is to rename the variable in your code to something that doesn't shadow a built-in Python class.

# Conclusion

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

  1. Make sure to only pass a class or a tuple of classes as the second argument. to the isinstance() function.
  2. Make sure to not shadow built-in classes by declaring a variable with the same name.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2025 Borislav Hadzhiev