diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index 97055afb01a..35f09ba3063 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -255,18 +255,19 @@ cdef class LazyImport(): raise FeatureNotPresentError(self._feature, reason=f'Importing {self._name} failed: {e}') raise - name = self._as_name if self._deprecation is not None: from sage.misc.superseded import deprecation_cython as deprecation try: trac_number, message = self._deprecation except TypeError: trac_number = self._deprecation - message = ('\nImporting {name} from here is deprecated. ' + - 'If you need to use it, please import it directly from' + - ' {module_name}').format(name=name, module_name=self._module) + import_command = f'from {self._module} import {self._name}' + if self._as_name != self._name: + import_command += f' as {self._as_name}' + message = f'\nImporting {self._as_name} from here is deprecated; please use "{import_command}" instead.' deprecation(trac_number, message) # Replace the lazy import in the namespace by the actual object + name = self._as_name if self._namespace is not None: if self._namespace.get(name) is self: self._namespace[name] = self._object @@ -1028,7 +1029,7 @@ def lazy_import(module, names, as_=None, *, sage: lazy_import('sage.all', 'Qp', 'my_Qp', deprecation=14275) sage: my_Qp(5) doctest:...: DeprecationWarning: - Importing my_Qp from here is deprecated. If you need to use it, please import it directly from sage.all + Importing my_Qp from here is deprecated; please use "from sage.all import Qp as my_Qp" instead. See http://trac.sagemath.org/14275 for details. 5-adic Field with capped relative precision 20