From e6d7445da3136afd9ff0b9a83fad220b6bf1cb44 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Fri, 30 Aug 2024 08:01:58 -0600 Subject: [PATCH] Standardize use of in_testing_environment(), building_documentation() --- doc/OnlineDocs/conf.py | 3 +++ pyomo/common/dependencies.py | 9 +++++---- pyomo/common/deprecation.py | 2 +- pyomo/common/log.py | 12 +++++------- pyomo/contrib/simplemodel/__init__.py | 3 ++- pyomo/pysp/__init__.py | 3 ++- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/doc/OnlineDocs/conf.py b/doc/OnlineDocs/conf.py index 467bd21f8b6..4d784d0fbb8 100644 --- a/doc/OnlineDocs/conf.py +++ b/doc/OnlineDocs/conf.py @@ -301,4 +301,7 @@ def check_output(self, want, got, optionflags): asl_available = False ma27_available = False mumps_available = False + +from pyomo.common.flags import in_testing_environment +in_testing_environment(True) ''' diff --git a/pyomo/common/dependencies.py b/pyomo/common/dependencies.py index e292cd2077d..0458bdeade6 100644 --- a/pyomo/common/dependencies.py +++ b/pyomo/common/dependencies.py @@ -19,9 +19,10 @@ from types import ModuleType from typing import List -from .deprecation import deprecated, deprecation_warning, in_testing_environment +import pyomo +from .deprecation import deprecated, deprecation_warning from .errors import DeferredImportError - +from .flags import in_testing_environment, building_documentation SUPPRESS_DEPENDENCY_WARNINGS = False @@ -240,12 +241,12 @@ def UnavailableClass(unavailable_module): class UnavailableMeta(type): def __getattr__(cls, name): - if 'sphinx' in sys.modules: + if building_documentation(): # If we are building documentation, avoid the # DeferredImportError (we will still raise one if # someone attempts to *create* an instance of this # class) - super().__getattr__(name) + return getattr(super(), name) raise DeferredImportError( unavailable_module._moduleunavailable_message( f"The class attribute '{cls.__name__}.{name}' is not available " diff --git a/pyomo/common/deprecation.py b/pyomo/common/deprecation.py index f8a63752c13..1e5dc960fb8 100644 --- a/pyomo/common/deprecation.py +++ b/pyomo/common/deprecation.py @@ -228,7 +228,7 @@ def deprecation_warning( logger.warning(msg) -if in_testing_environment(): +if in_testing_environment() or building_documentation(): deprecation_warning.emitted_warnings = None else: deprecation_warning.emitted_warnings = set() diff --git a/pyomo/common/log.py b/pyomo/common/log.py index d61ed62f373..3bf0a70c072 100644 --- a/pyomo/common/log.py +++ b/pyomo/common/log.py @@ -28,21 +28,19 @@ from pyomo.version.info import releaselevel from pyomo.common.deprecation import deprecated from pyomo.common.fileutils import PYOMO_ROOT_DIR +from pyomo.common.flags import in_testing_environment from pyomo.common.formatting import wrap_reStructuredText _indentation_re = re.compile(r'\s*') -_RTD_URL = "https://pyomo.readthedocs.io/en/%s/errors.html" % ( - 'stable' - if (releaselevel == 'final' or 'sphinx' in sys.modules or 'Sphinx' in sys.modules) - else 'latest' -) - def RTD(_id): _id = str(_id).lower() assert _id[0] in 'wex' - return f"{_RTD_URL}#{_id}" + return "https://pyomo.readthedocs.io/en/%s/errors.html#%s" % ( + 'stable' if (releaselevel == 'final' or in_testing_environment()) else 'latest', + _id, + ) _DEBUG = logging.DEBUG diff --git a/pyomo/contrib/simplemodel/__init__.py b/pyomo/contrib/simplemodel/__init__.py index f2f4922223e..201c1a0f657 100644 --- a/pyomo/contrib/simplemodel/__init__.py +++ b/pyomo/contrib/simplemodel/__init__.py @@ -9,7 +9,8 @@ # This software is distributed under the 3-clause BSD License. # ___________________________________________________________________________ -from pyomo.common.deprecation import deprecation_warning, in_testing_environment +from pyomo.common.deprecation import deprecation_warning +from pyomo.common.flags import in_testing_environment try: deprecation_warning( diff --git a/pyomo/pysp/__init__.py b/pyomo/pysp/__init__.py index bb8a401e45e..dae9a04943e 100644 --- a/pyomo/pysp/__init__.py +++ b/pyomo/pysp/__init__.py @@ -11,7 +11,8 @@ import logging import sys -from pyomo.common.deprecation import deprecation_warning, in_testing_environment +from pyomo.common.deprecation import deprecation_warning +from pyomo.common.flags import in_testing_environment try: # Warn the user