Adventures in Machine Learning

Python 311 Beta: Enhanced Features Improved Performance and TOML Support

Python 3.11 Beta: Enhanced Features, Improved Performance, and More!

Python 3.11, the latest version of the world’s most preferred programming language, is already rocking the world of developers as the beta version is out. The Python 3.11 Beta is full of new features and improvements that are set to make writing code even more smooth and effortless.

In this article, we will be going over some of the key highlights of this Python 3.11 beta release and the new tomllib TOML parser. Overview of Python 3.11 Beta Release

Python 3.11 Beta is the pre-release testing phase aimed at identifying bugs, errors, and glitches before the final release.

This version comes with several significant updates that project an overall focus on usability, performance, and reliability.

Beta Testing

Beta testing is a key step to ensure that the final release is fast, efficient, and bug-free. The Python community welcomes anybody to participate in this testing phase and report any concerns with the new version.

To participate in beta testing, install the Python 3.11 beta version, test, and provide feedback on the forums.

Enhanced Error Messages

Python has always been lauded for its excellent error messages, making the debugging process much less complex. With the release of Python 3.11, this is even more prominent with enhanced error messages.

The new version provides more concise, clear messages that even beginners can understand. This simplicity comes as a result of the improved syntax of exception messages.

Task and Exception Groups

Python developers can now organize into groups by wrapping them in ‘Task Groups’ that contain several asyncio events. Also, Python 3.11 introduces ‘Exception Groups’, which allow for the management and cancellation of groups of exceptions raised.

This feature makes the programming process more structured and smooth, especially when dealing with chains of dependencies in a program.

TOML Support

Python 3.11 ushers in built-in support for the TOML (Tom’s Obvious, Minimal Language) format, which is becoming increasingly popular in the configuration file world. This is a much-needed feature, especially because of the growing tendency to prefer pyproject.toml to files for managing Python dependencies.

Static Typing Improvements

Python is dynamic and thus, adjusting to the changing type of objects at runtime, which can cause unpredictable bugs. The new Python 3.11 version supports stricter typing, ensuring that variable types remain immutable throughout the program’s runtime, meaning less buggy and more efficient code.


Speed is a crucial factor in programming, and Python developers can now experience faster speeds with the new version. Python 3.11 introduces an optimized python runtime that interprets python code faster.

This optimized runtime has been enhanced by adding an acceleration module that speeds up bytecode execution without altering the core language.

TOMLlib – Parsing TOML Documents

Python 3.11 has introduced TOML support, and with it comes tomllib, a tool that facilitates reading and parsing TOML documents. The TOMLlib module offers a robust, lightweight, and efficient parser that can be used to read configuration files.

Python Packaging & pyproject.toml

Before diving into TOMLlib, it is critical to understand pyproject.toml, a configuration file that has become the convention for Python package development. This file is used to store metadata, dependencies, and plugins when managing a Python project.

Pyproject.toml paves the way for declarative dependencies, encouraging creating isolated environments without project-specific dependencies.

TOML Format

TOML stands for Tom’s Obvious Minimal Language, a file format that is simple, clean and easy to read. It uses key-value pairs to store data, and it’s straightforward and less verbose than other options like JSON or YAML.

TOML has dynamic data types that include strings, booleans, integers, floats, and date-time. Its flexibility makes it perfect for storing configuration files, which are used across different applications.

How to Use TOMLlib

TOMLlib, the new utility added in Python 3.11, makes life easier. Let’s say we have a configuration file named pyproject.toml that we want to parse.

At the top of the script, we’d make the tomllib import, like this:


import tomllib


