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

[wip] Pydantic v2 #2751

Closed
Closed
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0b4b7c0
bump reflex_hosting_cli, pydantic and fastapi deps
benedikt-bartscher Mar 1, 2024
25d0585
prepare base
benedikt-bartscher Feb 28, 2024
8e692cd
state adjustments
benedikt-bartscher Feb 28, 2024
95f9b12
types adjustments
benedikt-bartscher Feb 28, 2024
8b2d0b4
outer_type_ -> annotation
benedikt-bartscher Feb 28, 2024
ce06bf0
migrate config to pydantic v2
benedikt-bartscher Feb 28, 2024
67c8148
migrate remaining __fields__ to model_fields
benedikt-bartscher Feb 28, 2024
66f1e21
fix backend vars
benedikt-bartscher Feb 28, 2024
fd32942
parse_obj is deprecated in favor of model_validate
benedikt-bartscher Feb 28, 2024
6291c39
minor state fixes, and var serialization
benedikt-bartscher Feb 28, 2024
a5d197c
serialization fixes
benedikt-bartscher Feb 28, 2024
faebd22
black fixes
benedikt-bartscher Feb 28, 2024
2def8b9
FieldInfo has no name anymore
benedikt-bartscher Feb 28, 2024
c057f2e
minor pydantic fixups
benedikt-bartscher Feb 29, 2024
7f8a457
migrate to field.annotation and fix default var wrapping for undefined
benedikt-bartscher Feb 29, 2024
5c584da
add missing default values to icon and link
benedikt-bartscher Feb 29, 2024
1abc57c
fix default value for theme in ChakraProvider
benedikt-bartscher Feb 29, 2024
4d497e0
hacky workaround to allow __class_getitem__ with pydantic
benedikt-bartscher Feb 29, 2024
80a25d6
add missing type annotation for initialColorMode
benedikt-bartscher Feb 29, 2024
8c7dd39
fix EventSpec args type annotation for pydantic v2
benedikt-bartscher Feb 29, 2024
6d3809e
ModelField doesn't exist in pydantic v2
benedikt-bartscher Feb 29, 2024
1c89f01
optionalize some Component props
benedikt-bartscher Feb 29, 2024
fd5a5b9
Bare.contents should be a Var
benedikt-bartscher Feb 29, 2024
d0f15c5
optionalize all Var props without defaults
benedikt-bartscher Feb 29, 2024
78db90d
forgot to migrate one type_ to annotation
benedikt-bartscher Feb 29, 2024
211f9f3
fix auto-var conversion
benedikt-bartscher Feb 29, 2024
7789118
prevent calling __bool__ for is_hydrated
benedikt-bartscher Feb 29, 2024
4380cd2
fix pydantic _get_value for MutableProxy
benedikt-bartscher Feb 29, 2024
80f0275
add some missing type hints to test state vars
benedikt-bartscher Feb 29, 2024
37c360e
fix init_subclass for pydantic v2, add some missing type hints, forma…
benedikt-bartscher Mar 1, 2024
45cb36f
pydantic copy is deprecated in favor of model_copy
benedikt-bartscher Mar 1, 2024
b65c34b
update_forward_refs -> model_rebuild
benedikt-bartscher Mar 1, 2024
b565e89
dict -> model_dump and field_info fixes
benedikt-bartscher Mar 1, 2024
b359353
fix VarData json loads
benedikt-bartscher Mar 1, 2024
0240541
migrate pydantic config classes to ConfigDict
benedikt-bartscher Mar 1, 2024
db3c791
add hint for VarData deserialization
benedikt-bartscher Mar 1, 2024
32bc17b
minor IconButton default value fix
benedikt-bartscher Mar 1, 2024
99c28f8
add proper pydantic v2 FieldInfo annotation
benedikt-bartscher Mar 1, 2024
7534b2c
my regex was not smart enough
benedikt-bartscher Mar 1, 2024
935286b
add Component.tag str type annotation
benedikt-bartscher Mar 1, 2024
f44d0ed
add Component.is_default bool type annotation
benedikt-bartscher Mar 1, 2024
16b7bae
add Component.library str type annotation
benedikt-bartscher Mar 1, 2024
bcaab30
add Component.alias str type annotation
benedikt-bartscher Mar 1, 2024
16b838d
format component.library black
benedikt-bartscher Mar 1, 2024
3c30ceb
new regex missed f-strings
benedikt-bartscher Mar 1, 2024
bcd29a1
use typing.get_args instead of __args__
benedikt-bartscher Mar 2, 2024
7ac184f
rebuild pydantic model after dropping id field
benedikt-bartscher Mar 2, 2024
30df32b
add default value for new lang prop in Html
benedikt-bartscher Mar 5, 2024
be0a77a
pydantic_init_subclass
benedikt-bartscher Mar 5, 2024
16a7c1f
fix bug in memoization mode copy
benedikt-bartscher Mar 5, 2024
6d96a94
fix chakra list, allow Tag.tag to be None in validation
benedikt-bartscher Mar 5, 2024
cdc2f4f
cleanup unneeded init_subclass super calls
benedikt-bartscher Mar 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate remaining __fields__ to model_fields
benedikt-bartscher committed Mar 5, 2024
commit 67c81489cf412de9b204d16f77e24d12f984634a
2 changes: 1 addition & 1 deletion reflex/compiler/utils.py
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@ def _compile_client_storage_recursive(
cookies = {}
local_storage = {}
state_name = state.get_full_name()
for name, field in state.__fields__.items():
for name, field in state.model_fields.items():
if name in state.inherited_vars:
# only include vars defined in this state
continue
2 changes: 1 addition & 1 deletion reflex/components/chakra/base.py
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ def create(cls) -> Component:

def _get_imports(self) -> imports.ImportDict:
_imports = super()._get_imports()
_imports.setdefault(self.__fields__["library"].default, []).append(
_imports.setdefault(self.model_fields["library"].default, []).append(
imports.ImportVar(tag="extendTheme", is_default=False),
)
_imports.setdefault("/utils/theme.js", []).append(
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/base.py
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ def create(
"""
component = super().create(*children, **props)
if component.library is None:
component.library = RadixThemesComponent.__fields__["library"].default
component.library = RadixThemesComponent.model_fields["library"].default
component.alias = "RadixThemes" + (
component.tag or component.__class__.__name__
)
8 changes: 4 additions & 4 deletions reflex/model.py
Original file line number Diff line number Diff line change
@@ -61,12 +61,12 @@ def __init_subclass__(cls):
"""Drop the default primary key field if any primary key field is defined."""
non_default_primary_key_fields = [
field_name
for field_name, field in cls.__fields__.items()
for field_name, field in cls.model_fields.items()
if field_name != "id"
and getattr(field.field_info, "primary_key", None) is True
]
if non_default_primary_key_fields:
cls.__fields__.pop("id", None)
cls.model_fields.pop("id", None)

super().__init_subclass__()

@@ -95,9 +95,9 @@ def dict(self, **kwargs):
Returns:
The object as a dictionary.
"""
base_fields = {name: getattr(self, name) for name in self.__fields__}
base_fields = {name: getattr(self, name) for name in self.model_fields}
relationships = {}
# SQLModel relationships do not appear in __fields__, but should be included if present.
# SQLModel relationships do not appear in model_fields, but should be included if present.
for name in self.__sqlmodel_relationships__:
try:
relationships[name] = self._dict_recursive(getattr(self, name))
2 changes: 1 addition & 1 deletion reflex/state.py
Original file line number Diff line number Diff line change
@@ -760,7 +760,7 @@ def add_var(cls, name: str, type_: Any, default_value: Any = None):
Raises:
NameError: if a variable of this name already exists
"""
if name in cls.__fields__:
if name in cls.model_fields:
raise NameError(
f"The variable '{name}' already exist. Use a different name"
)
2 changes: 1 addition & 1 deletion scripts/pyi_generator.py
Original file line number Diff line number Diff line change
@@ -373,7 +373,7 @@ def _extract_class_props_as_ast_nodes(
# with the annotation in some cases.
with contextlib.suppress(AttributeError, KeyError):
# Try to get default from pydantic field definition.
default = target_class.__fields__[name].default
default = target_class.model_fields[name].default
if isinstance(default, Var):
default = default._decode() # type: ignore

4 changes: 2 additions & 2 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ def test_default_primary_key(model_default_primary):
Args:
model_default_primary: Fixture.
"""
assert "id" in model_default_primary.__class__.__fields__
assert "id" in model_default_primary.__class__.model_fields


def test_custom_primary_key(model_custom_primary):
@@ -54,7 +54,7 @@ def test_custom_primary_key(model_custom_primary):
Args:
model_custom_primary: Fixture.
"""
assert "id" not in model_custom_primary.__class__.__fields__
assert "id" not in model_custom_primary.__class__.model_fields


@pytest.mark.filterwarnings(