Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider moving to #65355

brettcannon opened this issue Apr 4, 2014 · 8 comments
stdlib Python modules in the Lib dir type-feature A feature request or enhancement


Copy link

BPO 21156
Nosy @brettcannon, @carljm, @ericsnowcurrently

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = ''
closed_at = <Date 2014-05-09.16:29:21.556>
created_at = <Date 2014-04-04.17:59:20.194>
labels = ['type-feature', 'library']
title = 'Consider moving to'
updated_at = <Date 2019-05-28.20:32:36.770>
user = '' fields:

activity = <Date 2019-05-28.20:32:36.770>
actor = 'brett.cannon'
assignee = 'brett.cannon'
closed = True
closed_date = <Date 2014-05-09.16:29:21.556>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2014-04-04.17:59:20.194>
creator = 'brett.cannon'
dependencies = []
files = []
hgrepos = []
issue_num = 21156
keywords = []
message_count = 8.0
messages = ['215544', '215628', '215631', '215637', '218174', '218175', '343735', '343817']
nosy_count = 4.0
nosy_names = ['brett.cannon', 'carljm', 'python-dev', 'eric.snow']
pr_nums = []
priority = 'low'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = ''
versions = ['Python 3.5']

Copy link
Member Author 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 There is also no reason to not make it a staticmethod so that one can use it without worrying about abstractmethod overrides.

@brettcannon brettcannon added the type-bug An unexpected behavior, bug, or error label Apr 4, 2014
@brettcannon brettcannon self-assigned this Apr 4, 2014
@brettcannon brettcannon added stdlib Python modules in the Lib dir type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Apr 4, 2014
Copy link

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?

Copy link
Member Author

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 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('') as file:
    module = loader.source_to_module(, location='')

Copy link

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.

Copy link

python-dev mannequin commented May 9, 2014

New changeset 9bd844792b32 by Brett Cannon in branch 'default':
Issue bpo-21156: is now a

Copy link
Member Author

I decided to make it a static method instead of relocating it.

Copy link

carljm commented May 28, 2019

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 interface.

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

Should we make SourceFileLoader.source_to_code a staticmethod also? If so, I can file a separate bug for that.

Copy link
Member Author

@carl Feel free to open new issues for whatever you need. :)

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
None yet

No branches or pull requests

3 participants