Skip to content

Commit 2cd6166

Browse files
author
Release Manager
committed
Trac #34945: Refactoring tool to fix modularization anti-patterns
The new command `sage --fiximports` replaces imports from `sage.PAC.KAGE.all` by more specific imports when `sage.PAC.KAGE` is an implicit namespace package. Part of Meta-ticket #34201 URL: https://trac.sagemath.org/34945 Reported by: mkoeppe Ticket author(s): Alex Chandler, Matthias Koeppe Reviewer(s): Matthias Koeppe, Alex Chandler
2 parents c898228 + e74dbf6 commit 2cd6166

File tree

4 files changed

+483
-1
lines changed

4 files changed

+483
-1
lines changed

src/.relint.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
- name: 'namespace_pkg_all_import: import from .all of a namespace package'
4747
hint: |
4848
Sage library code should not import from sage.PAC.KAGE.all when sage.PAC.KAGE is an implicit
49-
Hint: namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import.
49+
Hint: namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import,
50+
Hint: or use 'sage --fiximports' to fix automatically in the source file.
51+
# Keep in sync with SAGE_ROOT/src/sage/misc/replace_dot_all.py
5052
pattern: 'from\s+sage(|[.](arith|categories|combinat|ext|graphs(|[.]decompositions)|interfaces|libs|matrix|misc|numerical(|[.]backends)|rings|sets))[.]all\s+import'
5153
filePattern: '.*[.](py|pyx|pxi)$'
5254
error: false # Make this a warning instead of an error for now

src/bin/sage

+12
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,13 @@ usage_advanced() {
479479
echo " Sage documentation for \"string\"."
480480
echo " --search_src ... -- same as --grep"
481481
echo " --search_doc ... -- same as --grepdoc"
482+
echo " --fixdoctests file.py"
483+
echo " -- Run doctests and replace output of failing doctests"
484+
echo " with actual output."
485+
echo " --fiximports <files|dir>"
486+
echo " -- Replace imports from sage.PAC.KAGE.all by specific"
487+
echo " imports when sage.PAC.KAGE is an implicit namespace"
488+
echo " package"
482489
fi
483490
echo " --sh [...] -- run a shell with Sage environment variables"
484491
echo " as they are set in the runtime of Sage"
@@ -974,6 +981,11 @@ if [ "$1" = '-startuptime' -o "$1" = '--startuptime' ]; then
974981
exec sage-startuptime.py "$@"
975982
fi
976983

984+
if [ "$1" = '-fiximports' -o "$1" = '--fiximports' ]; then
985+
shift
986+
exec sage-python -m sage.misc.replace_dot_all "$@"
987+
fi
988+
977989
if [ "$1" = '-tox' -o "$1" = '--tox' ]; then
978990
shift
979991
if [ -n "$SAGE_SRC" -a -f "$SAGE_SRC/tox.ini" ]; then

src/doc/en/developer/packaging_sage_library.rst

+4
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ distribution -- which then must be declared as a run-time dependency.
274274
the ``sage:`` prompt. In particular, no Sage library code should import from
275275
:mod:`sage.rings.all`.
276276

277+
To audit the Sage library for such imports, use ``sage --tox -e relint``.
278+
In most cases, the imports can be fixed automatically using the
279+
tool ``sage --fiximports``.
280+
277281
- Replace module-level imports by method-level imports. Note that
278282
this comes with a small runtime overhead, which can become
279283
noticeable if the method is called in tight inner loops.

0 commit comments

Comments
 (0)