1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Tests that import inside functions binds to local scope, not global
# === Import statement inside function ===
def test_import_local():
import sys
return sys.platform
# Call to verify import works inside function
result = test_import_local()
assert isinstance(result, str)
# Verify sys is NOT in global scope after function call
try:
sys
assert False, 'sys should not be in global scope'
except NameError:
pass # Expected: sys is local to the function
# === From import inside function ===
def test_from_import_local():
from typing import Any
return Any
any_result = test_from_import_local()
assert repr(any_result) == 'typing.Any'
# Verify Any is NOT in global scope after function call
try:
Any
assert False, 'Any should not be in global scope'
except NameError:
pass # Expected: Any is local to the function
# === Aliased import inside function ===
def test_aliased_import_local():
import sys as system
return system.platform
alias_result = test_aliased_import_local()
assert isinstance(alias_result, str)
# Verify system is NOT in global scope
try:
system
assert False, 'system should not be in global scope'
except NameError:
pass # Expected: system is local to the function
# === Global import remains accessible ===
import sys as global_sys
assert isinstance(global_sys.platform, str)
def use_global_import():
# This should access the global sys, not create a new local
return global_sys.platform
assert use_global_import() == global_sys.platform