PythonMastery
reference 3 min read · lesson 74 of 80 in Errors

TypeError: 'type' object is not subscriptable

1 · The lesson

read

What this error means

You put square brackets after a class itself, not after an object made from that class. A class like Point or str is a "type": the blueprint. Square brackets, like x[0], ask for an item from a value that holds items. The blueprint holds no items, so Python refuses. Modern Python names the class in the message: type 'Point' is not subscriptable. Older versions of Python word the same error as 'type' object is not subscriptable.

When you see it

python
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    first = Point[0]
            ~~~~~^^^
TypeError: type 'Point' is not subscriptable

The same error in a type hint, before the function is even called:

python
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    def unpack(b: Box[int]) -> int:
                  ~~~^^^^^
TypeError: type 'Box' is not subscriptable

Why it happens

1. You forgot to create the object. Point is the class. Point(1, 2) is an object made from it. The brackets belong on the object, or on a list inside it:

python
class Point:
    def __init__(self, x, y):
        self.coords = [x, y]

first = Point[0]       # TypeError: the class, not a point

2. A type hint on your own class. list[int] works in hints because list supports it. Your own class does not, unless you make it generic (see the fix below). Up to Python 3.13, hints in a def line are evaluated when the function is defined, so the error appears as soon as the file runs. Python 3.14 waits until something reads the hints, so the same line may not fail there until a tool like a validator or a dataclass looks at them.

3. list[int] on Python 3.8 or older. Writing list[int], dict[str, int] or tuple[int, ...] in type hints only works from Python 3.9. On 3.8 and older it fails with TypeError: 'type' object is not subscriptable. This is the most common cause when you run newer code on an old server or an old Docker image.

4. A built-in name where you meant your variable. str[0] fails because str is the class. Careful: list[0] and dict["a"] don't fail at all on Python 3.9 and newer. They quietly create a type hint object, so the bug shows up later:

python
numbers = [4, 8, 15]
print(list[0])         # list[0]   (not 4: this is a type hint, not your list)
print(numbers[0])      # 4

How to fix it

Create the object, then index what it holds:

python
class Point:
    def __init__(self, x, y):
        self.coords = [x, y]

p = Point(3, 4)
print(p.coords[0])     # 3

If you want p[0] to work directly, give the class a __getitem__ method:

python
class Point:
    def __init__(self, x, y):
        self.coords = [x, y]

    def __getitem__(self, index):
        return self.coords[index]

p = Point(3, 4)
print(p[0], p[1])      # 3 4

Make your class generic if you want Box[int] in type hints. Generic works on every Python 3 version the editor offers:

python
from typing import Generic, TypeVar

T = TypeVar("T")

class Box(Generic[T]):
    def __init__(self, item: T):
        self.item = item

def unpack(b: Box[int]) -> int:
    return b.item

print(unpack(Box(5)))  # 5

On Python 3.12 and newer you can write the shorter class Box[T]: instead.

On Python 3.8 or older, use the typing names for built-in collections, or upgrade Python:

python
from typing import Dict, List

def total(scores: Dict[str, List[int]]) -> int:
    return sum(sum(v) for v in scores.values())

print(total({"Ada": [90, 85], "Grace": [70]}))   # 245

Another option is from __future__ import annotations as the very first line of the file (Python 3.7 and newer). It stops Python from evaluating hints when the function is defined, so list[int] in a hint no longer runs on old versions. It does not help for list[int] used outside hints.

python
from __future__ import annotations

class Box:
    pass

def unpack(b: Box[int]) -> int:   # not evaluated, so no error
    return 0

print(unpack(Box()))   # 0

Use your variable, not the built-in name. If the line says list[...], str[...] or dict[...], check that you didn't mean numbers[...], name[...] or scores[...].

When you'd see this in real code

  • A project written on Python 3.11 deployed to a server or a Docker image with Python 3.8, failing on its first list[str] hint.
  • A dataclass or Pydantic model used as Model[...] without being declared generic.
  • typing code copied from a recent answer into an older project.
  • In pandas: pd.DataFrame["price"] instead of df["price"], indexing the class instead of your table.

See Also

Found this useful? Share it with a friend who’s learning Python.