| 1 |
# Test cases for imports and related features (compile and run) |
| 2 |
|
| 3 |
[case testImports] |
| 4 |
import testmodule |
| 5 |
import pkg2.mod |
| 6 |
import pkg2.mod2 as mm2 |
| 7 |
|
| 8 |
def f(x: int) -> int: |
| 9 |
return testmodule.factorial(5) |
| 10 |
|
| 11 |
def g(x: int) -> int: |
| 12 |
from welp import foo |
| 13 |
return foo(x) |
| 14 |
|
| 15 |
def test_import_basics() -> None: |
| 16 |
assert f(5) == 120 |
| 17 |
assert g(5) == 5 |
| 18 |
assert "pkg2.mod" not in globals(), "the root module should be in globals!" |
| 19 |
assert pkg2.mod.x == 1 |
| 20 |
assert "mod2" not in globals(), "pkg2.mod2 is aliased to mm2!" |
| 21 |
assert mm2.y == 2 |
| 22 |
|
| 23 |
def test_import_submodule_within_function() -> None: |
| 24 |
import pkg.mod |
| 25 |
assert pkg.x == 1 |
| 26 |
assert pkg.mod.y == 2 |
| 27 |
assert "pkg.mod" not in globals(), "the root module should be in globals!" |
| 28 |
|
| 29 |
def test_import_as_submodule_within_function() -> None: |
| 30 |
import pkg.mod as mm |
| 31 |
assert mm.y == 2 |
| 32 |
assert "pkg.mod" not in globals(), "the root module should be in globals!" |
| 33 |
|
| 34 |
# TODO: Don't add local imports to globals() |
| 35 |
# |
| 36 |
# def test_local_import_not_in_globals() -> None: |
| 37 |
# import nob |
| 38 |
# assert 'nob' not in globals() |
| 39 |
|
| 40 |
def test_import_module_without_stub_in_function() -> None: |
| 41 |
# 'psutil' must not have a stub in typeshed for this test case |
| 42 |
import psutil # type: ignore |
| 43 |
# TODO: We shouldn't add local imports to globals() |
| 44 |
# assert 'psutil' not in globals() |
| 45 |
assert isinstance(psutil.__name__, str) |
| 46 |
|
| 47 |
def test_import_as_module_without_stub_in_function() -> None: |
| 48 |
# 'psutil' must not have a stub in typeshed for this test case |
| 49 |
import psutil as pp # type: ignore |
| 50 |
assert 'psutil' not in globals() |
| 51 |
# TODO: We shouldn't add local imports to globals() |
| 52 |
# assert 'pp' not in globals() |
| 53 |
assert isinstance(pp.__name__, str) |
| 54 |
|
| 55 |
[file testmodule.py] |
| 56 |
def factorial(x: int) -> int: |
| 57 |
if x == 0: |
| 58 |
return 1 |
| 59 |
else: |
| 60 |
return x * factorial(x-1) |
| 61 |
[file welp.py] |
| 62 |
def foo(x: int) -> int: |
| 63 |
return x |
| 64 |
[file pkg/__init__.py] |
| 65 |
x = 1 |
| 66 |
[file pkg/mod.py] |
| 67 |
y = 2 |
| 68 |
[file pkg2/__init__.py] |
| 69 |
[file pkg2/mod.py] |
| 70 |
x = 1 |
| 71 |
[file pkg2/mod2.py] |
| 72 |
y = 2 |
| 73 |
[file nob.py] |
| 74 |
z = 3 |
| 75 |
|
| 76 |
[case testImportMissing] |
| 77 |
# The unchecked module is configured by the test harness to not be |
| 78 |
# picked up by mypy, so we can test that we do that right thing when |
| 79 |
# calling library modules without stubs. |
| 80 |
import unchecked # type: ignore |
| 81 |
import unchecked as lol # type: ignore |
| 82 |
assert unchecked.x == 10 |
| 83 |
assert lol.x == 10 |
| 84 |
[file unchecked.py] |
| 85 |
x = 10 |
| 86 |
|
| 87 |
[file driver.py] |
| 88 |
import native |
| 89 |
|
| 90 |
[case testFromImport] |
| 91 |
from testmodule import g |
| 92 |
|
| 93 |
def f(x: int) -> int: |
| 94 |
return g(x) |
| 95 |
[file testmodule.py] |
| 96 |
def g(x: int) -> int: |
| 97 |
return x + 1 |
| 98 |
[file driver.py] |
| 99 |
from native import f |
| 100 |
assert f(1) == 2 |
| 101 |
|
| 102 |
[case testFromImportWithUntypedModule] |
| 103 |
|
| 104 |
# avoid including an __init__.py and use type: ignore to test what happens |
| 105 |
# if mypy can't tell if mod isn't a module |
| 106 |
from pkg import mod # type: ignore |
| 107 |
|
| 108 |
def test_import() -> None: |
| 109 |
assert mod.h(8) == 24 |
| 110 |
|
| 111 |
[file pkg/mod.py] |
| 112 |
def h(x): |
| 113 |
return x * 3 |
| 114 |
|
| 115 |
[case testFromImportWithKnownModule] |
| 116 |
from pkg import mod1 |
| 117 |
from pkg import mod2 as modmod |
| 118 |
from pkg.mod2 import g as gg |
| 119 |
from pkg.mod3 import h as h2, g as g2 |
| 120 |
|
| 121 |
def test_import() -> None: |
| 122 |
assert mod1.h(8) == 24 |
| 123 |
assert modmod.g(1) == 1 |
| 124 |
assert gg(2) == 2 |
| 125 |
assert h2(10) == 12 |
| 126 |
assert g2(10) == 13 |
| 127 |
|
| 128 |
[file pkg/__init__.py] |
| 129 |
[file pkg/mod1.py] |
| 130 |
def h(x: int) -> int: |
| 131 |
return x * 3 |
| 132 |
|
| 133 |
[file pkg/mod2.py] |
| 134 |
def g(x: int) -> int: |
| 135 |
return x |
| 136 |
|
| 137 |
[file pkg/mod3.py] |
| 138 |
def h(x: int) -> int: |
| 139 |
return x + 2 |
| 140 |
|
| 141 |
def g(x: int) -> int: |
| 142 |
return x + 3 |
| 143 |
|
| 144 |
[case testFromImportWithUnKnownModule] |
| 145 |
def test_import() -> None: |
| 146 |
try: |
| 147 |
from pkg import a # type: ignore |
| 148 |
except ImportError: |
| 149 |
pass |
| 150 |
|
| 151 |
[file pkg/__init__.py] |
| 152 |
|
| 153 |
[case testMultipleFromImportsWithSamePackageButDifferentModules] |
| 154 |
from pkg import a |
| 155 |
from pkg import b |
| 156 |
|
| 157 |
def test_import() -> None: |
| 158 |
assert a.g() == 4 |
| 159 |
assert b.h() == 39 |
| 160 |
|
| 161 |
[file pkg/__init__.py] |
| 162 |
[file pkg/a.py] |
| 163 |
|
| 164 |
def g() -> int: |
| 165 |
return 4 |
| 166 |
|
| 167 |
[file pkg/b.py] |
| 168 |
|
| 169 |
def h() -> int: |
| 170 |
return 39 |
| 171 |
|
| 172 |
[case testReexport] |
| 173 |
# Test that we properly handle accessing values that have been reexported |
| 174 |
import a |
| 175 |
def f(x: int) -> int: |
| 176 |
return a.g(x) + a.foo + a.b.foo |
| 177 |
|
| 178 |
whatever = a.A() |
| 179 |
|
| 180 |
[file a.py] |
| 181 |
from b import g as g, A as A, foo as foo |
| 182 |
import b |
| 183 |
|
| 184 |
[file b.py] |
| 185 |
def g(x: int) -> int: |
| 186 |
return x + 1 |
| 187 |
|
| 188 |
class A: |
| 189 |
pass |
| 190 |
|
| 191 |
foo = 20 |
| 192 |
|
| 193 |
[file driver.py] |
| 194 |
from native import f, whatever |
| 195 |
import b |
| 196 |
|
| 197 |
assert f(20) == 61 |
| 198 |
assert isinstance(whatever, b.A) |
| 199 |
|
| 200 |
[case testAssignModule] |
| 201 |
import a |
| 202 |
assert a.x == 20 |
| 203 |
a.x = 10 |
| 204 |
[file a.py] |
| 205 |
x = 20 |
| 206 |
[file driver.py] |
| 207 |
import native |
| 208 |
|
| 209 |
[case testLazyImport] |
| 210 |
import shared |
| 211 |
|
| 212 |
def do_import() -> None: |
| 213 |
import a |
| 214 |
|
| 215 |
def test_lazy() -> None: |
| 216 |
assert shared.counter == 0 |
| 217 |
do_import() |
| 218 |
assert shared.counter == 1 |
| 219 |
|
| 220 |
[file a.py] |
| 221 |
import shared |
| 222 |
shared.counter += 1 |
| 223 |
|
| 224 |
[file shared.py] |
| 225 |
counter = 0 |
| 226 |
|
| 227 |
[case testDelayedImport] |
| 228 |
def test_delayed() -> None: |
| 229 |
import a |
| 230 |
print("inbetween") |
| 231 |
import b |
| 232 |
|
| 233 |
[file a.py] |
| 234 |
print("first") |
| 235 |
|
| 236 |
[file b.py] |
| 237 |
print("last") |
| 238 |
|
| 239 |
[out] |
| 240 |
first |
| 241 |
inbetween |
| 242 |
last |
| 243 |
|
| 244 |
[case testImportErrorLineNumber] |
| 245 |
def test_error() -> None: |
| 246 |
try: |
| 247 |
import enum |
| 248 |
import dataclasses, missing # type: ignore[import] |
| 249 |
except ImportError as e: |
| 250 |
line = e.__traceback__.tb_lineno # type: ignore[attr-defined] |
| 251 |
assert line == 4, f"traceback's line number is {line}, expected 4" |
| 252 |
|
| 253 |
[case testImportGroupIsolation] |
| 254 |
def func() -> None: |
| 255 |
import second |
| 256 |
|
| 257 |
def test_isolation() -> None: |
| 258 |
import first |
| 259 |
func() |
| 260 |
|
| 261 |
[file first.py] |
| 262 |
print("first") |
| 263 |
|
| 264 |
[file second.py] |
| 265 |
print("second") |
| 266 |
|
| 267 |
[out] |
| 268 |
first |
| 269 |
second |