run-imports.test 5.2 KB

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