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

Add env variable and user config option to suppress packaging warnings #3362

Merged
merged 12 commits into from
Mar 31, 2020
10 changes: 10 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ stages:
matrix:
Python37:
python.version: '3.7'
variables:
QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y
steps:
- task: UsePythonVersion@0
inputs:
Expand Down Expand Up @@ -235,6 +237,8 @@ stages:
matrix:
Python37:
python.version: '3.7'
variables:
QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y
steps:
- task: UsePythonVersion@0
inputs:
Expand Down Expand Up @@ -295,6 +299,8 @@ stages:
python.version: '3.7'
Python38:
python.version: '3.8'
variables:
QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y
steps:
- task: UsePythonVersion@0
inputs:
Expand Down Expand Up @@ -350,6 +356,8 @@ stages:
python.version: '3.5'
Python38:
python.version: '3.8'
variables:
QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y
steps:
- task: UsePythonVersion@0
inputs:
Expand Down Expand Up @@ -406,6 +414,8 @@ stages:
python.version: '3.5'
Python38:
python.version: '3.8'
variables:
QISKIT_SUPPRESS_PACKAGING_WARNINGS: Y
steps:
- task: UsePythonVersion@0
inputs:
Expand Down
36 changes: 24 additions & 12 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

# pylint: disable=wrong-import-order
# pylint: disable=wrong-import-order,invalid-name,wrong-import-position


"""Main Qiskit public functionality."""

import pkgutil
import sys
import warnings
import os

# First, check for required Python and API version
from . import util
Expand All @@ -32,6 +33,9 @@
from qiskit.circuit import QuantumRegister
from qiskit.circuit import QuantumCircuit

# user config
from qiskit import user_config as _user_config

# The qiskit.extensions.x imports needs to be placed here due to the
# mechanism for adding gates dynamically.
import qiskit.extensions
Expand All @@ -46,29 +50,37 @@
# Please note these are global instances, not modules.
from qiskit.providers.basicaer import BasicAer

_config = _user_config.get_config()

# Try to import the Aer provider if installed.
try:
from qiskit.providers.aer import Aer
except ImportError:
warnings.warn('Could not import the Aer provider from the qiskit-aer '
'package. Install qiskit-aer or check your installation.',
RuntimeWarning)
suppress_warnings = os.environ.get('QISKIT_SUPPRESS_PACKAGING_WARNINGS', '')
if suppress_warnings.upper() != 'Y':
if not _config.get('suppress_packaging_warnings') or suppress_warnings.upper() == 'N':
warnings.warn('Could not import the Aer provider from the qiskit-aer '
'package. Install qiskit-aer or check your installation.',
RuntimeWarning)
# Try to import the IBMQ provider if installed.
try:
from qiskit.providers.ibmq import IBMQ
except ImportError:
warnings.warn('Could not import the IBMQ provider from the '
'qiskit-ibmq-provider package. Install qiskit-ibmq-provider '
'or check your installation.',
RuntimeWarning)
suppress_warnings = os.environ.get('QISKIT_SUPPRESS_PACKAGING_WARNINGS', '')
if suppress_warnings.upper() != 'Y':
if not _config.get('suppress_packaging_warnings') or suppress_warnings.upper() == 'N':
warnings.warn('Could not import the IBMQ provider from the '
'qiskit-ibmq-provider package. Install '
'qiskit-ibmq-provider or check your installation.',
RuntimeWarning)

# Moved to after IBMQ and Aer imports due to import issues
# with other modules that check for IBMQ (tools)
from qiskit.execute import execute
from qiskit.compiler import transpile, assemble, schedule
from qiskit.execute import execute # noqa
from qiskit.compiler import transpile, assemble, schedule # noqa

from .version import __version__
from .version import _get_qiskit_versions
from .version import __version__ # noqa
from .version import _get_qiskit_versions # noqa


