Skip to content

Commit

Permalink
Leave preexisting finders in sys.meta_path alone
Browse files Browse the repository at this point in the history
Add new finder to sys.meta_path in deprecated_submodule that
knows of module rename instead of wrapping all meta_path finders.
Use existing finders to get specs for the new module.

This fixes quantumlib#4729.
  • Loading branch information
pavoljuhas committed Dec 21, 2021
1 parent df3502d commit 9090e79
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
37 changes: 7 additions & 30 deletions cirq-core/cirq/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@ def find_spec(self, fullname: str, path: Any = None, target: Any = None) -> Any:
to the wrapped finder.
"""
if fullname != self.old_module_name and not fullname.startswith(self.old_module_name + "."):
# if we are not interested in it, then just pass through to the wrapped finder
return self.finder.find_spec(fullname, path, target)
return None

if self.broken_module_exception is not None:
raise self.broken_module_exception
Expand All @@ -553,33 +552,8 @@ def find_spec(self, fullname: str, path: Any = None, target: Any = None) -> Any:

new_fullname = self.new_module_name + fullname[len(self.old_module_name) :]

# find the corresponding spec in the new structure
if fullname == self.old_module_name:
# this is the first time the deprecated module is being found
# which means that the new parent needs to be found first and under
# the new parent's path, we should be able to find the new name of
# the deprecated module
# this code is heavily inspired by importlib.util.find_spec
parent_name = new_fullname.rpartition('.')[0]
if parent_name:
parent = __import__(parent_name, fromlist=['__path__'])
# note that compared to importlib.util.find_spec we don't handle
# AttributeError here because it is not expected to happen in case
# of a DeprecatedModuleLoader - the new parent should exist and be
# a proper package
parent_path = parent.__path__
else:
parent_path = None
spec = self.finder.find_spec(new_fullname, parent_path, None)
else:
# we are finding a submodule of the parent of the deprecated module,
# which means that the parent was already found, and thus, `path` is
# correctly pointing to the module's parent in the new hierarchy
spec = self.finder.find_spec(
new_fullname,
path=path,
target=target,
)
# use normal import mechanism for the new module specs
spec = importlib.util.find_spec(new_fullname)

# if the spec exists, return the DeprecatedModuleLoader that will do the loading as well
# as set the alias(es) in sys.modules as necessary
Expand Down Expand Up @@ -676,7 +650,10 @@ def wrap(finder: Any) -> Any:
finder, new_module_name, old_module_name, deadline, broken_module_exception
)

sys.meta_path = [wrap(finder) for finder in sys.meta_path]
finder = DeprecatedModuleFinder(
None, new_module_name, old_module_name, deadline, broken_module_exception
)
sys.meta_path.append(finder)


def _setup_deprecated_submodule_attribute(
Expand Down
1 change: 0 additions & 1 deletion cirq-core/cirq/_compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ def _test_metadata_search_path_inner():
assert m.metadata('flynt')


@pytest.mark.xfail(reason='bug in deprecated_submodule')
def test_metadata_distributions_after_deprecated_submodule():
subprocess_context(_test_metadata_distributions_after_deprecated_submodule)()

Expand Down

0 comments on commit 9090e79

Please sign in to comment.