commandline.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. -- Test cases for invoking mypyc on the command line
  2. --
  3. -- These are slow -- do not add test cases unless you have a very good reason to do so.
  4. [case testCompileMypyc]
  5. # cmd: a.py b.py p/__init__.py p/q.py
  6. import os.path
  7. import p
  8. import p.q
  9. import a
  10. import b
  11. print('<main>', b.g(a.A()))
  12. try:
  13. a.f('')
  14. except TypeError:
  15. pass
  16. else:
  17. assert False
  18. for x in [a, b, p, p.q]:
  19. assert os.path.splitext(x.__file__)[1] != '.py'
  20. [file z.py]
  21. [file a.py]
  22. import b
  23. import c
  24. from p import s
  25. from typing import NamedTuple
  26. print('<a>', ord('A') == 65) # Test full builtins
  27. class A:
  28. def __init__(self) -> None:
  29. self.x = 4
  30. def f(x: int) -> b.B:
  31. return b.B(x)
  32. class B:
  33. def __init__(self, x: int, y: str) -> None:
  34. self.x = x
  35. print('<a>', f(5).x)
  36. print('<c>', c.foo())
  37. assert s.bar(10) == 20
  38. class NT(NamedTuple):
  39. x: int
  40. print(NT(2))
  41. [file b.py]
  42. import a
  43. import p.q
  44. class B:
  45. def __init__(self, x: int) -> None:
  46. self.x = x
  47. def g(z: 'a.A') -> int:
  48. return p.q.foo(z.x)
  49. print('<b>', 'here')
  50. [file c.py]
  51. def foo() -> int:
  52. return 10
  53. [file p/__init__.py]
  54. [file p/q.py]
  55. import p.r
  56. def foo(x: int) -> int:
  57. return x*p.r.foo(x)
  58. [file p/r.py]
  59. def foo(x: int) -> int:
  60. return x
  61. [file p/s.py]
  62. def bar(x: int) -> int:
  63. return x*2
  64. [out]
  65. <b> here
  66. <a> True
  67. <a> 5
  68. <c> 10
  69. NT(x=2)
  70. <main> 16
  71. -- This test is here so we can turn it on when we get nervous about
  72. -- this case, but is disabled for speed reasons.
  73. [case testCompileMypycOne-skip]
  74. # cmd: a.py
  75. import os.path
  76. import a
  77. assert os.path.splitext(a.__file__)[1] != '.py'
  78. assert a.f(10) == 100
  79. [file a.py]
  80. def f(x: int) -> int:
  81. return x*x
  82. [case testErrorOutput]
  83. # cmd: test.py
  84. [file test.py]
  85. from typing import List, Any, AsyncIterable
  86. from typing_extensions import Final
  87. from mypy_extensions import trait, mypyc_attr
  88. from functools import singledispatch
  89. def busted(b: bool) -> None:
  90. for i in range(1, 10, 0): # E: range() step can't be zero
  91. try:
  92. if i == 5:
  93. break # E: break inside try/finally block is unimplemented
  94. elif i == 4:
  95. continue # E: continue inside try/finally block is unimplemented
  96. finally:
  97. print('oops')
  98. print(sum([1,2,3]))
  99. x = [1,2]
  100. class Foo:
  101. a, b = (10, 20) # E: Only assignment to variables is supported in class bodies
  102. x[0] = 10 # E: Only assignment to variables is supported in class bodies
  103. lol = 20
  104. l = [10] # W: Unsupported default attribute value
  105. c = d = 50 # E: Multiple assignment in class bodies not supported
  106. if 1+1 == 2: # E: Unsupported statement in class body
  107. x = 10
  108. Foo.lol = 50 # E: Only class variables defined as ClassVar can be assigned to
  109. def decorator(x: Any) -> Any:
  110. return x
  111. class NeverMetaclass(type): # E: Inheriting from most builtin types is unimplemented
  112. pass
  113. class Concrete1:
  114. pass
  115. @trait
  116. class PureTrait:
  117. pass
  118. @trait
  119. class Trait1:
  120. pass
  121. class Concrete2:
  122. pass
  123. @trait
  124. class Trait2(Concrete2):
  125. pass
  126. @decorator
  127. class NonExt(Concrete1): # E: Non-extension classes may not inherit from extension classes
  128. pass
  129. class NopeMultipleInheritance(Concrete1, Concrete2): # E: Multiple inheritance is not supported (except for traits)
  130. pass
  131. class NopeMultipleInheritanceAndBadOrder(Concrete1, Trait1, Concrete2): # E: Multiple inheritance is not supported (except for traits)
  132. pass
  133. class NopeMultipleInheritanceAndBadOrder2(Concrete1, Concrete2, Trait1): # E: Multiple inheritance is not supported (except for traits)
  134. pass
  135. class NopeMultipleInheritanceAndBadOrder3(Trait1, Concrete1, Concrete2): # E: Non-trait base must appear first in parent list # E: Multiple inheritance is not supported (except for traits)
  136. pass
  137. class NopeBadOrder(Trait1, Concrete2): # E: Non-trait base must appear first in parent list
  138. pass
  139. @decorator
  140. class NonExt2:
  141. @property # E: Property setters not supported in non-extension classes
  142. def test(self) -> int:
  143. return 0
  144. @test.setter
  145. def test(self, x: int) -> None:
  146. pass
  147. iterator_warning = (i+1 for i in range(10)) # W: Treating generator comprehension as list
  148. # But we don't want warnings for these cases:
  149. tup = tuple(i+1 for i in range(10))
  150. a_str = " ".join(str(i) for i in range(10))
  151. wtvr = next(i for i in range(10) if i == 5)
  152. d1 = {1: 2}
  153. # Make sure we can produce an error when we hit the awful None case
  154. def f(l: List[object]) -> None:
  155. x = None # E: Local variable "x" has inferred type None; add an annotation
  156. for i in l:
  157. if x is None:
  158. x = i
  159. @mypyc_attr(allow_interpreted_subclasses=True)
  160. class AllowInterp1(Concrete1): # E: Base class "test.Concrete1" does not allow interpreted subclasses
  161. pass
  162. @mypyc_attr(allow_interpreted_subclasses=True)
  163. class AllowInterp2(PureTrait): # E: Base class "test.PureTrait" does not allow interpreted subclasses
  164. pass
  165. async def async_generators() -> AsyncIterable[int]:
  166. yield 1 # E: async generators are unimplemented
  167. @singledispatch
  168. def a(arg) -> None:
  169. pass
  170. @decorator # E: Calling decorator after registering function not supported
  171. @a.register
  172. def g(arg: int) -> None:
  173. pass
  174. @a.register
  175. @decorator
  176. def h(arg: str) -> None:
  177. pass
  178. @decorator
  179. @decorator # E: Calling decorator after registering function not supported
  180. @a.register
  181. def i(arg: Foo) -> None:
  182. pass
  183. [case testOnlyWarningOutput]
  184. # cmd: test.py
  185. [file test.py]
  186. names = (str(v) for v in [1, 2, 3]) # W: Treating generator comprehension as list