From 1cfc8115065b6bac71725f480999304f1b031f6d Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Wed, 10 Jul 2024 12:28:39 -0700 Subject: [PATCH] [REF-3266] Check for pydantic v1 outside of try/except (#3643) If pydantic v1 is already installed, there is no reason to restore the original pydantic modules, which seems to introduce subtle incompatibilities with some pydantic versions. Perform the pydantic version check early on and exit for v1 before doing anything with the sys.modules. Fix #3642 --- reflex/utils/compat.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/reflex/utils/compat.py b/reflex/utils/compat.py index 7d2a03a11..ef5fcd3e1 100644 --- a/reflex/utils/compat.py +++ b/reflex/utils/compat.py @@ -32,6 +32,13 @@ def pydantic_v1_patch(): Yields: None when the Pydantic module is patched. """ + import pydantic + + if pydantic.__version__.startswith("1."): + # pydantic v1 is already installed + yield + return + patched_modules = [ "pydantic", "pydantic.fields", @@ -42,11 +49,6 @@ def pydantic_v1_patch(): try: import pydantic.v1 # type: ignore - if pydantic.__version__.startswith("1."): - # pydantic v1 is already installed - yield - return - sys.modules["pydantic.fields"] = pydantic.v1.fields # type: ignore sys.modules["pydantic.main"] = pydantic.v1.main # type: ignore sys.modules["pydantic.errors"] = pydantic.v1.errors # type: ignore