浏览代码

[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
Masen Furer 10 月之前
父节点
当前提交
1cfc811506
共有 1 个文件被更改,包括 7 次插入5 次删除
  1. 7 5
      reflex/utils/compat.py

+ 7 - 5
reflex/utils/compat.py

@@ -32,6 +32,13 @@ def pydantic_v1_patch():
     Yields:
     Yields:
         None when the Pydantic module is patched.
         None when the Pydantic module is patched.
     """
     """
+    import pydantic
+
+    if pydantic.__version__.startswith("1."):
+        # pydantic v1 is already installed
+        yield
+        return
+
     patched_modules = [
     patched_modules = [
         "pydantic",
         "pydantic",
         "pydantic.fields",
         "pydantic.fields",
@@ -42,11 +49,6 @@ def pydantic_v1_patch():
     try:
     try:
         import pydantic.v1  # type: ignore
         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.fields"] = pydantic.v1.fields  # type: ignore
         sys.modules["pydantic.main"] = pydantic.v1.main  # type: ignore
         sys.modules["pydantic.main"] = pydantic.v1.main  # type: ignore
         sys.modules["pydantic.errors"] = pydantic.v1.errors  # type: ignore
         sys.modules["pydantic.errors"] = pydantic.v1.errors  # type: ignore