This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Consider moving importlib.abc.InspectLoader.source_to_code() to importlib.abc.Loader
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, carljm, eric.snow, python-dev
Priority: low Keywords:

Created on 2014-04-04 17:59 by brett.cannon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg215544 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-04-04 17:59
importlib.abc.InspectLoader.source_to_code exists on InspectLoader because InspectLoader.get_code() uses it. But there is technically no reason to leave it there and not simply move it up to importlib.abc.Loader(). There is also no reason to not make it a staticmethod so that one can use it without worrying about abstractmethod overrides.
msg215628 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-04-05 20:46
source_to_code() seems like a good fit on InspectLoader to me.  Is there
something in particular that motivated the idea of moving it up to Loader?
msg215631 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-04-05 20:54
The inspiration was that I realized there was no technical reason to have it on InspectLoader. Past that there was my thinking of trying to come up with a source_to_module() method on importlib.abc.Loader which would do the right thing with create_module() and init_module_attrs() such that replicating imp.load_module() would be something like::

  loader = importlib.machinery.SourceLoader  # or something
  with open('file.py') as file:
    module = loader.source_to_module(file.read(), location='file.py')
msg215637 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-04-05 22:39
Now that I've thought about it a little more, I'm more open to the idea.  Source is definitely a universal concept in Python, as is code.  So source_to_code() makes some sense on a most-base type like Loader.  On the other hand, source isn't necessarily associated with all loaders.

I guess my initial gut reaction was that it seemed out of place API-wise.  On InspectLoader it's presence is tied to get_code(), so it makes more sense.
msg218174 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-05-09 16:28
New changeset 9bd844792b32 by Brett Cannon in branch 'default':
Issue #21156: importlib.abc.InspectLoader.source_to_code() is now a
http://hg.python.org/cpython/rev/9bd844792b32
msg218175 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-05-09 16:29
I decided to make it a static method instead of relocating it.
msg343735 - (view) Author: Carl Meyer (carljm) * Date: 2019-05-28 06:08
Making `source_to_code` a staticmethod on the `InspectLoader` abc but not in the `importlib.machinery` implementation causes awkwardness for anyone trying to inherit `SourceFileLoader` and override `source_to_code` in typechecked code, since typeshed assumes that `SourceFileLoader` actually implements the `importlib.abc.FileLoader` interface.

Given the ABC registration, it seems that `importlib.machinery.SourceFileLoader` should in fact implement the `importlib.abc.FileLoader` interface.

Should we make `SourceFileLoader.source_to_code` a staticmethod also? If so, I can file a separate bug for that.
msg343817 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-05-28 20:32
@Carl Feel free to open new issues for whatever you need. :)
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65355
2019-05-28 20:32:36brett.cannonsetmessages: + msg343817
2019-05-28 06:08:46carljmsetnosy: + carljm
messages: + msg343735
2014-05-13 22:39:44berker.peksagsetstage: test needed -> resolved
2014-05-09 16:29:21brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg218175
2014-05-09 16:28:29python-devsetnosy: + python-dev
messages: + msg218174
2014-04-05 22:39:03eric.snowsetmessages: + msg215637
2014-04-05 20:54:42brett.cannonsetmessages: + msg215631
2014-04-05 20:46:24eric.snowsetnosy: + eric.snow
messages: + msg215628
2014-04-04 18:00:41brett.cannonsettype: behavior -> enhancement
2014-04-04 17:59:20brett.cannoncreate