Skip to content

Commit

Permalink
Check pydantic correct installation (#1829)
Browse files Browse the repository at this point in the history
* Check pydantic correct installation

* typos
  • Loading branch information
Wauplin committed Nov 16, 2023
1 parent 8840a8e commit 6b0e015
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/huggingface_hub/utils/_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import importlib.metadata
import platform
import sys
import warnings
from typing import Any, Dict

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

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


def get_pydantic_version() -> str:
Expand Down

0 comments on commit 6b0e015

Please sign in to comment.