Skip to content

Fix module aliases breaking unittest via __warningregistry__ probes - #48969

Open
tanvir-ux wants to merge 1 commit into
huggingface:mainfrom
tanvir-ux:fix-getattr-warningregistry
Open

tanvir-ux wants to merge 1 commit into
huggingface:mainfrom
tanvir-ux:fix-getattr-warningregistry

Conversation

@tanvir-ux

@tanvir-ux tanvir-ux commented Sep 20, 2026

Copy link
Copy Markdown

CPU CI GPU run-slow

What does this PR do?

Fixes #48966.

I hit this while running unrelated unit tests after importing transformers: unittest.TestCase.assertWarns / assertWarnsRegex started raising ModuleNotFoundError: No module named 'torchvision' even when the test never touched image processors.

assertWarns walks every module in sys.modules and probes __warningregistry__. Our legacy *_fast image-processor alias modules implement __getattr__ by forwarding into importlib.import_module(...). That probe therefore imported torchvision-backed processors (e.g. Aria) and failed in environments without torchvision.

I taught both alias __getattr__ paths to raise AttributeError for dunder names without importing the target — the same idea as the existing explicit module.__file__ = None guard for inspect. I also added a regression test that exercises assertWarnsRegex after import and checks the Aria fast alias returns None for __warningregistry__.

Before / after

import unittest
import transformers  # torchvision not installed

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

# before: ModuleNotFoundError: No module named 'torchvision'
# after:  AssertionError: DeprecationWarning not triggered

Testing

  • Reproduced the failure on current main, applied the patch, and confirmed the snippet above now yields the expected AssertionError.
  • Ran tests/utils/test_import_utils.py::test_module_aliases_ignore_dunder_attribute_probes locally — passed.

Alias modules for image processors forwarded every attribute lookup —
including stdlib dunder probes such as __warningregistry__ — into
importlib.import_module. unittest.assertWarns walks sys.modules and
clears that attribute, which imported torchvision-backed processors and
raised ModuleNotFoundError when torchvision was absent.

Skip dunder names in the alias __getattr__ paths so probes raise
AttributeError without importing the target. Adds a regression test for
issue huggingface#48966.
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 35541237239:2
Result: success | Jobs: 16 | Tests: 175,687 | Failures: 0 | Duration: 16h 40m

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module-level getattr breaks unittest functionality

1 participant