From 487343346e222632f6597863cdf1ff84aad7b762 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Mon, 16 Dec 2024 23:21:27 +0000 Subject: [PATCH] Prevent users from seeing hardcoded print stmt when hypothesis is not installed (#142398) Fixes: #142357 Pull Request resolved: https://github.com/pytorch/pytorch/pull/142398 Approved by: https://github.com/zou3519 --- torch/_dynamo/variables/user_defined.py | 10 ++-------- torch/testing/_internal/common_utils.py | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) 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.