Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention Python >= 3.9.2 as an alternative to typing_extensions.TypedDict #3374

Merged
merged 6 commits into from Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
10 changes: 8 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 @@ -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'
BvB93 marked this conversation as resolved.
Show resolved Hide resolved
),
):

class Model(BaseModel):
user: User
Expand Down