Member-only story
4 New Type Annotation Features in Python 3.11
TypedDict, arbitrary literal string, and more
On Apr 25, 2022, Python published the latest alpha — 3.11.0a7, which represents the last release for the alpha phase for the 3.11 development. It’ll be moving to the beta phase early in May and the final official version of 3.11 is expected to be released in Oct 2022.
I know that it’s a big deal of work to adapt your applications to the latest Python, and most of the time, you don’t need to, because your applications are presumably working fine with an older version. However, if you’re the kind of person who likes to try new things, let’s get started with Python 3.11!
Note that you don’t want to modify your current applications because 3.11 is still in the alpha phase, but it doesn’t prevent us from trying it with a docker image — a separate virtual container that is independent of your applications.
Among the new features, I want to focus on type annotations in this article. Python is a dynamically typed language and the type annotations are used by many type checkers that can provide real-time code analysis. Many code bugs, such as type mismatches, can be identified while you’re still coding. Thus, the improvement of type annotations in Python 3.11 will help us write bug-free code.
Please note that we’re testing the alpha release, and these features are subject to change.
Prerequisites
Installing Python 3.11 with Docker
If you don’t have Docker installed on your computer, you can go to docker.com to find out the installation instruction. The idea of dockers is to create a reproducible container that streamlines the development of applications.
Once you install docker, you can run the following command on your command-line tool. In my case, I use the Terminal app on my Mac computer.
docker run -t -d python:3.11-rc-bullseye
This command pulls the image with the tag python:3.11-rc-bullseye
. Just in case you may be wondering what this tag means. 3.11
represents the version of Python, rc
means release candidate, and bullseye
is the Debian release bullseye, this image is based on this Debian…