From fd7ffbd100f342001a424f4a044bde4556ed4e0d Mon Sep 17 00:00:00 2001 From: Mark Larah Date: Mon, 27 Jan 2025 08:38:18 -0600 Subject: [PATCH 1/3] [docs] Add example of using `@final` with TypedDict Fixes https://github.com/python/mypy/issues/18543 This is a useful but also non-obvious pattern that seems worthy of a docs example :) --- docs/source/typed_dict.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/source/typed_dict.rst b/docs/source/typed_dict.rst index bbb10a12abe8..4d578b290928 100644 --- a/docs/source/typed_dict.rst +++ b/docs/source/typed_dict.rst @@ -289,6 +289,32 @@ need to give each TypedDict the same key where each value has a unique :ref:`Literal type `. Then, check that key to distinguish between your TypedDicts. +You can also use :py:class:`@final ` to indicate that an +instance of TypedDict will not be reassigned or subclassed. This allows mypy to do type refinement based on unique +properties of the TypedDict: + +.. code-block:: python + + from typing import TypedDict, final + + @final + class Movie(TypedDict): + director: str + runtime: int + + @final + class Book(TypedDict): + author: str + pages: int + + def foo(movie_or_book: Movie | Book) -> None: + if 'director' in movie_or_book: + director = movie_or_book['director'] + runtime = movie_or_book['runtime'] + elif 'author' in movie_or_book: + author = movie_or_book['author'] + pages = movie_or_book['pages'] + Inline TypedDict types ---------------------- From 5f8799fe0f8bf3958b05b89dddaa5e020e7ac806 Mon Sep 17 00:00:00 2001 From: Mark Larah Date: Mon, 27 Jan 2025 08:48:22 -0600 Subject: [PATCH 2/3] Update typed_dict.rst --- docs/source/typed_dict.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/typed_dict.rst b/docs/source/typed_dict.rst index 4d578b290928..71c7720f34c2 100644 --- a/docs/source/typed_dict.rst +++ b/docs/source/typed_dict.rst @@ -289,8 +289,8 @@ need to give each TypedDict the same key where each value has a unique :ref:`Literal type `. Then, check that key to distinguish between your TypedDicts. -You can also use :py:class:`@final ` to indicate that an -instance of TypedDict will not be reassigned or subclassed. This allows mypy to do type refinement based on unique +You can also use ``@final`` to indicate that an instance of TypedDict will not be +reassigned or subclassed. This allows mypy to do type refinement based on unique properties of the TypedDict: .. code-block:: python From 60f81249c47ae5dbbbfbe5342e8792ebd2d537f5 Mon Sep 17 00:00:00 2001 From: Mark Larah Date: Thu, 30 Jan 2025 15:05:02 -0600 Subject: [PATCH 3/3] Update typed_dict.rst --- docs/source/typed_dict.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/typed_dict.rst b/docs/source/typed_dict.rst index 71c7720f34c2..75750b571208 100644 --- a/docs/source/typed_dict.rst +++ b/docs/source/typed_dict.rst @@ -289,9 +289,9 @@ need to give each TypedDict the same key where each value has a unique :ref:`Literal type `. Then, check that key to distinguish between your TypedDicts. -You can also use ``@final`` to indicate that an instance of TypedDict will not be -reassigned or subclassed. This allows mypy to do type refinement based on unique -properties of the TypedDict: +You can also use ``@final`` to indicate that the TypedDict definition will not be +subclassed. This allows mypy to do type refinement based on unique properties of +the TypedDict: .. code-block:: python