Skip to content

Commit ef846d7

Browse files
authored
Check pydantic correct installation (#1829)
* Check pydantic correct installation * typos
1 parent 975b931 commit ef846d7

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/huggingface_hub/utils/_runtime.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import importlib.metadata
1717
import platform
1818
import sys
19+
import warnings
1920
from typing import Any, Dict
2021

2122
from .. import __version__, constants
@@ -165,7 +166,27 @@ def get_pillow_version() -> str:
165166

166167
# Pydantic
167168
def is_pydantic_available() -> bool:
168-
return _is_available("pydantic")
169+
if not _is_available("pydantic"):
170+
return False
171+
# For Pydantic, we add an extra check to test whether it is correctly installed or not. If both pydantic 2.x and
172+
# typing_extensions<=4.5.0 are installed, then pydantic will fail at import time. This should not happen when
173+
# it is installed with `pip install huggingface_hub[inference]` but it can happen when it is installed manually
174+
# by the user in an environment that we don't control.
175+
#
176+
# Usually we won't need to do this kind of check on optional dependencies. However, pydantic is a special case
177+
# as it is automatically imported when doing `from huggingface_hub import ...` even if the user doesn't use it.
178+
#
179+
# See https://github.com/huggingface/huggingface_hub/pull/1829 for more details.
180+
try:
181+
from pydantic import validator # noqa: F401
182+
except ImportError:
183+
# Example: "ImportError: cannot import name 'TypeAliasType' from 'typing_extensions'"
184+
warnings.warn(
185+
"Pydantic is installed but cannot be imported. Please check your installation. `huggingface_hub` will "
186+
"default to not using Pydantic. Error message: '{e}'"
187+
)
188+
return False
189+
return True
169190

170191

171192
def get_pydantic_version() -> str:

0 commit comments

Comments
 (0)