| 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 |
|
| 6 |
[case testSubclassSpecialize2] |
| 7 |
class A: |
| 8 |
def foo(self, x: int) -> object: |
| 9 |
return str(x) |
| 10 |
class B(A): |
| 11 |
def foo(self, x: object) -> object: |
| 12 |
return x |
| 13 |
class C(B): |
| 14 |
def foo(self, x: object) -> int: |
| 15 |
return id(x) |
| 16 |
|
| 17 |
def use_a(x: A, y: int) -> object: |
| 18 |
return x.foo(y) |
| 19 |
|
| 20 |
def use_b(x: B, y: object) -> object: |
| 21 |
return x.foo(y) |
| 22 |
|
| 23 |
def use_c(x: C, y: object) -> int: |
| 24 |
return x.foo(y) |
| 25 |
[out] |
| 26 |
def A.foo(self, x): |
| 27 |
self :: __main__.A |
| 28 |
x :: int |
| 29 |
r0 :: str |
| 30 |
L0: |
| 31 |
r0 = CPyTagged_Str(x) |
| 32 |
return r0 |
| 33 |
def B.foo(self, x): |
| 34 |
self :: __main__.B |
| 35 |
x :: object |
| 36 |
L0: |
| 37 |
return x |
| 38 |
def B.foo__A_glue(self, x): |
| 39 |
self :: __main__.B |
| 40 |
x :: int |
| 41 |
r0, r1 :: object |
| 42 |
L0: |
| 43 |
r0 = box(int, x) |
| 44 |
r1 = B.foo(self, r0) |
| 45 |
return r1 |
| 46 |
def C.foo(self, x): |
| 47 |
self :: __main__.C |
| 48 |
x :: object |
| 49 |
r0 :: int |
| 50 |
L0: |
| 51 |
r0 = CPyTagged_Id(x) |
| 52 |
return r0 |
| 53 |
def C.foo__B_glue(self, x): |
| 54 |
self :: __main__.C |
| 55 |
x :: object |
| 56 |
r0 :: int |
| 57 |
r1 :: object |
| 58 |
L0: |
| 59 |
r0 = C.foo(self, x) |
| 60 |
r1 = box(int, r0) |
| 61 |
return r1 |
| 62 |
def C.foo__A_glue(self, x): |
| 63 |
self :: __main__.C |
| 64 |
x :: int |
| 65 |
r0 :: object |
| 66 |
r1 :: int |
| 67 |
r2 :: object |
| 68 |
L0: |
| 69 |
r0 = box(int, x) |
| 70 |
r1 = C.foo(self, r0) |
| 71 |
r2 = box(int, r1) |
| 72 |
return r2 |
| 73 |
def use_a(x, y): |
| 74 |
x :: __main__.A |
| 75 |
y :: int |
| 76 |
r0 :: object |
| 77 |
L0: |
| 78 |
r0 = x.foo(y) |
| 79 |
return r0 |
| 80 |
def use_b(x, y): |
| 81 |
x :: __main__.B |
| 82 |
y, r0 :: object |
| 83 |
L0: |
| 84 |
r0 = x.foo(y) |
| 85 |
return r0 |
| 86 |
def use_c(x, y): |
| 87 |
x :: __main__.C |
| 88 |
y :: object |
| 89 |
r0 :: int |
| 90 |
L0: |
| 91 |
r0 = x.foo(y) |
| 92 |
return r0 |
| 93 |
|
| 94 |
[case testPropertyDerivedGen] |
| 95 |
from typing import Callable |
| 96 |
class BaseProperty: |
| 97 |
@property |
| 98 |
def value(self) -> object: |
| 99 |
return self._incrementer |
| 100 |
|
| 101 |
@property |
| 102 |
def bad_value(self) -> object: |
| 103 |
return self._incrementer |
| 104 |
|
| 105 |
@property |
| 106 |
def next(self) -> BaseProperty: |
| 107 |
return BaseProperty(self._incrementer + 1) |
| 108 |
|
| 109 |
def __init__(self, value: int) -> None: |
| 110 |
self._incrementer = value |
| 111 |
|
| 112 |
class DerivedProperty(BaseProperty): |
| 113 |
@property |
| 114 |
def value(self) -> int: |
| 115 |
return self._incrementer |
| 116 |
|
| 117 |
@property |
| 118 |
def bad_value(self) -> object: |
| 119 |
return self._incrementer |
| 120 |
|
| 121 |
@property |
| 122 |
def next(self) -> DerivedProperty: |
| 123 |
return DerivedProperty(self._incr_func, self._incr_func(self.value)) |
| 124 |
|
| 125 |
def __init__(self, incr_func: Callable[[int], int], value: int) -> None: |
| 126 |
BaseProperty.__init__(self, value) |
| 127 |
self._incr_func = incr_func |
| 128 |
|
| 129 |
|
| 130 |
class AgainProperty(DerivedProperty): |
| 131 |
@property |
| 132 |
def next(self) -> AgainProperty: |
| 133 |
return AgainProperty(self._incr_func, self._incr_func(self._incr_func(self.value))) |
| 134 |
|
| 135 |
@property |
| 136 |
def bad_value(self) -> int: |
| 137 |
return self._incrementer |
| 138 |
[out] |
| 139 |
def BaseProperty.value(self): |
| 140 |
self :: __main__.BaseProperty |
| 141 |
r0 :: int |
| 142 |
r1 :: object |
| 143 |
L0: |
| 144 |
r0 = self._incrementer |
| 145 |
r1 = box(int, r0) |
| 146 |
return r1 |
| 147 |
def BaseProperty.bad_value(self): |
| 148 |
self :: __main__.BaseProperty |
| 149 |
r0 :: int |
| 150 |
r1 :: object |
| 151 |
L0: |
| 152 |
r0 = self._incrementer |
| 153 |
r1 = box(int, r0) |
| 154 |
return r1 |
| 155 |
def BaseProperty.next(self): |
| 156 |
self :: __main__.BaseProperty |
| 157 |
r0, r1 :: int |
| 158 |
r2 :: __main__.BaseProperty |
| 159 |
L0: |
| 160 |
r0 = borrow self._incrementer |
| 161 |
r1 = CPyTagged_Add(r0, 2) |
| 162 |
keep_alive self |
| 163 |
r2 = BaseProperty(r1) |
| 164 |
return r2 |
| 165 |
def BaseProperty.__init__(self, value): |
| 166 |
self :: __main__.BaseProperty |
| 167 |
value :: int |
| 168 |
L0: |
| 169 |
self._incrementer = value |
| 170 |
return 1 |
| 171 |
def DerivedProperty.value(self): |
| 172 |
self :: __main__.DerivedProperty |
| 173 |
r0 :: int |
| 174 |
L0: |
| 175 |
r0 = self._incrementer |
| 176 |
return r0 |
| 177 |
def DerivedProperty.value__BaseProperty_glue(__mypyc_self__): |
| 178 |
__mypyc_self__ :: __main__.DerivedProperty |
| 179 |
r0 :: int |
| 180 |
r1 :: object |
| 181 |
L0: |
| 182 |
r0 = __mypyc_self__.value |
| 183 |
r1 = box(int, r0) |
| 184 |
return r1 |
| 185 |
def DerivedProperty.bad_value(self): |
| 186 |
self :: __main__.DerivedProperty |
| 187 |
r0 :: int |
| 188 |
r1 :: object |
| 189 |
L0: |
| 190 |
r0 = self._incrementer |
| 191 |
r1 = box(int, r0) |
| 192 |
return r1 |
| 193 |
def DerivedProperty.next(self): |
| 194 |
self :: __main__.DerivedProperty |
| 195 |
r0 :: object |
| 196 |
r1 :: int |
| 197 |
r2, r3 :: object |
| 198 |
r4 :: object[1] |
| 199 |
r5 :: object_ptr |
| 200 |
r6 :: object |
| 201 |
r7 :: int |
| 202 |
r8 :: __main__.DerivedProperty |
| 203 |
L0: |
| 204 |
r0 = self._incr_func |
| 205 |
r1 = self.value |
| 206 |
r2 = self._incr_func |
| 207 |
r3 = box(int, r1) |
| 208 |
r4 = [r3] |
| 209 |
r5 = load_address r4 |
| 210 |
r6 = PyObject_Vectorcall(r2, r5, 1, 0) |
| 211 |
keep_alive r3 |
| 212 |
r7 = unbox(int, r6) |
| 213 |
r8 = DerivedProperty(r0, r7) |
| 214 |
return r8 |
| 215 |
def DerivedProperty.next__BaseProperty_glue(__mypyc_self__): |
| 216 |
__mypyc_self__, r0 :: __main__.DerivedProperty |
| 217 |
L0: |
| 218 |
r0 = __mypyc_self__.next |
| 219 |
return r0 |
| 220 |
def DerivedProperty.__init__(self, incr_func, value): |
| 221 |
self :: __main__.DerivedProperty |
| 222 |
incr_func :: object |
| 223 |
value :: int |
| 224 |
r0 :: None |
| 225 |
L0: |
| 226 |
r0 = BaseProperty.__init__(self, value) |
| 227 |
self._incr_func = incr_func |
| 228 |
return 1 |
| 229 |
def AgainProperty.next(self): |
| 230 |
self :: __main__.AgainProperty |
| 231 |
r0 :: object |
| 232 |
r1 :: int |
| 233 |
r2, r3 :: object |
| 234 |
r4 :: object[1] |
| 235 |
r5 :: object_ptr |
| 236 |
r6 :: object |
| 237 |
r7 :: int |
| 238 |
r8, r9 :: object |
| 239 |
r10 :: object[1] |
| 240 |
r11 :: object_ptr |
| 241 |
r12 :: object |
| 242 |
r13 :: int |
| 243 |
r14 :: __main__.AgainProperty |
| 244 |
L0: |
| 245 |
r0 = self._incr_func |
| 246 |
r1 = self.value |
| 247 |
r2 = self._incr_func |
| 248 |
r3 = box(int, r1) |
| 249 |
r4 = [r3] |
| 250 |
r5 = load_address r4 |
| 251 |
r6 = PyObject_Vectorcall(r2, r5, 1, 0) |
| 252 |
keep_alive r3 |
| 253 |
r7 = unbox(int, r6) |
| 254 |
r8 = self._incr_func |
| 255 |
r9 = box(int, r7) |
| 256 |
r10 = [r9] |
| 257 |
r11 = load_address r10 |
| 258 |
r12 = PyObject_Vectorcall(r8, r11, 1, 0) |
| 259 |
keep_alive r9 |
| 260 |
r13 = unbox(int, r12) |
| 261 |
r14 = AgainProperty(r0, r13) |
| 262 |
return r14 |
| 263 |
def AgainProperty.next__DerivedProperty_glue(__mypyc_self__): |
| 264 |
__mypyc_self__, r0 :: __main__.AgainProperty |
| 265 |
L0: |
| 266 |
r0 = __mypyc_self__.next |
| 267 |
return r0 |
| 268 |
def AgainProperty.next__BaseProperty_glue(__mypyc_self__): |
| 269 |
__mypyc_self__, r0 :: __main__.AgainProperty |
| 270 |
L0: |
| 271 |
r0 = __mypyc_self__.next |
| 272 |
return r0 |
| 273 |
def AgainProperty.bad_value(self): |
| 274 |
self :: __main__.AgainProperty |
| 275 |
r0 :: int |
| 276 |
L0: |
| 277 |
r0 = self._incrementer |
| 278 |
return r0 |
| 279 |
def AgainProperty.bad_value__DerivedProperty_glue(__mypyc_self__): |
| 280 |
__mypyc_self__ :: __main__.AgainProperty |
| 281 |
r0 :: int |
| 282 |
r1 :: object |
| 283 |
L0: |
| 284 |
r0 = __mypyc_self__.bad_value |
| 285 |
r1 = box(int, r0) |
| 286 |
return r1 |
| 287 |
def AgainProperty.bad_value__BaseProperty_glue(__mypyc_self__): |
| 288 |
__mypyc_self__ :: __main__.AgainProperty |
| 289 |
r0 :: int |
| 290 |
r1 :: object |
| 291 |
L0: |
| 292 |
r0 = __mypyc_self__.bad_value |
| 293 |
r1 = box(int, r0) |
| 294 |
return r1 |
| 295 |
|
| 296 |
[case testPropertyTraitSubclassing] |
| 297 |
from mypy_extensions import trait |
| 298 |
@trait |
| 299 |
class SubclassedTrait: |
| 300 |
@property |
| 301 |
def this(self) -> SubclassedTrait: |
| 302 |
return self |
| 303 |
|
| 304 |
@property |
| 305 |
def boxed(self) -> object: |
| 306 |
return 3 |
| 307 |
|
| 308 |
class DerivingObject(SubclassedTrait): |
| 309 |
@property |
| 310 |
def this(self) -> DerivingObject: |
| 311 |
return self |
| 312 |
|
| 313 |
@property |
| 314 |
def boxed(self) -> int: |
| 315 |
return 5 |
| 316 |
[out] |
| 317 |
def SubclassedTrait.this(self): |
| 318 |
self :: __main__.SubclassedTrait |
| 319 |
L0: |
| 320 |
return self |
| 321 |
def SubclassedTrait.boxed(self): |
| 322 |
self :: __main__.SubclassedTrait |
| 323 |
r0 :: object |
| 324 |
L0: |
| 325 |
r0 = object 3 |
| 326 |
return r0 |
| 327 |
def DerivingObject.this(self): |
| 328 |
self :: __main__.DerivingObject |
| 329 |
L0: |
| 330 |
return self |
| 331 |
def DerivingObject.this__SubclassedTrait_glue(__mypyc_self__): |
| 332 |
__mypyc_self__, r0 :: __main__.DerivingObject |
| 333 |
L0: |
| 334 |
r0 = __mypyc_self__.this |
| 335 |
return r0 |
| 336 |
def DerivingObject.boxed(self): |
| 337 |
self :: __main__.DerivingObject |
| 338 |
L0: |
| 339 |
return 10 |
| 340 |
def DerivingObject.boxed__SubclassedTrait_glue(__mypyc_self__): |
| 341 |
__mypyc_self__ :: __main__.DerivingObject |
| 342 |
r0 :: int |
| 343 |
r1 :: object |
| 344 |
L0: |
| 345 |
r0 = __mypyc_self__.boxed |
| 346 |
r1 = box(int, r0) |
| 347 |
return r1 |
| 348 |
|
| 349 |
[case testI64GlueWithExtraDefaultArg] |
| 350 |
from mypy_extensions import i64 |
| 351 |
|
| 352 |
class C: |
| 353 |
def f(self) -> None: pass |
| 354 |
|
| 355 |
class D(C): |
| 356 |
def f(self, x: i64 = 44) -> None: pass |
| 357 |
[out] |
| 358 |
def C.f(self): |
| 359 |
self :: __main__.C |
| 360 |
L0: |
| 361 |
return 1 |
| 362 |
def D.f(self, x, __bitmap): |
| 363 |
self :: __main__.D |
| 364 |
x :: i64 |
| 365 |
__bitmap, r0 :: u32 |
| 366 |
r1 :: bit |
| 367 |
L0: |
| 368 |
r0 = __bitmap & 1 |
| 369 |
r1 = r0 == 0 |
| 370 |
if r1 goto L1 else goto L2 :: bool |
| 371 |
L1: |
| 372 |
x = 44 |
| 373 |
L2: |
| 374 |
return 1 |
| 375 |
def D.f__C_glue(self): |
| 376 |
self :: __main__.D |
| 377 |
r0 :: None |
| 378 |
L0: |
| 379 |
r0 = D.f(self, 0, 0) |
| 380 |
return r0 |
| 381 |
|
| 382 |
[case testI64GlueWithSecondDefaultArg] |
| 383 |
from mypy_extensions import i64 |
| 384 |
|
| 385 |
class C: |
| 386 |
def f(self, x: i64 = 11) -> None: pass |
| 387 |
class D(C): |
| 388 |
def f(self, x: i64 = 12, y: i64 = 13) -> None: pass |
| 389 |
[out] |
| 390 |
def C.f(self, x, __bitmap): |
| 391 |
self :: __main__.C |
| 392 |
x :: i64 |
| 393 |
__bitmap, r0 :: u32 |
| 394 |
r1 :: bit |
| 395 |
L0: |
| 396 |
r0 = __bitmap & 1 |
| 397 |
r1 = r0 == 0 |
| 398 |
if r1 goto L1 else goto L2 :: bool |
| 399 |
L1: |
| 400 |
x = 11 |
| 401 |
L2: |
| 402 |
return 1 |
| 403 |
def D.f(self, x, y, __bitmap): |
| 404 |
self :: __main__.D |
| 405 |
x, y :: i64 |
| 406 |
__bitmap, r0 :: u32 |
| 407 |
r1 :: bit |
| 408 |
r2 :: u32 |
| 409 |
r3 :: bit |
| 410 |
L0: |
| 411 |
r0 = __bitmap & 1 |
| 412 |
r1 = r0 == 0 |
| 413 |
if r1 goto L1 else goto L2 :: bool |
| 414 |
L1: |
| 415 |
x = 12 |
| 416 |
L2: |
| 417 |
r2 = __bitmap & 2 |
| 418 |
r3 = r2 == 0 |
| 419 |
if r3 goto L3 else goto L4 :: bool |
| 420 |
L3: |
| 421 |
y = 13 |
| 422 |
L4: |
| 423 |
return 1 |
| 424 |
def D.f__C_glue(self, x, __bitmap): |
| 425 |
self :: __main__.D |
| 426 |
x :: i64 |
| 427 |
__bitmap :: u32 |
| 428 |
r0 :: None |
| 429 |
L0: |
| 430 |
r0 = D.f(self, x, 0, __bitmap) |
| 431 |
return r0 |
| 432 |
|
| 433 |
[case testI64GlueWithInvalidOverride] |
| 434 |
from mypy_extensions import i64 |
| 435 |
|
| 436 |
class C: |
| 437 |
def f(self, x: i64, y: i64 = 5) -> None: pass |
| 438 |
def ff(self, x: int) -> None: pass |
| 439 |
class CC(C): |
| 440 |
def f(self, x: i64 = 12, y: i64 = 5) -> None: pass # Line 7 |
| 441 |
def ff(self, x: int = 12) -> None: pass |
| 442 |
|
| 443 |
class D: |
| 444 |
def f(self, x: int) -> None: pass |
| 445 |
class DD(D): |
| 446 |
def f(self, x: i64) -> None: pass # Line 13 |
| 447 |
|
| 448 |
class E: |
| 449 |
def f(self, x: i64) -> None: pass |
| 450 |
class EE(E): |
| 451 |
def f(self, x: int) -> None: pass # Line 18 |
| 452 |
[out] |
| 453 |
main:7: error: An argument with type "i64" cannot be given a default value in a method override |
| 454 |
main:13: error: Incompatible argument type "i64" (base class has type "int") |
| 455 |
main:18: error: Incompatible argument type "int" (base class has type "i64") |
| 456 |
|
| 457 |
[case testPropertySetterShadowGlue] |
| 458 |
import mypy_extensions |
| 459 |
|
| 460 |
@mypy_extensions.mypyc_attr(allow_interpreted_subclasses=True) |
| 461 |
class Base: |
| 462 |
_x: str = "" |
| 463 |
|
| 464 |
@property |
| 465 |
def x(self) -> str: |
| 466 |
return self._x |
| 467 |
|
| 468 |
@x.setter |
| 469 |
def x(self, v: str) -> None: |
| 470 |
self._x = v |
| 471 |
[out] |
| 472 |
def Base.x(self): |
| 473 |
self :: __main__.Base |
| 474 |
r0 :: str |
| 475 |
L0: |
| 476 |
r0 = self._x |
| 477 |
return r0 |
| 478 |
def Base.x__Base_glue(__mypyc_self__): |
| 479 |
__mypyc_self__ :: __main__.Base |
| 480 |
r0 :: str |
| 481 |
r1 :: object |
| 482 |
r2 :: str |
| 483 |
L0: |
| 484 |
r0 = 'x' |
| 485 |
r1 = CPyObject_GetAttr(__mypyc_self__, r0) |
| 486 |
r2 = cast(str, r1) |
| 487 |
return r2 |
| 488 |
def Base.__mypyc_setter__x(self, v): |
| 489 |
self :: __main__.Base |
| 490 |
v :: str |
| 491 |
r0 :: bool |
| 492 |
L0: |
| 493 |
self._x = v; r0 = is_error |
| 494 |
return 1 |
| 495 |
def Base.__mypyc_setter__x__Base_glue(self, v): |
| 496 |
self :: __main__.Base |
| 497 |
v, r0 :: str |
| 498 |
r1 :: i32 |
| 499 |
r2 :: bit |
| 500 |
L0: |
| 501 |
r0 = 'x' |
| 502 |
r1 = PyObject_SetAttr(self, r0, v) |
| 503 |
r2 = r1 >= 0 :: signed |
| 504 |
return 1 |
| 505 |
def Base.__mypyc_defaults_setup(__mypyc_self__): |
| 506 |
__mypyc_self__ :: __main__.Base |
| 507 |
r0 :: str |
| 508 |
L0: |
| 509 |
r0 = '' |
| 510 |
__mypyc_self__._x = r0 |
| 511 |
return 1 |