irbuild-glue-methods.test 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. # Test cases for glue methods.
  2. #
  3. # These are used when subclass method signature has a different representation
  4. # compared to the base class.
  5. [case testSubclassSpecialize2]
  6. class A:
  7. def foo(self, x: int) -> object:
  8. return str(x)
  9. class B(A):
  10. def foo(self, x: object) -> object:
  11. return x
  12. class C(B):
  13. def foo(self, x: object) -> int:
  14. return id(x)
  15. def use_a(x: A, y: int) -> object:
  16. return x.foo(y)
  17. def use_b(x: B, y: object) -> object:
  18. return x.foo(y)
  19. def use_c(x: C, y: object) -> int:
  20. return x.foo(y)
  21. [out]
  22. def A.foo(self, x):
  23. self :: __main__.A
  24. x :: int
  25. r0 :: str
  26. L0:
  27. r0 = CPyTagged_Str(x)
  28. return r0
  29. def B.foo(self, x):
  30. self :: __main__.B
  31. x :: object
  32. L0:
  33. return x
  34. def B.foo__A_glue(self, x):
  35. self :: __main__.B
  36. x :: int
  37. r0, r1 :: object
  38. L0:
  39. r0 = box(int, x)
  40. r1 = B.foo(self, r0)
  41. return r1
  42. def C.foo(self, x):
  43. self :: __main__.C
  44. x :: object
  45. r0 :: int
  46. L0:
  47. r0 = CPyTagged_Id(x)
  48. return r0
  49. def C.foo__B_glue(self, x):
  50. self :: __main__.C
  51. x :: object
  52. r0 :: int
  53. r1 :: object
  54. L0:
  55. r0 = C.foo(self, x)
  56. r1 = box(int, r0)
  57. return r1
  58. def C.foo__A_glue(self, x):
  59. self :: __main__.C
  60. x :: int
  61. r0 :: object
  62. r1 :: int
  63. r2 :: object
  64. L0:
  65. r0 = box(int, x)
  66. r1 = C.foo(self, r0)
  67. r2 = box(int, r1)
  68. return r2
  69. def use_a(x, y):
  70. x :: __main__.A
  71. y :: int
  72. r0 :: object
  73. L0:
  74. r0 = x.foo(y)
  75. return r0
  76. def use_b(x, y):
  77. x :: __main__.B
  78. y, r0 :: object
  79. L0:
  80. r0 = x.foo(y)
  81. return r0
  82. def use_c(x, y):
  83. x :: __main__.C
  84. y :: object
  85. r0 :: int
  86. L0:
  87. r0 = x.foo(y)
  88. return r0
  89. [case testPropertyDerivedGen]
  90. from typing import Callable
  91. class BaseProperty:
  92. @property
  93. def value(self) -> object:
  94. return self._incrementer
  95. @property
  96. def bad_value(self) -> object:
  97. return self._incrementer
  98. @property
  99. def next(self) -> BaseProperty:
  100. return BaseProperty(self._incrementer + 1)
  101. def __init__(self, value: int) -> None:
  102. self._incrementer = value
  103. class DerivedProperty(BaseProperty):
  104. @property
  105. def value(self) -> int:
  106. return self._incrementer
  107. @property
  108. def bad_value(self) -> object:
  109. return self._incrementer
  110. @property
  111. def next(self) -> DerivedProperty:
  112. return DerivedProperty(self._incr_func, self._incr_func(self.value))
  113. def __init__(self, incr_func: Callable[[int], int], value: int) -> None:
  114. BaseProperty.__init__(self, value)
  115. self._incr_func = incr_func
  116. class AgainProperty(DerivedProperty):
  117. @property
  118. def next(self) -> AgainProperty:
  119. return AgainProperty(self._incr_func, self._incr_func(self._incr_func(self.value)))
  120. @property
  121. def bad_value(self) -> int:
  122. return self._incrementer
  123. [out]
  124. def BaseProperty.value(self):
  125. self :: __main__.BaseProperty
  126. r0 :: int
  127. r1 :: object
  128. L0:
  129. r0 = self._incrementer
  130. r1 = box(int, r0)
  131. return r1
  132. def BaseProperty.bad_value(self):
  133. self :: __main__.BaseProperty
  134. r0 :: int
  135. r1 :: object
  136. L0:
  137. r0 = self._incrementer
  138. r1 = box(int, r0)
  139. return r1
  140. def BaseProperty.next(self):
  141. self :: __main__.BaseProperty
  142. r0, r1 :: int
  143. r2 :: __main__.BaseProperty
  144. L0:
  145. r0 = borrow self._incrementer
  146. r1 = CPyTagged_Add(r0, 2)
  147. keep_alive self
  148. r2 = BaseProperty(r1)
  149. return r2
  150. def BaseProperty.__init__(self, value):
  151. self :: __main__.BaseProperty
  152. value :: int
  153. L0:
  154. self._incrementer = value
  155. return 1
  156. def DerivedProperty.value(self):
  157. self :: __main__.DerivedProperty
  158. r0 :: int
  159. L0:
  160. r0 = self._incrementer
  161. return r0
  162. def DerivedProperty.value__BaseProperty_glue(__mypyc_self__):
  163. __mypyc_self__ :: __main__.DerivedProperty
  164. r0 :: int
  165. r1 :: object
  166. L0:
  167. r0 = __mypyc_self__.value
  168. r1 = box(int, r0)
  169. return r1
  170. def DerivedProperty.bad_value(self):
  171. self :: __main__.DerivedProperty
  172. r0 :: int
  173. r1 :: object
  174. L0:
  175. r0 = self._incrementer
  176. r1 = box(int, r0)
  177. return r1
  178. def DerivedProperty.next(self):
  179. self :: __main__.DerivedProperty
  180. r0 :: object
  181. r1 :: int
  182. r2, r3, r4 :: object
  183. r5 :: int
  184. r6 :: __main__.DerivedProperty
  185. L0:
  186. r0 = self._incr_func
  187. r1 = self.value
  188. r2 = self._incr_func
  189. r3 = box(int, r1)
  190. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  191. r5 = unbox(int, r4)
  192. r6 = DerivedProperty(r0, r5)
  193. return r6
  194. def DerivedProperty.next__BaseProperty_glue(__mypyc_self__):
  195. __mypyc_self__, r0 :: __main__.DerivedProperty
  196. L0:
  197. r0 = __mypyc_self__.next
  198. return r0
  199. def DerivedProperty.__init__(self, incr_func, value):
  200. self :: __main__.DerivedProperty
  201. incr_func :: object
  202. value :: int
  203. r0 :: None
  204. L0:
  205. r0 = BaseProperty.__init__(self, value)
  206. self._incr_func = incr_func
  207. return 1
  208. def AgainProperty.next(self):
  209. self :: __main__.AgainProperty
  210. r0 :: object
  211. r1 :: int
  212. r2, r3, r4 :: object
  213. r5 :: int
  214. r6, r7, r8 :: object
  215. r9 :: int
  216. r10 :: __main__.AgainProperty
  217. L0:
  218. r0 = self._incr_func
  219. r1 = self.value
  220. r2 = self._incr_func
  221. r3 = box(int, r1)
  222. r4 = PyObject_CallFunctionObjArgs(r2, r3, 0)
  223. r5 = unbox(int, r4)
  224. r6 = self._incr_func
  225. r7 = box(int, r5)
  226. r8 = PyObject_CallFunctionObjArgs(r6, r7, 0)
  227. r9 = unbox(int, r8)
  228. r10 = AgainProperty(r0, r9)
  229. return r10
  230. def AgainProperty.next__DerivedProperty_glue(__mypyc_self__):
  231. __mypyc_self__, r0 :: __main__.AgainProperty
  232. L0:
  233. r0 = __mypyc_self__.next
  234. return r0
  235. def AgainProperty.next__BaseProperty_glue(__mypyc_self__):
  236. __mypyc_self__, r0 :: __main__.AgainProperty
  237. L0:
  238. r0 = __mypyc_self__.next
  239. return r0
  240. def AgainProperty.bad_value(self):
  241. self :: __main__.AgainProperty
  242. r0 :: int
  243. L0:
  244. r0 = self._incrementer
  245. return r0
  246. def AgainProperty.bad_value__DerivedProperty_glue(__mypyc_self__):
  247. __mypyc_self__ :: __main__.AgainProperty
  248. r0 :: int
  249. r1 :: object
  250. L0:
  251. r0 = __mypyc_self__.bad_value
  252. r1 = box(int, r0)
  253. return r1
  254. def AgainProperty.bad_value__BaseProperty_glue(__mypyc_self__):
  255. __mypyc_self__ :: __main__.AgainProperty
  256. r0 :: int
  257. r1 :: object
  258. L0:
  259. r0 = __mypyc_self__.bad_value
  260. r1 = box(int, r0)
  261. return r1
  262. [case testPropertyTraitSubclassing]
  263. from mypy_extensions import trait
  264. @trait
  265. class SubclassedTrait:
  266. @property
  267. def this(self) -> SubclassedTrait:
  268. return self
  269. @property
  270. def boxed(self) -> object:
  271. return 3
  272. class DerivingObject(SubclassedTrait):
  273. @property
  274. def this(self) -> DerivingObject:
  275. return self
  276. @property
  277. def boxed(self) -> int:
  278. return 5
  279. [out]
  280. def SubclassedTrait.this(self):
  281. self :: __main__.SubclassedTrait
  282. L0:
  283. return self
  284. def SubclassedTrait.boxed(self):
  285. self :: __main__.SubclassedTrait
  286. r0 :: object
  287. L0:
  288. r0 = object 3
  289. return r0
  290. def DerivingObject.this(self):
  291. self :: __main__.DerivingObject
  292. L0:
  293. return self
  294. def DerivingObject.this__SubclassedTrait_glue(__mypyc_self__):
  295. __mypyc_self__, r0 :: __main__.DerivingObject
  296. L0:
  297. r0 = __mypyc_self__.this
  298. return r0
  299. def DerivingObject.boxed(self):
  300. self :: __main__.DerivingObject
  301. L0:
  302. return 10
  303. def DerivingObject.boxed__SubclassedTrait_glue(__mypyc_self__):
  304. __mypyc_self__ :: __main__.DerivingObject
  305. r0 :: int
  306. r1 :: object
  307. L0:
  308. r0 = __mypyc_self__.boxed
  309. r1 = box(int, r0)
  310. return r1
  311. [case testI64GlueWithExtraDefaultArg]
  312. from mypy_extensions import i64
  313. class C:
  314. def f(self) -> None: pass
  315. class D(C):
  316. def f(self, x: i64 = 44) -> None: pass
  317. [out]
  318. def C.f(self):
  319. self :: __main__.C
  320. L0:
  321. return 1
  322. def D.f(self, x, __bitmap):
  323. self :: __main__.D
  324. x :: i64
  325. __bitmap, r0 :: u32
  326. r1 :: bit
  327. L0:
  328. r0 = __bitmap & 1
  329. r1 = r0 == 0
  330. if r1 goto L1 else goto L2 :: bool
  331. L1:
  332. x = 44
  333. L2:
  334. return 1
  335. def D.f__C_glue(self):
  336. self :: __main__.D
  337. r0 :: None
  338. L0:
  339. r0 = D.f(self, 0, 0)
  340. return r0
  341. [case testI64GlueWithSecondDefaultArg]
  342. from mypy_extensions import i64
  343. class C:
  344. def f(self, x: i64 = 11) -> None: pass
  345. class D(C):
  346. def f(self, x: i64 = 12, y: i64 = 13) -> None: pass
  347. [out]
  348. def C.f(self, x, __bitmap):
  349. self :: __main__.C
  350. x :: i64
  351. __bitmap, r0 :: u32
  352. r1 :: bit
  353. L0:
  354. r0 = __bitmap & 1
  355. r1 = r0 == 0
  356. if r1 goto L1 else goto L2 :: bool
  357. L1:
  358. x = 11
  359. L2:
  360. return 1
  361. def D.f(self, x, y, __bitmap):
  362. self :: __main__.D
  363. x, y :: i64
  364. __bitmap, r0 :: u32
  365. r1 :: bit
  366. r2 :: u32
  367. r3 :: bit
  368. L0:
  369. r0 = __bitmap & 1
  370. r1 = r0 == 0
  371. if r1 goto L1 else goto L2 :: bool
  372. L1:
  373. x = 12
  374. L2:
  375. r2 = __bitmap & 2
  376. r3 = r2 == 0
  377. if r3 goto L3 else goto L4 :: bool
  378. L3:
  379. y = 13
  380. L4:
  381. return 1
  382. def D.f__C_glue(self, x, __bitmap):
  383. self :: __main__.D
  384. x :: i64
  385. __bitmap :: u32
  386. r0 :: None
  387. L0:
  388. r0 = D.f(self, x, 0, __bitmap)
  389. return r0
  390. [case testI64GlueWithInvalidOverride]
  391. from mypy_extensions import i64
  392. class C:
  393. def f(self, x: i64, y: i64 = 5) -> None: pass
  394. def ff(self, x: int) -> None: pass
  395. class CC(C):
  396. def f(self, x: i64 = 12, y: i64 = 5) -> None: pass # Line 7
  397. def ff(self, x: int = 12) -> None: pass
  398. class D:
  399. def f(self, x: int) -> None: pass
  400. class DD(D):
  401. def f(self, x: i64) -> None: pass # Line 13
  402. class E:
  403. def f(self, x: i64) -> None: pass
  404. class EE(E):
  405. def f(self, x: int) -> None: pass # Line 18
  406. [out]
  407. main:7: error: An argument with type "i64" cannot be given a default value in a method override
  408. main:13: error: Incompatible argument type "i64" (base class has type "int")
  409. main:18: error: Incompatible argument type "int" (base class has type "i64")