Python 3.11 Preview: TOML and tomllib

Python 3.11 Preview: TOML and tomllib

by Geir Arne Hjelle Reading time estimate 35m intermediate data-structures python

Python 3.11 is getting closer to its final release, which will happen in October 2022. The new version is currently going through beta testing, and you can install it yourself to preview and test some of the new features, including support for reading TOML with the new tomllib module.

TOML is a configuration file format that’s getting more and more popular in the Python ecosystem. This is driven by the adoption of pyproject.toml as the central configuration file in Python packaging. Other important tools, like Black, mypy, and pytest, also use TOML for their configuration.

In this tutorial, you’ll:

  • Install Python 3.11 beta on your computer, next to your current Python installations
  • Get familiar with the basics of the TOML format
  • Read TOML files with the new tomllib module
  • Write TOML with third-party libraries and learn why this functionality is not included in tomllib
  • Explore Python 3.11’s new typing features, including the Self and LiteralString types as well as variadic generics

There are many other new features and improvements coming in Python 3.11. Check out what’s new in the changelog for an up-to-date list, and read other Python 3.11 previews on Real Python to learn about other features.

Python 3.11 Beta

A new version of Python is released in October each year. The code is developed and tested over a seventeen-month period before the release date. New features are implemented during the alpha phase. For Python 3.11, seven alpha releases were made between October 2021 and April 2022.

The first beta release of Python 3.11 happened in the early hours of May 8, 2022. Each such pre-release is coordinated by a release manager—currently Pablo Galindo Salgado—and ties together hundreds of commits from Python’s core developers and other volunteers.

This release also marked the feature freeze for the new version. In other words, no new features will be added to Python 3.11 that aren’t already present in Python 3.11.0b1. Instead, the time between the feature freeze and the release date—October 3, 2022—is used to test and solidify the code.

About once a month during the beta phase, Python’s core developers release a new beta version to continue showing off the new features, testing them, and getting early feedback. Currently, the latest beta version of Python 3.11 is 3.11.0b3, released on June 1, 2022.

If you’re maintaining your own Python package, then the beta phase is an important period when you should start testing your package with the new version. Together with the community, the core developers want to find and fix as many bugs as possible before the final release.

Cool New Features

Some of the highlights of Python 3.11 include:

  • Enhanced error messages, which help you more effectively debug your code
  • Task and exception groups, which streamline the use of asynchronous code and allow programs to raise and handle multiple exceptions at the same time
  • TOML support, which allows you to parse TOML documents using the standard library
  • Static typing improvements, which let you annotate your code more precisely
  • Optimizations, which promise to make Python 3.11 significantly faster than previous versions

There’s a lot to look forward to in Python 3.11! You can already read about the enhanced error messages and task and exception groups in earlier Python 3.11 preview articles. For a comprehensive overview, check out Python 3.11: Cool New Features for You to Try.

In this tutorial, you’ll focus on how you can use the new tomllib library to read and parse TOML files. You’ll also get a short peek at some of the typing improvements that’ll be shipping with Python 3.11.

Installation

To play with the code examples in this tutorial, you’ll need to install a version of Python 3.11 onto your system. In this subsection, you’ll learn about a few different ways to do this: using Docker, using pyenv, or installing from source. Pick the one that works best for you and your system.

If you have access to Docker on your system, then you can download the latest version of Python 3.11 by pulling and running the python:3.11-rc-slim Docker image:

Language: Shell
$ docker pull python:3.11-rc-slim
3.11-rc-slim: Pulling from library/python
[...]
docker.io/library/python:3.11-rc-slim

$ docker run -it --rm python:3.11-rc-slim

This drops you into a Python 3.11 REPL. Check out Run Python Versions in Docker for more information about working with Python through Docker, including how to run scripts.

The pyenv tool is great for managing different versions of Python on your system, and you can use it to install Python 3.11 beta if you like. It comes with two different versions, one for Windows and one for Linux and macOS. Choose your platform with the switcher below:

On Windows, you can use