Club.noww.in
This article goes over the major improvements made to the typing module in Python 3.15.
Since static type hinting is becoming more and more popular in the Python world, every Python release brings with it major improvements to the world of typing. This article goes over the main improvements made to typing in Python 3.15 like the improvements to TypedDict, the addition of typing.TypeForm, or the addition of the decorator typing.disjoint_base.
15 days of Python 3.15
To celebrate the release of Python 3.15, over the next 15 business days I'll be writing about a new 3.15 feature every day. Explained clearly and with examples so you don't have to sift through the changelog.
Subscribe to receive this free email series:
If you want, you can also check the schedule of the upcoming emails.
New class parameters for TypedDict
Python 3.15 added two class parameters to TypedDict: extra_items and closed. The gist of it is that extra_items allows specifying that your dictionary may have other, unknown keys whose values have the specified type:
from typing import TypedDict class Movie(TypedDict, extra_items=str): title: str year: intThe definition above means that a variable of type Movie might have other keys, as long as their values are assignable to the type str:
movie: Movie = { "title": "Forrest Gump", "year": 1994, "genre": "Comedy / Romance", }The extra item "genre": "Comedy / Romance" is allowed because the value is a string and extra_items=str.
To completely disallow extra items, regardless of their type, you can set closed=True as a class parameter.
There's an article dedicated to the improvements made to TypedDict in Python 3.15 that...












English (US)