-
-
Notifications
You must be signed in to change notification settings - Fork 656
Description
It's relatively easy using import system hooks in Python to make new file types 'importable' using the standard import statement. E.g. a file like mymodule.sage
could be imported as mymodule
just as though it were mymodule.py
.
Importing a .sage
module is of course just a thin layer over a normal Python module import, but to pass it through the Sage preparser.
There are of course some risks to making .sage
modules importable. For example, what if a user has both a foo.sage
and an foo.py
? Which one takes priority upon import foo
? In particular it could introduce quite some surprises if a user has something like os.sage
in their current directory.
Unfortunately it's not possible to extend the syntax of import statements, which would be nice. But, while the "module name" portion of an import statement must be a valid identifier, we can still otherwise process it however we want. So maybe .sage
module imports would be initiated only if .sage
is used explicitly in the import statement. So for example, to import a sage module named foo.sage
one would literally write:
import foo.sage
to distinguish it from a Python module named foo.py
. Of course, if there is a Python package named foo
that happens to have a sage
sub-module there is still a conflict. But this is a bit unlikely for most cases.
So in order to make such a feature available, while mitigating potential problems with it, I might suggest a few additional restrictions:
-
There would be a function to enable/disable
.sage
import functionality on the fly. When first introduced this would be disabled by default, but we would certainly want to make the feature easily discoverable through the documentation, with all the caveats discussed. -
In the off chance that there is a conflict between a
.sage
module an a plain Python module, the system should check for that. In that case the.sage
module still takes priority (if.sage
imports are enabled), but a warning about the conflict is shown. -
This feature should be Python 3 only. That will make it easier to implement, due to the significant differences between the import systems, and it will also give a nice motivation, to anyone who wants the feature, to switch to Python 3.
See also: https://groups.google.com/d/msg/sage-devel/tL8Whabyaoc/RMzlMyUtBwAJ
CC: @dimpase @jdemeyer @EmmanuelCharpentier @nthiery @nbruin @yuan-zhou
Component: interfaces
Issue created by migration from https://trac.sagemath.org/ticket/27074