Closed chuanzzx closed 2 years ago
I'm a bit confused about the semantics.
if excluding=["foo"]
is specified, this should cause import foo
to be eager, but not the imports within foo
, no?
I also feel the API's behavior is not intuitive.
My understanding for excluding=["foo"]
is closer to eagerly load foo
when it is loading.
Therefore, if we import foo
after setting excluding=["foo"]
, foo
is still a lazy object.
However, when we run foo
to force loading, foo
will eagerly load itself and its sub-modules.
That's why the message I'm bar!
is in the expected result.
Checking the original description in T120948621 might be helpful!
Summary
Add an optional argument
excluding
forimportlib.set_lazy_imports
.Test Plan
excluding
is an optional argument for importlib.set_lazy_imports(). It should be a list or a callback function, containing all the modules would like to be eagerly loaded.For example, let us have a module called
foo.py
.If we call
importlib.set_lazy_imports(excluding=["foo"])
, two actions will be executed at this moment.foo
is marked as eagerly loaded in the futureThat is, if we
import foo
and runfoo
, it will loadimportlib
andbar
at the same time.Pre-req
Create
foo.py
andbar.py
under the same path ofpython.exe
.Start with/without Lazy Imports
No matter with or without
-L
, the expected results are the same in the following tests. The only difference is we need to use./python.exe -L
rather than./python.exe
if we would like to start with Lazy Imports at the beginning.Use a list as a filter
Run
Expected result:
Use a callback function as a filter
Run
Expected result:
Correct the excluding argument by running again
If we would like to change our
excluding
setting, we can run the commandset_lazy_imports
with the newexcluding
argument again.For example, run
Set the first
excluding
argument as theHello
callback function.Correct the
excluding
argument by setting nothing.Correct the
excluding
argument by using theeager_foo
callback function.Wrong type of the argument
No matter with or without
-L
, if a parameter use inexcluding
is not alist
or acallback
, it will return the TypeError message.