facebookincubator / cinder

Cinder is Meta's internal performance-oriented production version of CPython.
https://trycinder.com
Other
3.49k stars 121 forks source link

Implement `is_lazy_import` inspection API #77

Closed chuanzzx closed 2 years ago

chuanzzx commented 2 years ago

Summary

Add _importlib.is_lazy_import. Then, we can import _importlib and use its method is_lazy_import to check if a key in the specified dict is lazy when using Python.

Test Plan

We will import _importlib and use its function is_lazy_import to validate the behavior. The first argument needs to be a dictionary, and the second argument needs to be a string which is the module name you would like to check if it is lazy now. For example,

import _importlib
_importlib.is_lazy_import(globals(), "math")

Without Lazy Imports

Run

$ ./python.exe
>>> import _importlib

1. Non-imported module

When we test a non-imported module, it will throw the exception KeyError. This is because the module is not imported.

>>> _importlib.is_lazy_import(globals(), "math")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'math'

2. Imported module

When we test an imported module, it should return False. We are not using Lazy Imports, so the module is eagerly imported.

>>> import math
>>> _importlib.is_lazy_import(globals(), "math")
False

With Lazy Imports

Run

$ ./python.exe -L
>>> import _importlib

1. Non-imported module

When we test a non-imported module, it will throw the exception KeyError. This is because the module is not imported.

>>> import _importlib
>>> _importlib.is_lazy_import(globals(), "math")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'math'

2. Imported module

When we test an imported module, it should return True. We are using Lazy Imports, so the module should be lazy until it is used.

>>> import math
>>> _importlib.is_lazy_import(globals(), "math")
True

3. Executed module

After we use math.pi, the module is loaded. Thus, it should return False.

>>> math.pi
3.141592653589793
>>> _importlib.is_lazy_import(globals(), "math")
False

4. Wrong arguments

Wrong numbers of arguments

If we do not give the correct numbers of arguments to is_lazy_import, it will return an error message.

>>> _importlib.is_lazy_import(globals())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: is_lazy_import expected 2 arguments, got 1

Wrong dictionary type

If we provide a non-dict type argument in argument 1, it will return an error message.

>>> _importlib.is_lazy_import(123, "math")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: is_lazy_import() argument 1 must be dict, not int

Wrong key type

If we provide a non-string type argument in argument 2, it will return an error message.

>>> _importlib.is_lazy_import(globals(), 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: is_lazy_import() argument 2 must be str, not int
chuanzzx commented 2 years ago

Updated the code and test plan based on Itamar's feedback. Thanks! :)