My motivation for this is wanting to write a module that handles .gql files by parsing them as GraphQL and compiling the resulting Python, though I can immediately several things that would benefit from similar support:
- A templating system that uses the new t-strings.
- Parser generators
- Dynamic wrapping of code in other languages into extension modules
…and I’m sure people can think of others.
I asked here whether I’d found a genuine limitation, and it seems that I have.
Right now (3.14) there are two key specialisation points for the import machinery: sys.meta_path and sys.path_hooks. Neither of those is well suited to the task of adding a new suffix and a Loader to handle it: the existing machinery specifies the list of handled suffixes at boot time (when constructing the FileFinder.path_hook to put in sys.path_hooks). What is needed is a specialisation point for file extensions.
I’ve studied the source code and believe it could be added very easily:
- Create a new
sys.suffix_loaders, a list of (suffix, loader) pairs. - Augment the
FileFinderconstructor andpath_hookclass method to accept adetailskeyword argument. Either*loader_detailsmust be empty ordetailsmust beNone. Ifdetailsis not None, it must be an iterable returning (suffix, loader) pairs. Note thatdetailsis inherently compatible with, and could be assigned directly to, theFileFinder._loadersmember; this minimises refactoring. - Alter
_bootstrap_external._installto, instead of creating aFileFinder(*supported_loaders), populatesys.suffix_loaderswith them and create aFileFinder(details=sys.suffix_loaders).
I believe that would be 100% backward compatible, and have negligible performance (CPU, memory, code size) impact.
Does this seem like a sensible improvement to other people?