builtin: added `__xonsh__.imp` to have an ability to import and use modules immediately in one line (#5595)

* imp

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* imp

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* imp

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andy Kipp 2024-07-12 02:21:21 +02:00 committed by GitHub
parent 87032f6d30
commit d08248f6aa
Failed to generate hash of commit
2 changed files with 35 additions and 0 deletions

25
news/inline_import.rst Normal file
View file

@ -0,0 +1,25 @@
**Added:**
* builtin: added ``__xonsh__.imp`` to have an ability to import and use modules immediately in one line
e.g. ``__xonsh__.json.loads('{}')``.
In the future ``__xonsh__`` will have short notation ``@`` and the line will looks more elegant ``@.imp.json.loads('{}')``.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -520,6 +520,15 @@ def xonsh_builtins(execer=None):
XSH.unload() XSH.unload()
class InlineImporter:
"""Inline importer allows to import and use module attribute or function in one line."""
def __getattr__(self, name):
if name.startswith("__"):
return getattr(super(), name)
return __import__(name)
class XonshSession: class XonshSession:
"""All components defining a xonsh session.""" """All components defining a xonsh session."""
@ -530,6 +539,7 @@ class XonshSession:
self.history = None self.history = None
self.shell = None self.shell = None
self.env = None self.env = None
self.imp = InlineImporter()
self.rc_files = None self.rc_files = None
# AST-invoked functions # AST-invoked functions