Files
edx-platform/import_shims/warn.py
Kyle McCormick 090e10683c Rename sys_path_hacks/ to import_shims/
The old folder name is somewhat confusing, because the
folder contains shims to _compensate for the removal
of sys.path hacks_, but does not contain the sys.path
hacks themselves.

Furthermore, this import_shims/ system could also be
used for other import path changes, such as turning
the locally-installed packages in common/lib/
into regular, importable modules
(e.g. `from common.lib.xmodule import abc` instead of
`from xmodule import abc`). So, a name that is not
specific to the sys.path hacks may be better
in the medium-to-long term.

Along the same lines, we also rename SysPathHackWarning
to DeprecatedEdxPlatformImportWarning.
2020-10-30 10:20:48 -04:00

46 lines
1.4 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
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)
def warn_deprecated_import(old_import, new_import):
"""
Warn that a module is being imported from its old location.
"""
warnings.warn(
DeprecatedEdxPlatformImportWarning(old_import, new_import),
stacklevel=3, # Should surface the line that is doing the importing.
)