1import sys
2
3
4class nothing:
5 """
6 A context that does nothing, the interpreter deferring the imports itself.
7 """
8
9 def __enter__(self):
10 return self
11
12 def __exit__(self, *exc_info):
13 return False
14
15
16def backport(): # pragma: no cover
17 """
18 Borrow lazy_importer's emulation of PEP 810.
19 """
20 from lazy_importer import lazy_importer
21
22 return lazy_importer()
23
24
25lazy_importer = nothing if sys.version_info >= (3, 15) else backport
26"""
27Defer the imports in the block that follows, before 3.15 as after.
28
29Two properties of the emulation shape how a module may declare its lazy
30imports. Neither holds from 3.15, which defers them natively.
31
32A lazy name is bound to a proxy. Attribute access and calls reach the
33object it stands for, so ``csv.reader`` and ``_adapters.Message`` behave,
34but ``isinstance`` and anything else handed the object itself see the
35proxy and get the wrong answer in silence. Reach such an object through
36a module (``_adapters.Message``), or resolve the name with
37``lazy_importer.resolve`` before testing it.
38
39``from . import <submodule>`` names the package as the module to defer.
40That is fine from another module, but in the package's own ``__init__``
41the proxy stands for the module being executed and resolves itself
42through the globals entry that is the proxy, until the stack is
43exhausted. Import the member there instead.
44"""