Skip to content

Commit

Permalink
Fix a bug in module deprecator (#3937)
Browse files Browse the repository at this point in the history
Adds `__dict__` to the wrapped module to ensure its member attributes stay on it.
  • Loading branch information
balopat committed Mar 19, 2021
1 parent 0baadeb commit 0d37128
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cirq/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ def _setup_deprecated_submodule_attribute(
setattr(parent_module, old_child, new_module)

class Wrapped(ModuleType):
__dict__ = parent_module.__dict__

def __getattr__(self, name):
if name == old_child:
_deduped_module_warn_or_error(
Expand Down
13 changes: 13 additions & 0 deletions cirq/_compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,3 +708,16 @@ def test_subprocess_test_failure():

def _test_subprocess_test_failure_inner():
raise ValueError('this fails')


def test_dir_is_still_valid():
subprocess_context(_dir_is_still_valid_inner)()


def _dir_is_still_valid_inner():
"""to ensure that create_attribute=True keeps the dir(module) intact"""

import cirq.testing._compat_test_data as mod

for m in ['fake_a', 'info', 'module_a', 'sys']:
assert m in dir(mod)

0 comments on commit 0d37128

Please sign in to comment.