A ‘parse’ method is used to read and parse the TOML file and store the results in a dictionary object. “`

config = tomllib.parse(‘pyproject.toml’)


We can then print any value by calling it using the dictionary’s key, like this:






The release of Python 3.11 Beta brings in several new, groundbreaking features that make programming on the platform even more effortless. These include enhanced error messages, task and exception groups, support for the TOML configuration format, and more.

Furthermore, the addition of tomllib makes it easier than ever before to read and parse configuration files. Overall, Python 3.11 lays the foundation for a more exceptional and reliable Python platform that will undoubtedly enhance the programming experience for developers worldwide.

Learn Basic TOML: The Configuration File Format

When it comes to configuration files, the TOML format is quickly establishing itself as a popular choice due to its simplicity and flexibility. With TOML, programmers can define structured information in a way that’s both human-readable and easy to parse.

In this article, we’ll take a deep dive into the basics of TOML, including its syntax, purpose, and schema validation. We’ll also discuss how to use the tomllib module to read and parse TOML files efficiently.

Overview of

TOML Format and its Purpose

TOML stands for Tom’s Obvious, Minimal Language. Created by Tom Preston-Werner, co-founder of GitHub, TOML is a configuration file format that aims to be readable, maintainable, and concise.

It has grown in popularity due to its clarity, absence of ambiguities, and ease of use. While reading plain text files with simple key-value formats was a common practice in the past for configuration files, it proved to be insufficient to handle complex data architectures.

TOML was designed to organize and structure configuration data within a more structured, readable, and easy-to-edit format. Syntax of

TOML Format

The syntax of TOML is intuitive and straightforward, making it easy for anyone to read and understand.

TOML uses key-value pairs to store data, and each key follows the syntax of []. This makes it possible to define nested data structures within the data file.

Here’s an example of a simple TOML configuration:



name = “example_db”

host = “localhost”

port = 5432

user = “admin”

password = “admin1234”


The above is an example of a simple TOML configuration file that defines a group with the name database that includes several key-value pairs, like name, host, port, user, and password. Additionally, TOML also supports arrays, which can be defined using the bracket syntax.



type = [“apple”, “grape”, “orange”]


It is also possible to define nested tables in TOML. This allows you to structure your configuration data into more complex data hierarchies.

Here’s an example:



name = “example_db”


host = “localhost”

port = 5432

user = “admin”

password = “admin1234”


In the above example, a nested table is created to contain the connection information for the database. A section [database.connection] is used to group the host, port, user, and password information.

Informal Schema for Validating TOML Documents

Validating TOML documents can be achieved by establishing an informal schema for defining the expected structure of the configuration file. The schema represents the expected structure of the source file that is utilized to validate the target configuration file against it.

The schema also ensures that the TOML configuration file is well-formed, meets the specification requirements, and can be automatically parsed. PEP 517 and PEP 518 are two popular methods for validation of TOML files.

PEP 517 provides a standardized interface for building Python packages and checks that metadata is consistent across all build artifacts. PEP 518 provides a standardized way of defining dependencies in Python packages by relying on TOML as the file format of choice.

Both PEP 517 and PEP 518 go a long way in establishing schema validations and driving adoption of TOML within the Python community.

Read TOML with tomllib

Reading and parsing TOML files can be done simply and easily with the help of the tomllib module. The module offers a straightforward way to read and parse TOML files while handling the complexities of encoding and decoding data.

Here’s how to use the tomllib.load() function to read and parse TOML files efficiently:


import tomllib

# Open a TOML file in binary mode

with open(‘config.toml’, ‘rb’) as file:

# Pass the file object to the load() function

config = tomllib.load(file)

# Work with the configuration data

hostname = config[‘database’][‘host’]

port = config[‘database’][‘port’]

username = config[‘database’][‘user’]

password = config[‘database’][‘password’]


In the above example, we use the load() function from the tomllib module to read and parse a TOML file named ‘config.toml’. Once the data is parsed, it can be accessed via the dictionary config object, with keys representing the groups and key-value pairs defined in the TOML file.


Learning basic TOML can enable you to create well-structured configuration files with ease. TOML’s simple syntax and flexibility make it one of the most popular configuration file formats in use today.

Apart from being human-readable, it’s easy to parse, making it an ideal choice for modern applications. Knowing how to use the tomllib module to read and parse TOML files is a valuable skill for any Python developer dealing with configuration data.

Additionally, understanding informal schema validation concepts such as PEP 517 and PEP 518 can go a long way in ensuring that your TOML files meet the specification requirements and are well-formed. Write TOML: Third-Party Libraries and New Typing Features in Python 3.11

TOML (Tom’s Obvious, Minimal Language) has quickly established itself as a flexible and easy-to-read configuration file format.

However, writing TOML files can be less straightforward than reading them, especially when it comes to more complex data structures. In this article, we will explore third-party libraries for writing TOML files, as well as discuss why the tomllib module doesn’t include write functionality.

We’ll also take a look at some of the new typing features included in Python 3.11, such as the Self type, LiteralString type, and

Variadic Generic Third-Party Libraries for Writing TOML Files

While the tomllib module in Python 3.11 provides an efficient and easy way to read and parse TOML files, it doesn’t support writing TOML files. This is because the underlying TOML specification has limitations when it comes to writing data structures and doesn’t offer a clear way to serialize Python objects to the corresponding TOML format.

However, there are several third-party libraries that provide functionality to write TOML files. One such library is tomlkit, a Python-based TOML serialization and parsing library.

It provides TOML’s full functionality, including writing, reading, and creating TOML documents, while maintaining simplicity and readability. It also includes several useful features such as automatic handling of nested data structures and comments.

Another popular third-party solution is PyYAML, a YAML-based data serialization and deserialization library that supports TOML conversion. PyYAML provides a BidirectionalEncoder, which can convert the TOML data to Python objects and vice versa.

Why Write Functionality was not Included in tomllib

The reason why the tomllib module doesn’t include write functionality is due to the limitations of the underlying TOML specification. TOML doesn’t provide a clear way to serialize Python objects to TOML format, which makes it difficult to write complex data structures.

Additionally, the extensions allowed by TOML such as comments and duplicate keys, make it more challenging to write Python objects to TOML file. The creators of the tomllib module aimed to keep the parsing process of TOML files simple and concise, without bloating the module with write functionality and potentially risking New Typing Features in Python 3.11

Python’s dynamic typing system has been a key feature of the language that makes it flexible and easy to use.

However, it also has some limitations, making it difficult to identify and prevent errors and catches bugs early in development. Python 3.11 introduces several new typing features that provide more robust, strict and predictable type annotations, which will help make Python code safer and easier to maintain.

Self Type

The Self type, introduced in Python 3.11, is a forward reference that enables developers to type hint the type of the class they are defining before the class has been defined fully. This new feature allows type annotations to reference ‘self’ before the class has been fully defined, making the code easier to read and more predictable.

Here is an example of using the Self type:


from __future__ import annotations

class Person:

name: str

email: str

def __init__(self, name: str, email: str): = name = email

def send_email(self, recepient: Person) -> None:

… “`

