From b6a65ae52a6df67180be8f1e162a3d1829a51bc0 Mon Sep 17 00:00:00 2001 From: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Date: Tue, 7 Jun 2022 08:00:36 -0400 Subject: [PATCH] Fix circular import in onnx.utils (#17577) * Fix circular import in onnx.utils * Add comment for test fetcher * Here too * Style --- src/transformers/onnx/utils.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/transformers/onnx/utils.py b/src/transformers/onnx/utils.py index 83b18c6ab5..9672b0a96a 100644 --- a/src/transformers/onnx/utils.py +++ b/src/transformers/onnx/utils.py @@ -14,9 +14,11 @@ from ctypes import c_float, sizeof from enum import Enum -from typing import Optional, Union +from typing import TYPE_CHECKING, Optional, Union -from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer + +if TYPE_CHECKING: + from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer # tests_ignore class ParameterFormat(Enum): @@ -66,7 +68,7 @@ def compute_serialized_parameters_size(num_parameters: int, dtype: ParameterForm return num_parameters * dtype.size -def get_preprocessor(model_name: str) -> Optional[Union[AutoTokenizer, AutoFeatureExtractor, AutoProcessor]]: +def get_preprocessor(model_name: str) -> Optional[Union["AutoTokenizer", "AutoFeatureExtractor", "AutoProcessor"]]: """ Gets a preprocessor (tokenizer, feature extractor or processor) that is available for `model_name`. @@ -79,6 +81,9 @@ def get_preprocessor(model_name: str) -> Optional[Union[AutoTokenizer, AutoFeatu returned. If both a tokenizer and a feature extractor exist, an error is raised. The function returns `None` if no preprocessor is found. """ + # Avoid circular imports by only importing this here. + from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer # tests_ignore + try: return AutoProcessor.from_pretrained(model_name) except (ValueError, OSError, KeyError):