gh-118761: Add helper to ensure that lazy imports are actually lazy (#132614)

This ensures that if we jump through some hoops to make sure something is imported
lazily, we don't regress on importing it.

I recently already accidentally made typing import warnings and annotationlib eagerly.

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Jelle Zijlstra 2025-04-16 20:46:36 -07:00 committed by GitHub
parent b530e174a3
commit 39ee468e09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 0 deletions

View file

@ -6317,6 +6317,15 @@ class InternalsTests(BaseTestCase):
typing._collect_parameters
self.assertEqual(cm.filename, __file__)
def test_lazy_import(self):
import_helper.ensure_lazy_imports("typing", {
"warnings",
"inspect",
"re",
"contextlib",
# "annotationlib", # TODO
})
@lru_cache()
def cached_func(x, y):