Skip to content

What's New in Python 3.11

3.11

Python 3.11 landed in October 2022. The two big stories are the tomllib standard library module and exception groups — plus the interpreter became roughly 25% faster than 3.10.


Summary

Category Change
Performance ~25% faster than 3.10
Language Exception groups and ExceptionGroup (PEP 654)
Language except* clause for exception groups
Language Fine-grained error locations in tracebacks
Typing Self type (PEP 673), Unpack, TypeVarTuple (PEP 646)
Standard Library tomllib — TOML parser
Standard Library math.exp2(), math.cbrt()

Exception Groups and except* — PEP 654

Before 3.11, if multiple concurrent tasks raised exceptions, you could only propagate one at a time. ExceptionGroup lets you group multiple exceptions together and except* lets you handle each type separately.

# Create an exception group
eg = ExceptionGroup("multiple errors", [
    ValueError("bad value"),
    TypeError("wrong type"),
    ValueError("another bad value"),
])

# Handle different exception types from the group
try:
    raise eg
except* ValueError as eg:
    for exc in eg.exceptions:
        print(f"ValueError: {exc}")
except* TypeError as eg:
    for exc in eg.exceptions:
        print(f"TypeError: {exc}")
Output
ValueError: bad value ValueError: another bad value TypeError: wrong type

This is particularly useful with asyncio — when multiple async tasks fail simultaneously, you now get all the errors, not just the first one.


Better Error Messages / Tracebacks

3.11 added exact caret (^) highlighting to point at the specific token causing the error, not just the whole line:

Traceback (most recent call last):
  File "example.py", line 5, in <module>
    result = (x + y) * (a + b)
                       ^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'

This makes debugging much faster — especially for nested expressions.


tomllib — Built-in TOML Parser

Python finally ships with a TOML parser. TOML is the format used by pyproject.toml, and now you can read it without installing tomli:

import tomllib

with open("pyproject.toml", "rb") as f:
    config = tomllib.load(f)

print(config["project"]["name"])
print(config["project"]["version"])

Read-only

tomllib only reads TOML. For writing TOML, you still need a third-party library like tomli-w.


Self Type — PEP 673

Self lets you type methods that return self in a way that works correctly with inheritance:

from typing import Self

class Builder:
    def set_name(self, name: str) -> Self:
        self.name = name
        return self

    def set_value(self, value: int) -> Self:
        self.value = value
        return self

class ExtendedBuilder(Builder):
    def set_extra(self, extra: str) -> Self:
        self.extra = extra
        return self

# Type checkers know this returns ExtendedBuilder, not just Builder
result = ExtendedBuilder().set_name("x").set_value(1).set_extra("y")

Performance: ~25% Faster

3.11 shipped a major rewrite of the CPython bytecode interpreter (PEP 659 — specializing adaptive interpreter). Numeric loops, function calls, and attribute lookups are all noticeably faster. The classic benchmark: the pyperformance suite improved ~25% over 3.10 across the board.