run-tuples.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. class FuncIR: pass
  84. StealsDescription = Union[bool, List[bool]]
  85. class Record(NamedTuple):
  86. st_mtime: float
  87. st_size: int
  88. is_borrowed: bool
  89. hash: str
  90. python_path: Tuple[str, ...]
  91. type: 'ClassIR'
  92. method: FuncIR
  93. shadow_method: Optional[FuncIR]
  94. classes: Dict[str, 'ClassIR']
  95. steals: StealsDescription
  96. ordering: Optional[List[int]]
  97. extra_int_constants: List[Tuple[int]]
  98. # Make sure mypyc loads the annotation string for this forward reference.
  99. # Ref: https://github.com/mypyc/mypyc/issues/938
  100. class ClassIR: pass
  101. [file driver.py]
  102. from typing import ForwardRef, Optional
  103. from native import ClassIR, FuncIR, Record
  104. assert Record.__annotations__ == {
  105. 'st_mtime': float,
  106. 'st_size': int,
  107. 'is_borrowed': bool,
  108. 'hash': str,
  109. 'python_path': tuple,
  110. 'type': ForwardRef('ClassIR'),
  111. 'method': FuncIR,
  112. 'shadow_method': type,
  113. 'classes': dict,
  114. 'steals': type,
  115. 'ordering': type,
  116. 'extra_int_constants': list,
  117. }, Record.__annotations__
  118. [case testTupleOps]
  119. from typing import Tuple, List, Any, Optional
  120. from typing_extensions import Final
  121. def f() -> Tuple[()]:
  122. return ()
  123. def test_empty_tuple() -> None:
  124. assert f() == ()
  125. def f2() -> Any:
  126. return ()
  127. def test_empty_tuple_with_any_type():
  128. assert f2() == ()
  129. def f3() -> int:
  130. x = (False, 1)
  131. return x[1]
  132. def test_new_tuple() -> None:
  133. assert f3() == 1
  134. def f4(y: int) -> int:
  135. x = (False, y)
  136. return x[1]
  137. def test_new_tuple_boxed_int() -> None:
  138. big_number = 1208925819614629174706176
  139. assert f4(big_number) == big_number
  140. def f5(x: List[int]) -> int:
  141. return tuple(x)[1]
  142. def test_sequence_tuple() -> None:
  143. assert f5([1,2,3,4]) == 2
  144. def f6(x: List[int]) -> int:
  145. return len(tuple(x))
  146. def test_sequence_tuple_len() -> None:
  147. assert f6([1,2,3,4]) == 4
  148. def f7(x: List[Tuple[int, int]]) -> int:
  149. a, b = x[0]
  150. return a + b
  151. def test_unbox_tuple() -> None:
  152. assert f7([(5, 6)]) == 11
  153. # Test that order is irrelevant to unions. Really I only care that this builds.
  154. class A:
  155. pass
  156. def lol() -> A:
  157. return A()
  158. def foo(x: bool, y: bool) -> Tuple[Optional[A], bool]:
  159. z = lol()
  160. return None if y else z, x
  161. def test_slicing() -> None:
  162. # Use dummy adds to avoid constant folding
  163. zero = int()
  164. two = zero + 2
  165. s: Tuple[str, ...] = ("f", "o", "o", "b", "a", "r")
  166. assert s[two:] == ("o", "b", "a", "r")
  167. assert s[:two] == ("f", "o")
  168. assert s[two:-two] == ("o", "b")
  169. assert s[two:two] == ()
  170. assert s[two:two + 1] == ("o",)
  171. assert s[-two:] == ("a", "r")
  172. assert s[:-two] == ("f", "o", "o", "b")
  173. assert s[:] == ("f", "o", "o", "b", "a", "r")
  174. assert s[two:333] == ("o", "b", "a", "r")
  175. assert s[333:two] == ()
  176. assert s[two:-333] == ()
  177. assert s[-333:two] == ("f", "o")
  178. long_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
  179. assert s[1:long_int] == ("o", "o", "b", "a", "r")
  180. assert s[long_int:] == ()
  181. assert s[-long_int:-1] == ("f", "o", "o", "b", "a")
  182. def f8(val: int) -> bool:
  183. return val % 2 == 0
  184. def test_sequence_generator() -> None:
  185. source_list = [1, 2, 3]
  186. a = tuple(f8(x) for x in source_list)
  187. assert a == (False, True, False)
  188. source_tuple: Tuple[int, ...] = (1, 2, 3)
  189. a = tuple(f8(x) for x in source_tuple)
  190. assert a == (False, True, False)
  191. source_fixed_length_tuple = (1, 2, 3, 4)
  192. a = tuple(f8(x) for x in source_fixed_length_tuple)
  193. assert a == (False, True, False, True)
  194. source_str = 'abbc'
  195. b = tuple('s:' + x for x in source_str)
  196. assert b == ('s:a', 's:b', 's:b', 's:c')
  197. TUPLE: Final[Tuple[str, ...]] = ('x', 'y')
  198. def test_final_boxed_tuple() -> None:
  199. t = TUPLE
  200. assert t == ('x', 'y')