Files
edx-platform/import_shims/warn.py
Kyle McCormick 44d5060c60 Add setting for error'ing on deprecated imports
Deprecated edx-platform import paths
(for example, `student` instead of
`common.djangoapps.student`) currently raise
warnings when used. We want to fully remove
support for those paths.

As an easily reversible way to initially remove
support, we add a new setting to LMS and Studio
called `ERROR_ON_DEPRECATED_EDX_PLATFORM_IMPORTS`,
defaulting to False. We set it to True for devstack
and will set it to True in Stage and Production
soon. If critical errors occur, we can easily
flip the setting back to False.
2021-01-21 10:49:47 -05:00

70 lines
2.2 KiB
Python

"""
Utilities for warning about deprecated imports temporarily supported by
the import_shim/ system.
See /docs/decisions/0007-sys-path-modification-removal.rst for details.
"""
import warnings
from django.conf import settings
from edx_django_utils.monitoring import set_custom_attribute
class DeprecatedEdxPlatformImportWarning(DeprecationWarning):
"""
A warning that a module is being imported from an unsupported location.
Example use case:
edx-platform modules should be imported from the root of the repository.
For example, `from lms.djangoapps.course_wiki import views` is good.
However, we historically modify `sys.path` to allow importing relative to
certain subdirectories. For example, `from course_wiki ipmort views` currently
works.
We want to stardize on the prefixed version for a few different reasons.
"""
def __init__(self, old_import, new_import):
super().__init__()
self.old_import = old_import
self.new_import = new_import
def __str__(self):
return (
"Importing {self.old_import} instead of {self.new_import} is deprecated"
).format(self=self)
class DeprecatedEdxPlatformImportError(Exception):
"""
Error: An edx-platform module is being imported from an unsupported location.
See `DeprecatedEdxPlatformImportWarning` above for context.
"""
def __init__(self, old_import, new_import):
super().__init__()
self.old_import = old_import
self.new_import = new_import
def __str__(self):
return (
"Importing {self.old_import} instead of {self.new_import} is deprecated"
).format(self=self)
def warn_deprecated_import(old_import, new_import):
"""
Warn that a module is being imported from its old location.
"""
if settings.ERROR_ON_DEPRECATED_EDX_PLATFORM_IMPORTS:
raise DeprecatedEdxPlatformImportError(old_import, new_import)
set_custom_attribute("deprecated_edx_platform_import", old_import)
warnings.warn(
DeprecatedEdxPlatformImportWarning(old_import, new_import),
stacklevel=3, # Should surface the line that is doing the importing.
)