From ad9ecf279929bc13f989ebc3b0e6ed8f448c71ac Mon Sep 17 00:00:00 2001 From: Peiyuan Liu Date: Wed, 26 Apr 2023 15:27:40 +0000 Subject: [PATCH 1/2] add isinstance check and unittest --- python/oneflow/mock_torch/__init__.py | 18 ++++++++++++++++++ python/oneflow/test/misc/test_mock_scope.py | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/python/oneflow/mock_torch/__init__.py b/python/oneflow/mock_torch/__init__.py index c7d23af95bc..40c5c853b0e 100644 --- a/python/oneflow/mock_torch/__init__.py +++ b/python/oneflow/mock_torch/__init__.py @@ -248,6 +248,24 @@ def __bool__(self): ) return False + def __enter__(self): + if _importer.verbose: + print( + f'"{self.__name__}" is a dummy object, and does not support __enter__.' + ) + + def __exit__(self, exception_type, exception_value, traceback): + if _importer.verbose: + print( + f'"{self.__name__}" is a dummy object, and does not support __exit__.' + ) + + def __subclasscheck__(self, subclass): + return False + + def __instancecheck__(self, instance): + return False + class enable: def __init__( diff --git a/python/oneflow/test/misc/test_mock_scope.py b/python/oneflow/test/misc/test_mock_scope.py index 0a4c66a64b9..b8f1afb7d83 100644 --- a/python/oneflow/test/misc/test_mock_scope.py +++ b/python/oneflow/test/misc/test_mock_scope.py @@ -180,6 +180,12 @@ def test_hazard_list(test_case): test_case.assertTrue("safetensors._safetensors_rust" in sys.modules) import safetensors + def test_isinstance(test_case): + with mock.enable(lazy=True): + import torch + + test_case.assertFalse(isinstance(int, torch._six.string_class)) + # MUST use pytest to run this test def test_verbose(capsys): From 55870e809a37e70b970fbb2eb39cb4282b0ebab5 Mon Sep 17 00:00:00 2001 From: Peiyuan Liu Date: Thu, 27 Apr 2023 01:52:45 +0000 Subject: [PATCH 2/2] update --- python/oneflow/mock_torch/__init__.py | 14 ++++++-------- python/oneflow/test/misc/test_mock_scope.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/python/oneflow/mock_torch/__init__.py b/python/oneflow/mock_torch/__init__.py index 40c5c853b0e..a3ae2a048c0 100644 --- a/python/oneflow/mock_torch/__init__.py +++ b/python/oneflow/mock_torch/__init__.py @@ -249,16 +249,14 @@ def __bool__(self): return False def __enter__(self): - if _importer.verbose: - print( - f'"{self.__name__}" is a dummy object, and does not support __enter__.' - ) + raise RuntimeError( + f'"{self.__name__}" is a dummy object, and does not support "with" statement.' + ) def __exit__(self, exception_type, exception_value, traceback): - if _importer.verbose: - print( - f'"{self.__name__}" is a dummy object, and does not support __exit__.' - ) + raise RuntimeError( + f'"{self.__name__}" is a dummy object, and does not support "with" statement.' + ) def __subclasscheck__(self, subclass): return False diff --git a/python/oneflow/test/misc/test_mock_scope.py b/python/oneflow/test/misc/test_mock_scope.py index b8f1afb7d83..774be056bd6 100644 --- a/python/oneflow/test/misc/test_mock_scope.py +++ b/python/oneflow/test/misc/test_mock_scope.py @@ -186,6 +186,18 @@ def test_isinstance(test_case): test_case.assertFalse(isinstance(int, torch._six.string_class)) + def test_with_statement(test_case): + with mock.enable(lazy=True): + with test_case.assertRaises(RuntimeError) as context: + import torch.noexist + + with torch.noexist: + pass + test_case.assertTrue( + '"oneflow.noexist" is a dummy object, and does not support "with" statement.' + in str(context.exception) + ) + # MUST use pytest to run this test def test_verbose(capsys):