From e012089f1209106d215a8c3d87920e18bb920b6e Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 11 Dec 2021 22:44:09 +0100 Subject: [PATCH] Mention Python >= 3.9.2 as an alternative to `typing_extensions.TypedDict` (#3374) * Mention python >= 3.9.2 as an alternative to `typing_extensions.TypedDict` * Narrow the upper version for `LegacyTypedDict`: 3.9 -> 3.9.2 * Add an entry to `changes` * Update pydantic/annotated_types.py Co-authored-by: Samuel Colvin * Update `TypedDict` exception message in the test suite * linting Co-authored-by: Samuel Colvin Co-authored-by: Samuel Colvin --- changes/3374-BvB93.md | 1 + pydantic/annotated_types.py | 2 +- tests/test_annotated_types.py | 7 +++++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 changes/3374-BvB93.md diff --git a/changes/3374-BvB93.md b/changes/3374-BvB93.md new file mode 100644 index 0000000000..340c6ef099 --- /dev/null +++ b/changes/3374-BvB93.md @@ -0,0 +1 @@ +Mention Python >= 3.9.2 as an alternative to `typing_extensions.TypedDict`. diff --git a/pydantic/annotated_types.py b/pydantic/annotated_types.py index 2af344664f..86f79341b8 100644 --- a/pydantic/annotated_types.py +++ b/pydantic/annotated_types.py @@ -22,7 +22,7 @@ def create_model_from_typeddict( # Best case scenario: with python 3.9+ or when `TypedDict` is imported from `typing_extensions` if not hasattr(typeddict_cls, '__required_keys__'): raise TypeError( - 'You should use `typing_extensions.TypedDict` instead of `typing.TypedDict`. ' + 'You should use `typing_extensions.TypedDict` instead of `typing.TypedDict` with Python < 3.9.2. ' 'Without it, there is no way to differentiate required and optional fields when subclassed.' ) diff --git a/tests/test_annotated_types.py b/tests/test_annotated_types.py index efff5348c8..e9065dfb6a 100644 --- a/tests/test_annotated_types.py +++ b/tests/test_annotated_types.py @@ -13,7 +13,7 @@ from pydantic import BaseModel, PositiveInt, ValidationError -if sys.version_info < (3, 9): +if sys.version_info < (3, 9, 2): try: from typing import TypedDict as LegacyTypedDict except ImportError: @@ -221,7 +221,10 @@ class OptionalUser(LegacyTypedDict, total=False): class User(OptionalUser): id: int - with pytest.raises(TypeError, match='^You should use `typing_extensions.TypedDict` instead of `typing.TypedDict`'): + with pytest.raises( + TypeError, + match='^You should use `typing_extensions.TypedDict` instead of `typing.TypedDict` with Python < 3.9.2.', + ): class Model(BaseModel): user: User