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.

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
_______________________________________________
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/VGQBGOVC5OMMYNHY7MXXBOHK6ZXK3MFQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to