File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed
src/huggingface_hub/utils Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change 16
16
import importlib .metadata
17
17
import platform
18
18
import sys
19
+ import warnings
19
20
from typing import Any , Dict
20
21
21
22
from .. import __version__ , constants
@@ -165,7 +166,27 @@ def get_pillow_version() -> str:
165
166
166
167
# Pydantic
167
168
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
169
190
170
191
171
192
def get_pydantic_version () -> str :
You can’t perform that action at this time.
0 commit comments