if sys.version_info[0] == 3 and sys.version_info[1] == 5:
Expand Down
5 changes: 5 additions & 0 deletions qiskit/user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def read_config_file(self):
"0, 1, 2, or 3.")
self.settings['transpile_optimization_level'] = (
transpile_optimization_level)
# Parse package warnings
package_warnings = self.config_parser.getboolean(
'default', 'suppress_packaging_warnings', fallback=False)
if package_warnings:
self.settings['suppress_packaging_warnings'] = package_warnings


def get_config():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
features:
- |
A new environment variable ``QISKIT_SUPPRESS_PACKAGING_WARNINGS`` can be
set to ``Y`` or ``y`` which will suppress the warnings about
``qiskit-aer`` and ``qiskit-ibmq-provider`` not being installed at import
time. This is useful for users who are only running qiskit-terra (or just
not qiskit-aer and/or qiskit-ibmq-provider) and the warnings are not an
indication of a potential packaging problem. You can set the environment
variable to ``N`` or ``n`` to ensure that warnings are always enabled
even if the user config file is set to disable them.
- |
A new user config file option, ``suppress_packaging_warnings`` has been
added. When set to ``true`` in your user config file like::

[default]
suppress_packaging_warnings = true

it will suppress the warnings about ``qiskit-aer`` and
``qiskit-ibmq-provider`` not being installed at import time. This is useful
for users who are only running qiskit-terra (or just not qiskit-aer and/or
qiskit-ibmq-provider) and the warnings are not an indication of a potential
packaging problem. If the user config file is set to disable the warnings
this can be overriden by setting the ``QISKIT_SUPPRESS_PACKAGING_WARNINGS``
to ``N`` or ``n``
45 changes: 44 additions & 1 deletion test/python/test_user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def test_empty_file_read(self):
config.read_config_file()
self.assertEqual({}, config.settings)

def test_invalid_suppress_packaging_warnings(self):
test_config = """
[default]
suppress_packaging_warnings = 76
"""
self.addCleanup(os.remove, self.file_path)
with open(self.file_path, 'w') as file:
file.write(test_config)
file.flush()
config = user_config.UserConfig(self.file_path)
self.assertRaises(ValueError, config.read_config_file)

def test_invalid_optimization_level(self):
test_config = """
[default]
Expand Down Expand Up @@ -88,12 +100,41 @@ def test_optimization_level_valid(self):
self.assertEqual({'transpile_optimization_level': 1},
config.settings)

def test_valid_suppress_packaging_warnings_false(self):
test_config = """
[default]
suppress_packaging_warnings = false
"""
self.addCleanup(os.remove, self.file_path)
with open(self.file_path, 'w') as file:
file.write(test_config)
file.flush()
config = user_config.UserConfig(self.file_path)
config.read_config_file()
self.assertEqual({},
config.settings)

def test_valid_suppress_packaging_warnings_true(self):
test_config = """
[default]
suppress_packaging_warnings = true
"""
self.addCleanup(os.remove, self.file_path)
with open(self.file_path, 'w') as file:
file.write(test_config)
file.flush()
config = user_config.UserConfig(self.file_path)
config.read_config_file()
self.assertEqual({'suppress_packaging_warnings': True},
config.settings)

def test_all_options_valid(self):
test_config = """
[default]
circuit_drawer = latex
circuit_mpl_style = default
transpile_optimization_level = 3
suppress_packaging_warnings = true
"""
self.addCleanup(os.remove, self.file_path)
with open(self.file_path, 'w') as file:
Expand All @@ -103,4 +144,6 @@ def test_all_options_valid(self):
config.read_config_file()
self.assertEqual({'circuit_drawer': 'latex',
'circuit_mpl_style': 'default',
'transpile_optimization_level': 3}, config.settings)
'transpile_optimization_level': 3,
'suppress_packaging_warnings': True},
config.settings)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ setenv =
LANGUAGE=en_US
LC_ALL=en_US.utf-8
ARGS="-V"
QISKIT_SUPRESS_PACKAGING_WARNINGS=Y
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/requirements-dev.txt
commands =
Expand Down