Submitting the form below will ensure a prompt response from us.
One of the most common and confusing errors Python developers run into is:
csharp
TypeError: 'type' object is not subscriptable
If you’ve seen this in your logs or terminal, don’t worry. It’s a relatively simple error once you understand why it happens and how Python handles types and classes.
TypeError: ‘type’ object is not subscriptable in Python – Want to know how to fix it? Keep reading.
In Python, a subscriptable object is something that supports indexing with square brackets [], like:
When you see this error, it means you’re trying to index (subscript) something that isn’t designed to be indexed—in this case, a type/class.
The most frequent situation where this occurs is when working with type annotations or generic types.
my_dict = dict[str, int]
This will throw:
csharp
TypeError: 'type' object is not subscriptable
Why? Because in Python versions before 3.9, built-in types like dict, list, and tuple are not subscriptable unless you import them from the typing module.
If you’re using Python < 3.9, update your type hint like this:
from typing import Dict
my_dict: Dict[str, int] = {"a": 1, "b": 2}
Python 3.9 introduced native generics support:
my_dict: dict[str, int] = {"a": 1, "b": 2}
No need to import from typing anymore!
class MyClass:
pass
obj = MyClass[0] # Will raise the TypeError
Fix:
obj = MyClass()
value = obj[0] # Only if MyClass defines __getitem__
def get_type():
return type
print(get_type()[0]) # type is not subscriptable
Fix:
print(type("abc")[0]) # Valid: type("abc") returns <class 'str'>, then str[0] returns 'a'
An object is subscriptable if it implements the __getitem__() method. You can check this like so:
print('__getitem__' in dir(dict)) #True
print('__getitem__' in dir(type)) #False
import sys
if sys.version_info < (3, 9):
print("Use typing.Dict, typing.List for generics.")
else:
print("You're good to use dict[str, int] and list[int].")
We help development teams debug and optimize Python code with precision. Don’t let bugs slow your release cycles.
The error “TypeError: ‘type’ object is not subscriptable” might sound intimidating, but it boils down to Python’s type system and how it evolved over versions. Whether you’re annotating types or dealing with class instances, understanding when and how to subscript will make your code safer and easier to maintain.
If you’re building scalable Python apps and want to catch these issues before they happen, consider integrating mypy, Pylint, or Pyright in your dev workflow.