-
-
Notifications
You must be signed in to change notification settings - Fork 535
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
Pydantic v2 support #2972
Pydantic v2 support #2972
Changes from 28 commits
ee45b97
70c9423
a66452b
fe52908
356a5fb
0c45267
37b8c2e
f45d576
dc0b9dd
1171abc
43dcf1d
969112b
107ea3c
612a30c
50d71fb
e5673c1
16c674d
94d4844
a116364
58e2f6f
7696efd
79c41d2
3b3a392
817c325
bfd7ac4
9314609
af3299e
8cd57d9
eec5c8e
f17ee94
5afee6b
db26d26
01182e2
d78d907
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Release type: minor | ||
|
||
Adds initial support for pydantic V2. | ||
|
||
This is extremely experimental for wider initial testing. | ||
|
||
We do not encourage using this in production systems yet. |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ flask = {version = ">=1.1", optional = true} | |
opentelemetry-api = {version = "<2", optional = true} | ||
opentelemetry-sdk = {version = "<2", optional = true} | ||
chalice = {version = "^1.22", optional = true} | ||
pydantic = {version = "<2", optional = true} | ||
pydantic = {version = ">1.6.1", optional = true} | ||
python-multipart = {version = ">=0.0.5,<0.0.7", optional = true} | ||
sanic = {version = ">=20.12.2", optional = true} | ||
aiohttp = {version = "^3.7.4.post0", optional = true} | ||
|
@@ -112,7 +112,7 @@ channels = "^3.0.5" | |
Django = ">=3.2" | ||
fastapi = {version = ">=0.65.0", optional = false} | ||
flask = ">=1.1" | ||
pydantic = {version = "<2", optional = false} | ||
pydantic = {version = ">1.6.1", optional = false} | ||
pytest-aiohttp = "^1.0.3" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set this back to >1.6.1 and update lock file (am I doing this right?) |
||
pytest-django = {version = "^4.5"} | ||
pytest-flask = {version = "^1.2.0"} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
import builtins | ||
from decimal import Decimal | ||
from typing import Any, List, Optional, Type | ||
from typing import Any, List, Optional, Type, Union | ||
from uuid import UUID | ||
|
||
import pydantic | ||
from pydantic import BaseModel | ||
from pydantic.typing import get_args, get_origin, is_new_type, new_type_supertype | ||
from pydantic.utils import lenient_issubclass | ||
|
||
from strawberry.experimental.pydantic.exceptions import ( | ||
UnregisteredTypeException, | ||
UnsupportedTypeError, | ||
) | ||
from strawberry.experimental.pydantic.v2_compat import ( | ||
IS_PYDANTIC_V2, | ||
get_args, | ||
get_origin, | ||
is_new_type, | ||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should have these on our typing module? |
||
lenient_issubclass, | ||
new_type_supertype, | ||
) | ||
from strawberry.types.types import StrawberryObjectDefinition | ||
|
||
try: | ||
from types import UnionType as TypingUnionType | ||
from typing import GenericAlias as TypingGenericAlias # type: ignore | ||
except ImportError: | ||
import sys | ||
|
@@ -25,6 +32,10 @@ | |
TypingGenericAlias = () | ||
else: | ||
raise | ||
if sys.version_info < (3, 10): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, not sure if anyone will read this comment, but it is related to this change I believe. On Python 3.9 I'm noticing some issues here when (indirectly) importing from this module:
On Python 3.10 and 3.11 it works fine. From reading the import handling here I think it makes sense because it seems that this is what happens for different python versions; Python 3.8 -> ImportError on UnionType
Python 3.9 -> ImportError on UnionType
Python 3.10 -> (no import error)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mark90 this should be fixed now 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great :) Thanks for notifying me! |
||
TypingUnionType = () | ||
else: | ||
raise | ||
|
||
ATTR_TO_TYPE_MAP = { | ||
"NoneStr": Optional[str], | ||
|
@@ -70,23 +81,31 @@ | |
"RedisDsn": str, | ||
} | ||
|
||
|
||
FIELDS_MAP = { | ||
getattr(pydantic, field_name): type | ||
for field_name, type in ATTR_TO_TYPE_MAP.items() | ||
if hasattr(pydantic, field_name) | ||
} | ||
"""TODO: | ||
Most of these fields are not supported by pydantic V2 | ||
""" | ||
FIELDS_MAP = ( | ||
{ | ||
getattr(pydantic, field_name): type | ||
for field_name, type in ATTR_TO_TYPE_MAP.items() | ||
if hasattr(pydantic, field_name) | ||
} | ||
if not IS_PYDANTIC_V2 | ||
else {} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think quite a few are still supported, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will investigate further in an issue / PR because i think pydantic v2's types are just normal annotated types that should work out of the box |
||
|
||
|
||
def get_basic_type(type_: Any) -> Type[Any]: | ||
if lenient_issubclass(type_, pydantic.ConstrainedInt): | ||
return int | ||
if lenient_issubclass(type_, pydantic.ConstrainedFloat): | ||
return float | ||
if lenient_issubclass(type_, pydantic.ConstrainedStr): | ||
return str | ||
if lenient_issubclass(type_, pydantic.ConstrainedList): | ||
return List[get_basic_type(type_.item_type)] # type: ignore | ||
if not IS_PYDANTIC_V2: | ||
# only pydantic v1 has these | ||
if lenient_issubclass(type_, pydantic.ConstrainedInt): | ||
return int | ||
if lenient_issubclass(type_, pydantic.ConstrainedFloat): | ||
return float | ||
if lenient_issubclass(type_, pydantic.ConstrainedStr): | ||
return str | ||
if lenient_issubclass(type_, pydantic.ConstrainedList): | ||
return List[get_basic_type(type_.item_type)] # type: ignore | ||
|
||
if type_ in FIELDS_MAP: | ||
type_ = FIELDS_MAP.get(type_) | ||
|
@@ -125,7 +144,8 @@ def replace_types_recursively(type_: Any, is_input: bool) -> Any: | |
|
||
if isinstance(replaced_type, TypingGenericAlias): | ||
return TypingGenericAlias(origin, converted) | ||
|
||
if isinstance(replaced_type, TypingUnionType): | ||
return Union[converted] | ||
replaced_type = replaced_type.copy_with(converted) | ||
|
||
if isinstance(replaced_type, StrawberryObjectDefinition): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errr otherwise starlette goes boom