Introduction
Python newest version 3.11 is expected to be released in October 2022 even though the alpha version (3.11.0a6) is already out.
Today we will be discussing about the updates and new additions that are expected to be delivered with the newest version and also showcase how to install the 3.11 Alpha version in order to get used to the new functionality prior to its official release.
How to install Python 3.11 Alpha
The new features are implemented and tested for about five months prior to the final release of the new Python version. Therefore, the alpha phase is expected to end by May 2022.
A new alpha version is being released on (approximately) a monthly basis so that the core developers can get early access and make any adjustments if required. By the time this article was written, the sixth alpha version has been released (namely 3.11.0a6)— if you want to ensure what is the latest alpha version available you can simply refer to the official documentation.
Below you can find all the relevant information on how to install Python 3.11 alpha version on your local machine in order to experiment and get used to the new features, or even provide some feedback to the community.
Docker installation
If you want to install the Python 3.11 alpha version via Docker, then you first need to pull the [python:3.11-rc-slim](https://hub.docker.com/_/python) image
$ docker pull python:3.11-rc-slim
and finally run it
$ docker run -it --rm python:3.11-rc-slim
Ubuntu installation
$ sudo apt update && sudo apt upgrade -y
$ sudo apt install software-properties-common -y
$ sudo add-apt-repository ppa:deadsnakes/ppa -y
$ sudo add-apt-repository ppa:deadsnakes/nightly -y
$ sudo apt update
$ sudo apt install python3.11
Python 3.11 alpha should now be installed but you may also need to install some additional extras (for example if you want to be able to create a virtual environment):
$ sudo apt install python3.11-dev
$ sudo apt install python3.11-venv
$ sudo apt install python3.11-distutils
$ sudo apt install python3.11-lib2to3
$ sudo apt install python3.11-gdbm
$ sudo apt install python3.11-tk
OSX installation
For Mac users, the .pkg installation can be found in the official documentation. The latest alpha version available will be listed on the right-hand side under the Pre-Releases section.
Windows installation
In order to install the Python Alpha version on your Windows machine you can use [pyenv-win](https://pyenv-win.github.io/pyenv-win/).
C:> pyenv update
C:> pyenv install 3.11.0a6
And once the installation is completed you can then go ahead and create and activate a virtual environment with the Python Alpha version you’ve specified in the previous step:
C:> pyenv local 3.11.0a6
C:> python -m venv myenv
C:> myenvScriptsactivate.bat
Better Error Messages
The first major updated is related to the tracebacks printed on the standard output wherever an error is being raised. More precisely, Python 3.11 is expected to have an enhanced error location in the reported tracebacks.
Previous Python versions of the interpreter would only point to the specific line causing the error. This was quite ambiguous due to the fact that it wasn’t clear which expression or object was triggering the reported error.
For instance, let’s suppose that in our script called test.py we are making a call to my_test_function() method by passing two arguments namely a and b where the latter is None. Now if we attempt to use an attribute over the None object we will end up with an AttributeError.
With the enhanced error location, the traceback will point the specific function call as well as the specific expression that is causing the AttributeError as illustrated below.
Traceback (most recent call last):
File "test.py", line 15, in <module>
print(my_test_function(a, b))
^^^^^^^^^^^^^^^^^^^^^^^
File "test.py", line 3, in my_test_function
return abs(param1.x - param2.x) + abs(param1.y - param2.y)
^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'x'
This is also extremely helpful when dealing with deeply nested objects, such as dictionaries. For instance,
Traceback (most recent call last):
File "test.py", line 37, in <module>
extract_msg('my_key')
^^^^^^^^^^^^^^^^^^^^^
File "test.py", line 18, in extract_msg
return extract_counts(x)
^^^^^^^^^^^^^^^^
File "test.py", line 24, in extract_counts
return query_nums(x)
^^^^^^^^^^^^^
File "test.py", line 32, in query_nums
return response['a']['b']['c']['nums']
~~~~~~~~~~~~~~~^^^^^^^^
TypeError: 'NoneType' object is not subscriptable
Additionally, this is helpful when it comes to dealing with complex mathematical expressions. Let’s supposed that we attempt to divide a number by 0. The new error notation will point to the precise position that we attempt to perform this invalid division.
Traceback (most recent call last):
File "test.py", line 54, in <module>
print((x / y / z) * (a / b))
~~~~~~~~^
ZeroDivisionError: division by zero
For more details regarding this update you can refer to PEP-657.
Multiple Exception handling with except*
Another interesting language extension that is going to be packed into Python 3.11 is related to multiple exception handling. This feature will allow programs to raise and handle multiple exceptions (even unrelated) at the same time.
This functionality is facilitated by a new standard exception type called ExceptionGroup, that corresponds to a group of exceptions that are being propagated together. The new ExceptionGroup will be handled using a new except* syntax. The * symbol indicates that multiple exceptions can be handled by each except* clause.
For example, in order to raise multiple exceptions you could create an instance of ExceptionGroup:
raise ExceptionGroup('Example Exception Group', (
TypeError('Example TypeError'),
ValueError('Example ValueError'),
KeyError('Example KeyError'),
AttributeError('Example AttributeError')
))
And finally use the except* to handle either individual or multiple exceptions from the ExceptionGroup in a single clause as shown below:
try:
raise ExceptionGroup('Example ExceptionGroup', (
TypeError('Example TypeError'),
ValueError('Example ValueError'),
KeyError('Example KeyError'),
AttributeError('Example AttributeError')
))
except* TypeError:
...
except* ValueError as e:
...
except* (KeyError, AttributeError) as e:
...
For more details and rationales behind this update you can refer to PEP-654.
A new typing feature called Self
Python 3.11 will also implement a more intuitive way to annotate methods that return an instance of their class.
Until now we could achieve the same behaviour through the TypeVar approach as described in PEP-484, but this new feature is definitely easier and way more intuitive.
For example, let’s suppose we have a Person class with an instance method called update_name that returns an instance of the Person class. With the TypeVar notation, the type hinting would look like below.
from typing import TypeVar
TPerson = TypeVar('TPerson', bound='Person')
class Person:
def update_name(self: TPerson, name: str) -> TPerson:
self.name = name
return self
This old notation is quite unintuitive and unnecessarily verbose. With the new addition in Python 3.11 this is simplified to
from typing import Self
class Person:
def update_name(self, name: str) -> Self:
self.name = name
return self
Much cleaner and more intuitive, right?
For more details regarding this update you can refer to PEP-673.
A new module for parsing TOML files
Python 3.11 will bring a brand new module called [tomllib](https://docs.python.org/3.11/library/tomllib.html#module-tomllib), that was added to facilitate TOML parsing. Note that the module does not support writing TOML (at least for the time being).
tomllib can be used to load TOML from files
import tomllib
with open('settings.toml', 'rb') as f:
data = tomllib.load(f)
or strings:
import tomllib
settings = """
python-version = "3.11.0"
python-implementation = "CPython"
"""
data = tomllib.loads(settings)
For more details about this new addition you can refer to bpo-40050.
Some unittest functions are now deprecated
Apart from new additions and updates, Python 3.11 is also expected come with some functions being deprecated. Some of these include three functions in the unittest module. These are
unittest.findTestCases()unittest.makeSuite()unittest.getTestCaseNames()
and are scheduled for removal in Python 3.13. Instead, users are now expected to use the corresponding methods in [TestLoader](https://docs.python.org/3.11/library/unittest.html#unittest.TestLoader) module.
[unittest.TestLoader.loadTestsFromModule()](https://docs.python.org/3.11/library/unittest.html#unittest.TestLoader.loadTestsFromModule)[unittest.TestLoader.loadTestsFromTestCase()](https://docs.python.org/3.11/library/unittest.html#unittest.TestLoader.loadTestsFromTestCase)[unittest.TestLoader.getTestCaseNames()](https://docs.python.org/3.11/library/unittest.html#unittest.TestLoader.getTestCaseNames)
For more details about this new addition you can refer to bpo-5846.
CPython Performance Optimizations
The CPython interpreter in version 3.11 is expected to be much more optimized and significantly faster compared to previous versions.
For more details about performance gains on specific operations you can refer to the table published in the tweet shared below.
You can also find the original code to profile these operations on GitHub.
Final Thoughts
In today’s article we discussed about the exciting new additions and updates that are expected to come with Python 3.11 version release in early October.
To summarise, in Python 3.11 we expect to see more intuitive error messages through more fine grained error locations in tracebacks. Furthermore, we also explore the new standard exception type called ExceptionGroup that can be used to propagate multiple exceptions as well as the new except* clause that is used to handle multiple exceptions in a single clause.
Additionally, we discussed about the new typing feature called Self, that provides a more intuitive way for annotating methods that return an instance of their class. We also showcased how to use the newly added module called tomllib in order to parse TOML from files and strings.
Furthermore, we discussed about the unittest functions that are going to be deprecated in Python 3.11 and up for removal as of 3.13, as well as some important performance optimisations in CPython interpreter that is expected to be significantly faster compared to previous versions.
Finally, we also showcased how to install the latest available alpha version in case you want to experiment with the new features, get used to them or even provide some feedback to the community and the core developers that are actively working on the release.
For a more comprehensive read about the 3.11 release you can read the details in the corresponding section of the official documentation. In this article we discussed about a very small set of the upcoming updates. More deprecations, module updates and additions can be found in the link attached above.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
Related articles you may also like






