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

better error messaging when deprecated module can't be imported #4324

Merged

Conversation

balopat
Copy link
Contributor

@balopat balopat commented Jul 15, 2021

Adds better error messaging when deprecated module can't be imported.

Now, import cirq prints zero warnings, even if there are errors importing the modules.
Instead, when the deprecated modules are used, an informative error is thrown, including the root cause error:

When cirq_pasqal can't be imported, import cirq.pasqal results in:

Traceback (most recent call last):
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 590, in deprecated_submodule
    new_module = importlib.import_module(new_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cirq_pasqal'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 914, in _find_spec
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 504, in find_spec
    raise self.broken_module_exception
cirq._compat.DeprecatedModuleImportError: cirq_pasqal cannot be imported. The typical reasons are that
 1.) cirq_pasqal is not installed, or
 2.) when developing Cirq, you don't have your PYTHONPATH setup. In this case run `source dev_tools/pypath`.

 You can check the detailed exception above for more details or run `import cirq_pasqal to reproduce the issue.

Note that from cirq import pasqal will still run and return a dummy module that will throw the same error when trying to access any of its attributes.

Fixes #4106.
Fixes #4291.

@balopat balopat requested review from cduck, vtomole and a team as code owners July 15, 2021 02:18
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Jul 15, 2021
Copy link
Collaborator

@mpharrigan mpharrigan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure exactly how this works, but it looks well tested enough and is an improvement on what we have!

@@ -457,6 +457,7 @@ def __init__(
new_module_name: str,
old_module_name: str,
deadline: str,
broken_module_exception: Optional[BaseException],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type is optional but this argument isn't optional(?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just marked it as it could be None...that doesn't mean you don't have to mandatorily state whether it's None or not...is that weird? I can make it None by default.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not that weird

@balopat balopat added automerge Tells CirqBot to sync and merge this PR. (If it's running.) and removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) labels Jul 15, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 15, 2021
@CirqBot
Copy link
Collaborator

CirqBot commented Jul 15, 2021

Automerge cancelled: A required status check is not present.

Missing statuses: ['Typescript lint check', 'Typescript tests', 'Typescript tests coverage']

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jul 15, 2021
@mpharrigan mpharrigan added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 15, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 15, 2021
@CirqBot CirqBot merged commit 16dd1bb into quantumlib:master Jul 15, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jul 15, 2021
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…tumlib#4324)

Adds better error messaging when deprecated module can't be imported.

Now, `import cirq` prints zero warnings, even if there are errors importing the modules.
Instead, when the deprecated modules are used, an informative error is thrown, including the root cause error: 

When `cirq_pasqal` can't be imported, `import cirq.pasqal` results in: 

```
Traceback (most recent call last):
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 590, in deprecated_submodule
    new_module = importlib.import_module(new_module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cirq_pasqal'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 914, in _find_spec
  File "/Users/balintp/dev/proj/Cirq/cirq-core/cirq/_compat.py", line 504, in find_spec
    raise self.broken_module_exception
cirq._compat.DeprecatedModuleImportError: cirq_pasqal cannot be imported. The typical reasons are that
 1.) cirq_pasqal is not installed, or
 2.) when developing Cirq, you don't have your PYTHONPATH setup. In this case run `source dev_tools/pypath`.

 You can check the detailed exception above for more details or run `import cirq_pasqal to reproduce the issue.

```

Note that `from cirq import pasqal` will still run and return a dummy module that will throw the same error when trying to access any of its attributes.

Fixes quantumlib#4106.
Fixes quantumlib#4291.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
3 participants