run-tuples.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Test cases for tuples (compile and run)
  2. [case testTuple]
  3. from typing import List, Optional, Tuple
  4. from typing import Tuple
  5. def f(x: Tuple[int, int]) -> Tuple[int,int]:
  6. return x
  7. def lurr(x: List[Optional[Tuple[int, str]]]) -> object:
  8. return x[0]
  9. def asdf(x: Tuple[int, str]) -> None:
  10. pass
  11. [file driver.py]
  12. from testutil import assertRaises
  13. from native import f, lurr, asdf
  14. assert f((1,2)) == (1, 2)
  15. assert lurr([(1, '2')]) == (1, '2')
  16. with assertRaises(TypeError):
  17. print(lurr([(1, 2)]))
  18. with assertRaises(TypeError):
  19. asdf((1, 2))
  20. [case testTupleGet]
  21. from typing import Tuple
  22. def f(x: Tuple[Tuple[int, bool], int]) -> int:
  23. return x[0][0]
  24. [file driver.py]
  25. from native import f
  26. print(f(((1,True),2)))
  27. big_number = pow(2, 80)
  28. print(f(((big_number,True),2)))
  29. [out]
  30. 1
  31. 1208925819614629174706176
  32. [case testSequenceTupleArg]
  33. from typing import Tuple
  34. def f(x: Tuple[int, ...]) -> int:
  35. return x[1]
  36. [file driver.py]
  37. from native import f
  38. print(f((1,2,3,4)))
  39. [out]
  40. 2
  41. [case testTupleAttr]
  42. from typing import Tuple
  43. class C:
  44. b: Tuple[Tuple[Tuple[int, int], int], int, str, object]
  45. c: Tuple[()]
  46. def f() -> None:
  47. c = C()
  48. c.b = (((1, 2), 2), 1, 'hi', 'hi2')
  49. print(c.b)
  50. def g() -> None:
  51. try:
  52. h()
  53. except Exception:
  54. print('caught the exception')
  55. def h() -> Tuple[Tuple[Tuple[int, int], int], int, str, object]:
  56. raise Exception('Intentional exception')
  57. [file driver.py]
  58. from native import f, g, C
  59. f()
  60. g()
  61. assert not hasattr(C(), 'c')
  62. [out]
  63. (((1, 2), 2), 1, 'hi', 'hi2')
  64. caught the exception
  65. [case testNamedTupleAttributeRun]
  66. from typing import NamedTuple
  67. NT = NamedTuple('NT', [('x', int), ('y', int)])
  68. def f(nt: NT) -> int:
  69. if nt.x > nt.y:
  70. return nt.x
  71. return nt.y
  72. nt = NT(1, 2)
  73. [file driver.py]
  74. from native import NT, nt, f
  75. assert f(nt) == 2
  76. assert f(NT(3, 2)) == 3
  77. class Sub(NT):
  78. pass
  79. assert f(Sub(3, 2)) == 3
  80. -- Ref: https://github.com/mypyc/mypyc/issues/924
  81. [case testNamedTupleClassSyntax]
  82. from typing import Dict, List, NamedTuple, Optional, Tuple, Union
  83. from typing_extensions import final
  84. class FuncIR: pass
  85. StealsDescription = Union[bool, List[bool]]
  86. class Record(NamedTuple):
  87. st_mtime: float
  88. st_size: int
  89. is_borrowed: bool
  90. hash: str
  91. python_path: Tuple[str, ...]
  92. type: 'ClassIR'
  93. method: FuncIR
  94. shadow_method: Optional[FuncIR]
  95. classes: Dict[str, 'ClassIR']
  96. steals: StealsDescription
  97. ordering: Optional[List[int]]
  98. extra_int_constants: List[Tuple[int]]
  99. # Make sure mypyc loads the annotation string for this forward reference.
  100. # Ref: https://github.com/mypyc/mypyc/issues/938
  101. class ClassIR: pass
  102. # Ref: https://github.com/mypyc/mypyc/issues/927
  103. @final
  104. class Inextensible(NamedTuple):
  105. x: int
  106. [file driver.py]
  107. from typing import ForwardRef, Optional
  108. from native import ClassIR, FuncIR, Record
  109. assert Record.__annotations__ == {
  110. 'st_mtime': float,
  111. 'st_size': int,
  112. 'is_borrowed': bool,
  113. 'hash': str,
  114. 'python_path': tuple,
  115. 'type': ForwardRef('ClassIR'),
  116. 'method': FuncIR,
  117. 'shadow_method': type,
  118. 'classes': dict,
  119. 'steals': type,
  120. 'ordering': type,
  121. 'extra_int_constants': list,
  122. }, Record.__annotations__
  123. [case testTupleOps]
  124. from typing import Tuple, List, Any, Optional
  125. from typing_extensions import Final
  126. def f() -> Tuple[()]:
  127. return ()
  128. def test_empty_tuple() -> None:
  129. assert f() == ()
  130. def f2() -> Any:
  131. return ()
  132. def test_empty_tuple_with_any_type():
  133. assert f2() == ()
  134. def f3() -> int:
  135. x = (False, 1)
  136. return x[1]
  137. def test_new_tuple() -> None:
  138. assert f3() == 1
  139. def f4(y: int) -> int:
  140. x = (False, y)
  141. return x[1]
  142. def test_new_tuple_boxed_int() -> None:
  143. big_number = 1208925819614629174706176
  144. assert f4(big_number) == big_number
  145. def f5(x: List[int]) -> int:
  146. return tuple(x)[1]
  147. def test_sequence_tuple() -> None:
  148. assert f5([1,2,3,4]) == 2
  149. def f6(x: List[int]) -> int:
  150. return len(tuple(x))
  151. def test_sequence_tuple_len() -> None:
  152. assert f6([1,2,3,4]) == 4
  153. def f7(x: List[Tuple[int, int]]) -> int:
  154. a, b = x[0]
  155. return a + b
  156. def test_unbox_tuple() -> None:
  157. assert f7([(5, 6)]) == 11
  158. # Test that order is irrelevant to unions. Really I only care that this builds.
  159. class A:
  160. pass
  161. def lol() -> A:
  162. return A()
  163. def foo(x: bool, y: bool) -> Tuple[Optional[A], bool]:
  164. z = lol()
  165. return None if y else z, x
  166. def test_slicing() -> None:
  167. # Use dummy adds to avoid constant folding
  168. zero = int()
  169. two = zero + 2
  170. s: Tuple[str, ...] = ("f", "o", "o", "b", "a", "r")
  171. assert s[two:] == ("o", "b", "a", "r")
  172. assert s[:two] == ("f", "o")
  173. assert s[two:-two] == ("o", "b")
  174. assert s[two:two] == ()
  175. assert s[two:two + 1] == ("o",)
  176. assert s[-two:] == ("a", "r")
  177. assert s[:-two] == ("f", "o", "o", "b")
  178. assert s[:] == ("f", "o", "o", "b", "a", "r")
  179. assert s[two:333] == ("o", "b", "a", "r")
  180. assert s[333:two] == ()
  181. assert s[two:-333] == ()
  182. assert s[-333:two] == ("f", "o")
  183. long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
  184. assert s[1:long_int] == ("o", "o", "b", "a", "r")
  185. assert s[long_int:] == ()
  186. assert s[-long_int:-1] == ("f", "o", "o", "b", "a")
  187. def f8(val: int) -> bool:
  188. return val % 2 == 0
  189. def test_sequence_generator() -> None:
  190. source_list = [1, 2, 3]
  191. a = tuple(f8(x) for x in source_list)
  192. assert a == (False, True, False)
  193. source_tuple: Tuple[int, ...] = (1, 2, 3)
  194. a = tuple(f8(x) for x in source_tuple)
  195. assert a == (False, True, False)
  196. source_fixed_length_tuple = (1, 2, 3, 4)
  197. a = tuple(f8(x) for x in source_fixed_length_tuple)
  198. assert a == (False, True, False, True)
  199. source_str = 'abbc'
  200. b = tuple('s:' + x for x in source_str)
  201. assert b == ('s:a', 's:b', 's:b', 's:c')
  202. TUPLE: Final[Tuple[str, ...]] = ('x', 'y')
  203. def test_final_boxed_tuple() -> None:
  204. t = TUPLE
  205. assert t == ('x', 'y')