diff --git a/torch/_dynamo/variables/user_defined.py b/torch/_dynamo/variables/user_defined.py index f88b9db8d18c..18340712b5ce 100644 --- a/torch/_dynamo/variables/user_defined.py +++ b/torch/_dynamo/variables/user_defined.py @@ -91,14 +91,8 @@ def is_forbidden_context_manager(ctx): except ImportError: pass - try: - from torch.testing._internal.jit_utils import ( - _AssertRaisesRegexWithHighlightContext, - ) - - f_ctxs.append(_AssertRaisesRegexWithHighlightContext) - except ImportError: - pass + if m := sys.modules.get("torch.testing._internal.jit_utils"): + f_ctxs.append(m._AssertRaisesRegexWithHighlightContext) return ctx in f_ctxs diff --git a/torch/testing/_internal/common_utils.py b/torch/testing/_internal/common_utils.py index 20a54acb7d5a..1bb4b1ac8ee2 100644 --- a/torch/testing/_internal/common_utils.py +++ b/torch/testing/_internal/common_utils.py @@ -2570,7 +2570,7 @@ try: "pytorch_ci" if IS_CI else os.getenv('PYTORCH_HYPOTHESIS_PROFILE', 'dev') ) except ImportError: - print('Fail to import hypothesis in common_utils, tests are not derandomized') + warnings.warn('Fail to import hypothesis in common_utils, tests are not derandomized', ImportWarning) # Used in check_if_enable to see if a test method should be disabled by an issue, # sanitizes a test method name from appended suffixes by @dtypes parametrization.