Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
New command 'sage --fiximports', invokes sage.misc.replace_dot_all
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Jan 28, 2023
1 parent 1fb9b35 commit 7a54612
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/.relint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
in mathematics it should be "homogeneous"
pattern: 'homogenous'

# Modularization anti-patterns (note: a tool to replace such 'from sage.all import' statements is given in SAGE_ROOT/src/sage/misc/replace_dot_all.py)
# Modularization anti-patterns

- name: 'namespace_pkg_all_import: import from .all of a namespace package'
hint: |
Sage library code should not import from sage.PAC.KAGE.all when sage.PAC.KAGE is an implicit
Hint: namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import.
Hint: namespace package. Type import_statements("SOME_IDENTIFIER") to find a more specific import,
Hint: or use 'sage --fiximports' to fix automatically in the source file.
# Keep in sync with SAGE_ROOT/src/sage/misc/replace_dot_all.py
pattern: 'from\s+sage(|[.](arith|categories|combinat|ext|graphs(|[.]decompositions)|interfaces|libs|matrix|misc|numerical(|[.]backends)|rings|sets))[.]all\s+import'
filePattern: '.*[.](py|pyx|pxi)$'
error: false # Make this a warning instead of an error for now
12 changes: 12 additions & 0 deletions src/bin/sage
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ usage_advanced() {
echo " Sage documentation for \"string\"."
echo " --search_src ... -- same as --grep"
echo " --search_doc ... -- same as --grepdoc"
echo " --fixdoctests file.py"
echo " -- Run doctests and replace output of failing doctests"
echo " with actual output."
echo " --fiximports <files|dir>"
echo " -- Replace imports from sage.PAC.KAGE.all by specific"
echo " imports when sage.PAC.KAGE is an implicit namespace"
echo " package"
fi
echo " --sh [...] -- run a shell with Sage environment variables"
echo " as they are set in the runtime of Sage"
Expand Down Expand Up @@ -974,6 +981,11 @@ if [ "$1" = '-startuptime' -o "$1" = '--startuptime' ]; then
exec sage-startuptime.py "$@"
fi

if [ "$1" = '-fiximports' -o "$1" = '--fiximports' ]; then
shift
exec sage-python -m sage.misc.replace_dot_all "$@"
fi

if [ "$1" = '-tox' -o "$1" = '--tox' ]; then
shift
if [ -n "$SAGE_SRC" -a -f "$SAGE_SRC/tox.ini" ]; then
Expand Down
15 changes: 11 additions & 4 deletions src/sage/misc/replace_dot_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
r"""
Implementation of the command ``sage --fiximports``.
This file provides a tool to fix the modularization antipattern ``namespace_pkg_all_import``
reported by ``tox -e relint``. Sage library code should not import from ``sage.PAC.KAGE.all``
when ``sage.PAC.KAGE`` is an implicit namespace package.
Expand Down Expand Up @@ -35,7 +37,7 @@
will fix all files in ``src/sage/arith`` and print out the unusual examples of ``import`` statements it finds.
In some rare cases, such as ``import`` statments appearing in doctests, the program will not be able to fix the ``import`` statement. The program will
In some rare cases, such as ``import`` statements appearing in doctests, the program will not be able to fix the ``import`` statement. The program will
print out the location of the file and the line number of the exceptional ``import`` statement. The user can then manually fix the ``import`` statement.
The program will also (usually) print out the suggested replacement for the ``import`` statement. The user can then copy and paste this replacement
into the file. In the cases a suggested replacement is not printed out, the user should use the function :func:`~sage.misc.dev_tools.import_statements`
Expand All @@ -62,9 +64,14 @@ def parse_arguments():
parser = argparse.ArgumentParser()
# Optional arguments
parser.add_argument(
"-l", "--location", help="Location of directory or file (root set at src/sage so input path from here). If no argument given, walks through all files in src/sage.", type=str)
parser.add_argument("-v", "--verbose", help="Increase output verbosity. Shows locations of any unusual cases of import statements and the corresponding changes.",
action="store_true") # Parse arguments
"-l", "--location",
help=("Location of directory or file (root set at src/sage so input path from here). "
"If no argument given, walks through all files in src/sage."),
type=str)
parser.add_argument(
"-v", "--verbose",
help="Increase output verbosity. Shows locations of any unusual cases of import statements and the corresponding changes.",
action="store_true") # Parse arguments
args = parser.parse_args()
return args

Expand Down

0 comments on commit 7a54612

Please sign in to comment.