Skip to content

Commit

Permalink
Mention Python >= 3.9.2 as an alternative to `typing_extensions.Typed…
Browse files Browse the repository at this point in the history
…Dict` (#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 <samcolvin@gmail.com>

* Update `TypedDict` exception message in the test suite

* linting

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
3 people committed Dec 11, 2021
1 parent ce316cd commit e012089
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions changes/3374-BvB93.md
@@ -0,0 +1 @@
Mention Python >= 3.9.2 as an alternative to `typing_extensions.TypedDict`.
2 changes: 1 addition & 1 deletion pydantic/annotated_types.py
Expand Up @@ -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.'
)

Expand Down
7 changes: 5 additions & 2 deletions tests/test_annotated_types.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e012089

Please sign in to comment.