Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mock behaviour #10207

Merged
merged 8 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions python/oneflow/mock_torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ def __bool__(self):
)
return False

def __enter__(self):
raise RuntimeError(
f'"{self.__name__}" is a dummy object, and does not support "with" statement.'
)

def __exit__(self, exception_type, exception_value, traceback):
raise RuntimeError(
f'"{self.__name__}" is a dummy object, and does not support "with" statement.'
)

def __subclasscheck__(self, subclass):
return False

def __instancecheck__(self, instance):
return False


class enable:
def __init__(
Expand Down
18 changes: 18 additions & 0 deletions python/oneflow/test/misc/test_mock_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ 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))

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):
Expand Down