From dde620107a81dbf92a05c3ec64028e98c1e04b83 Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 30 Oct 2021 14:13:17 +0200 Subject: [PATCH 1/6] Mention python >= 3.9.2 as an alternative to `typing_extensions.TypedDict` --- pydantic/annotated_types.py | 2 +- tests/test_annotated_types.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pydantic/annotated_types.py b/pydantic/annotated_types.py index 2af344664f..1f0d27211d 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` or 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 4b4beab074..573424bc07 100644 --- a/tests/test_annotated_types.py +++ b/tests/test_annotated_types.py @@ -215,7 +215,13 @@ 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` ' + 'or upgrade to Python >= 3.9.2' + ), + ): class Model(BaseModel): user: User From 0089ff5cf7b97d90c42d5245101b3e15183afe0f Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 30 Oct 2021 14:25:25 +0200 Subject: [PATCH 2/6] Narrow the upper version for `LegacyTypedDict`: 3.9 -> 3.9.2 --- tests/test_annotated_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_annotated_types.py b/tests/test_annotated_types.py index 573424bc07..fb728b7814 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: From 99a38cbe87c1d53b5bc754b8f045a52dc4dba4bd Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Sat, 30 Oct 2021 14:34:22 +0200 Subject: [PATCH 3/6] Add an entry to `changes` --- changes/3374-BvB93.md | 1 + 1 file changed, 1 insertion(+) 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`. From 27c50272d40968b1ef3998147516d37b5f4fea1d Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 10 Dec 2021 11:19:04 +0100 Subject: [PATCH 4/6] Update pydantic/annotated_types.py Co-authored-by: Samuel Colvin --- pydantic/annotated_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic/annotated_types.py b/pydantic/annotated_types.py index 1f0d27211d..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` or Python >= 3.9.2. ' + '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.' ) From b49afccdf1b92298ca039a93510cca0dbc84fa47 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Fri, 10 Dec 2021 11:52:56 +0100 Subject: [PATCH 5/6] Update `TypedDict` exception message in the test suite --- tests/test_annotated_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_annotated_types.py b/tests/test_annotated_types.py index fb728b7814..7882ceb3ac 100644 --- a/tests/test_annotated_types.py +++ b/tests/test_annotated_types.py @@ -219,7 +219,7 @@ class User(OptionalUser): TypeError, match=( '^You should use `typing_extensions.TypedDict` instead of `typing.TypedDict` ' - 'or upgrade to Python >= 3.9.2' + 'with Python < 3.9.2.' ), ): From 2a3b9656f82474e160af9918f4bce72e10c95721 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Fri, 10 Dec 2021 16:51:04 +0000 Subject: [PATCH 6/6] linting --- tests/test_annotated_types.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_annotated_types.py b/tests/test_annotated_types.py index 7882ceb3ac..774051bb9d 100644 --- a/tests/test_annotated_types.py +++ b/tests/test_annotated_types.py @@ -217,10 +217,7 @@ class User(OptionalUser): with pytest.raises( TypeError, - match=( - '^You should use `typing_extensions.TypedDict` instead of `typing.TypedDict` ' - 'with Python < 3.9.2.' - ), + match='^You should use `typing_extensions.TypedDict` instead of `typing.TypedDict` with Python < 3.9.2.', ): class Model(BaseModel):