From: Brett Cannon <br...@python.org>
Sent: 08 November 2019 18:10
To: Ricky Teachey <ri...@teachey.org>
Cc: Dan Sommers <2qdxy4rzwzuui...@potatochowder.com>; python-ideas 
<python-ideas@python.org>
Subject: [Python-ideas] Re: Suggest having a mechanism to distinguish import 
sources



On Fri, Nov 8, 2019 at 10:01 AM Ricky Teachey 
<ri...@teachey.org<mailto:ri...@teachey.org>> wrote:
throwing this idea out there, no idea if it is practical but it might be pretty 
nice/easily understood syntax.

could a context manager be created such that anything imported under it is 
guaranteed to be imported from the standard library, and produce an error 
otherwise? perhaps by adding a level keyword argument to the __import__ built 
in.

__import__ already has a 'level' argument.
[Steve Barnes]
To be honest this is the first that I have heard of __import__(level) but 
looking at the docs and help for 3.8 doesn’t read like what you describe below 
– maybe it is an un/under-documented feature – it certainly wasn’t easy to find 
in the documentation 
(https://docs.python.org/3/library/functions.html#__import__)!

level specifies whether to use absolute or relative imports. 0 (the default) 
means only perform absolute imports. Positive values for level indicate the 
number of parent directories to search relative to the directory of the module 
calling 
__import__()<https://docs.python.org/3/library/functions.html#__import__> (see 
PEP 328<https://www.python.org/dev/peps/pep-0328> for the details).
PEP 328 doesn’t seem to mention any of the names detailed below.


something like:

with __import__(level="std"):
    # imports guaranteed to fail of not in the standard library
    from pathlib import Path
    from sys import argv

with __import__(level="package"):
    # imports guaranteed to fail of not in the current package
    import mod1
    import mod2

with __import__(level="local"):
    # imports guaranteed to fail of not in the local directory
    import mod1
    import mod2

with __import__(level="site"):
    # imports guaranteed to fail if not in site-packages, or some other 
definition that makes sense
    import numpy as np

Not without frame inspection to know what import statements are in the context 
manager's block.

This can all be done with code which calls importlib.import_module() and checks 
__spec__.origin to see where the module came from. Basically if you're willing 
to give up the syntax support of 'import' statements (which are just calls to 
__import__ with some assignments afterwards to bind things to names) you can 
have this protection today without adding syntax (which is always a massive 
ask).
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TKGOPWMXDLKE3X2OX6IDNKZQZJN723MM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to