In the above example, the receiver parameter of the `send_email()` method is typed as a Person object.

Since the Person class is defined later in the program, we use a forward reference to type the parameter.

Literal String Type


Literal String Type is another new typing feature in Python 3.11. It allows developers to declare strict string types to avoid the common mistake of passing the wrong string constant as an argument, leading to hard-to-find bugs.

Here is an example:


from typing import Literal

def color_picker(color: Literal[‘red’, ‘green’, ‘blue’]) -> str:

return f”The chosen color is {color}”


The `Literal` type above accepts only the strings ‘red’, ‘green’ or ‘blue’ as valid inputs. If any value other than the specified strings is passed, the type checker would generate an error.

Variadic Generic Types

Variadic Generic Types is a new feature that allows developers to specify a variable number of arguments during type hinting while at the same time enforcing the type of the arguments. Here is an example:


from typing import Tuple, TypeVar, List

T = TypeVar(‘T’)

def repeat(n: int, value: T) -> List[T]:

return [value] * n

def first_two(*args: Tuple[T, T]) -> Tuple[T, T]:

return args[0], args[1]

print(repeat(3, ‘Python’))

print(first_two((‘Python’, ‘is’), (‘popular’, ‘!’)))


The code above will return the following output:


[‘Python’, ‘Python’, ‘Python’]

((‘Python’, ‘is’), (‘popular’, ‘!’))


In the above example, the `repeat()` function takes the number of times to repeat and the value to be repeated as parameters and returns a list of values.

By using the TypeVar `T`, the type of the arguments is enforced.


Writing TOML files with complex data structures is not a straightforward task, but several third-party libraries can facilitate the process. New typing features introduced in Python 3.11, such as the Self type,

Literal String Type, and

Variadic Generic Types, contribute to the language’s safety and predictability, making Python more accessible to use and maintain.

Keeping up with the latest Python versions and staying informed on the latest coding practices can enhance the development process and lead to creating better software applications.