Skip to content

Module-level getattr breaks unittest functionality #48966

Description

@chschroeder

System Info

Certain functions from the unittest package are broken if transformers is loaded:

ModuleNotFoundError: No module named 'torchvision'

For me, the error occurs because torchvision is not installed. The error, however, is a more systematic one. From the stack trace, I ended up here:

def getattr_factory(target):
def _getattr(name):
if name.endswith("Fast"):
new_name = name.removesuffix("Fast")
logger.warning_once(
"Accessing `%s` from `%s`. Returning `%s` instead. Behavior may be "
"different and this alias will be removed in future versions.",
name,
target,
new_name,
)
return getattr(importlib.import_module(target, __name__), new_name)
# Silently forward non-Fast names to target (transparent alias behavior)
return getattr(importlib.import_module(target, __name__), name)
return _getattr
sys.modules[f"{__name__}.models.{_model}.{_module}_fast"].__getattr__ = getattr_factory(_target)

This later interferes with unittest. See here for the example of assertWarnsRegex():

https://github.com/python/cpython/blob/c016c2535b74227fddf2cf7334dbfead6c930214/Lib/unittest/case.py#L289-L298


Environment:
Python 3.12
transformers 5.17.0
torchvision not installed

**Full stacktrace**
  Traceback (most recent call last):
  File "", line 16, in 
  File "", line 10, in test
  File "/user/.pyenv/versions/3.12.8/lib/python3.12/unittest/case.py", line 293, in __enter__
    if getattr(v, '__warningregistry__', None):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/user/workspaces/small-text/.nox/8e14c2bf/lib/python3.12/site-packages/transformers/__init__.py", line 867, in _getattr
    return getattr(importlib.import_module(target, __name__), name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/user/.pyenv/versions/3.12.8/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "", line 1387, in _gcd_import
  File "", line 1360, in _find_and_load
  File "", line 1331, in _find_and_load_unlocked
  File "", line 935, in _load_unlocked
  File "", line 999, in exec_module
  File "", line 488, in _call_with_frames_removed
  File "/user/workspaces/small-text/.nox/8e14c2bf/lib/python3.12/site-packages/transformers/models/aria/image_processing_aria.py", line 23, in 
    from torchvision.transforms.v2 import functional as tvF

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

import unittest
import transformers  # uncomment to fix the reported error


class TestWarning(unittest.TestCase):
    def test_warns_regex(self):
        with self.assertWarnsRegex(DeprecationWarning, r"hello"):
            pass


if __name__ == "__main__":
    unittest.main()

Expected behavior

The test should output: AssertionError: DeprecationWarning not triggered (or respectively pass with a different testing logic, but it should not fail with an unrelated import).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions