소스 검색

[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:
         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