Syntax restrictions for lazy imports

This commit is contained in:
Pablo Galindo 2025-09-25 17:33:12 +01:00 committed by Dino Viehland
parent 07a633f1f4
commit 20b14d9ca4
3 changed files with 175 additions and 0 deletions

View file

@ -3394,6 +3394,119 @@ def test_ifexp_body_stmt_else_stmt(self):
]:
self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg)
class LazyImportRestrictionTestCase(SyntaxErrorTestCase):
"""Test syntax restrictions for lazy imports."""
def test_lazy_import_in_try_block(self):
"""Test that lazy imports are not allowed inside try blocks."""
self._check_error("""\
try:
lazy import os
except:
pass
""", "lazy import not allowed inside try/except blocks")
self._check_error("""\
try:
lazy from sys import path
except ImportError:
pass
""", "lazy from ... import not allowed inside try/except blocks")
def test_lazy_import_in_trystar_block(self):
"""Test that lazy imports are not allowed inside try* blocks."""
self._check_error("""\
try:
lazy import json
except* Exception:
pass
""", "lazy import not allowed inside try/except blocks")
self._check_error("""\
try:
lazy from collections import defaultdict
except* ImportError:
pass
""", "lazy from ... import not allowed inside try/except blocks")
def test_lazy_import_in_function(self):
"""Test that lazy imports are not allowed inside functions."""
self._check_error("""\
def func():
lazy import math
""", "lazy import not allowed inside functions")
self._check_error("""\
def func():
lazy from datetime import datetime
""", "lazy from ... import not allowed inside functions")
def test_lazy_import_in_async_function(self):
"""Test that lazy imports are not allowed inside async functions."""
self._check_error("""\
async def async_func():
lazy import asyncio
""", "lazy import not allowed inside functions")
self._check_error("""\
async def async_func():
lazy from json import loads
""", "lazy from ... import not allowed inside functions")
def test_lazy_import_in_class(self):
"""Test that lazy imports are not allowed inside classes."""
self._check_error("""\
class MyClass:
lazy import typing
""", "lazy import not allowed inside classes")
self._check_error("""\
class MyClass:
lazy from abc import ABC
""", "lazy from ... import not allowed inside classes")
def test_lazy_import_star_forbidden(self):
"""Test that 'lazy from ... import *' is forbidden everywhere."""
# At module level should also be forbidden
self._check_error("lazy from os import *",
"lazy from ... import \\* is not allowed")
# Inside function should give lazy function error first
self._check_error("""\
def func():
lazy from sys import *
""", "lazy from ... import not allowed inside functions")
def test_lazy_import_nested_scopes(self):
"""Test lazy imports in nested scopes."""
self._check_error("""\
class Outer:
def method(self):
lazy import sys
""", "lazy import not allowed inside functions")
self._check_error("""\
def outer():
class Inner:
lazy import json
""", "lazy import not allowed inside classes")
self._check_error("""\
def outer():
def inner():
lazy from collections import deque
""", "lazy from ... import not allowed inside functions")
def test_lazy_import_valid_cases(self):
"""Test that lazy imports work at module level."""
# These should compile without errors
compile("lazy import os", "<test>", "exec")
compile("lazy from sys import path", "<test>", "exec")
compile("lazy import json as j", "<test>", "exec")
compile("lazy from datetime import datetime as dt", "<test>", "exec")
def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite())
return tests