blob: 2289f509c3ae4c19d38332d682d3c1470c9541c6 [file] [log] [blame] [edit]
[case testGetAttribute]
class A:
x: int
def f(a: A) -> int:
return a.x
[out]
def f(a):
a :: __main__.A
r0 :: int
L0:
r0 = a.x
return r0
[case testSetAttribute]
class A:
x: int
def f(a: A) -> None:
a.x = 1
[out]
def f(a):
a :: __main__.A
r0 :: short_int
r1 :: bool
r2 :: None
L0:
r0 = 1
a.x = r0; r1 = is_error
r2 = None
return r2
[case testUserClassInList]
class C:
x: int
def f() -> int:
c = C()
c.x = 5
a = [c]
d = a[0]
return d.x + 1
[out]
def f():
r0, c :: __main__.C
r1 :: short_int
r2 :: bool
r3, a :: list
r4 :: short_int
r5 :: object
r6, d :: __main__.C
r7 :: int
r8 :: short_int
r9 :: int
L0:
r0 = C()
c = r0
r1 = 5
c.x = r1; r2 = is_error
r3 = [c]
a = r3
r4 = 0
r5 = a[r4] :: list
r6 = cast(__main__.C, r5)
d = r6
r7 = d.x
r8 = 1
r9 = r7 + r8 :: int
return r9
[case testMethodCall]
class A:
def f(self, x: int, y: str) -> int:
return x + 10
def g(a: A) -> None:
a.f(1, 'hi')
[out]
def A.f(self, x, y):
self :: __main__.A
x :: int
y :: str
r0 :: short_int
r1 :: int
L0:
r0 = 10
r1 = x + r0 :: int
return r1
def g(a):
a :: __main__.A
r0 :: short_int
r1 :: str
r2 :: int
r3 :: None
L0:
r0 = 1
r1 = unicode_4 :: static ('hi')
r2 = a.f(r0, r1)
r3 = None
return r3
[case testForwardUse]
def g(a: A) -> int:
return a.n
class A:
n : int
[out]
def g(a):
a :: __main__.A
r0 :: int
L0:
r0 = a.n
return r0
[case testOptionalMember]
from typing import Optional
class Node:
next: Optional[Node]
def length(self) -> int:
if self.next is not None:
return 1 + self.next.length()
return 1
[out]
def Node.length(self):
self :: __main__.Node
r0 :: union[__main__.Node, None]
r1 :: None
r2 :: object
r3, r4 :: bool
r5 :: short_int
r6 :: union[__main__.Node, None]
r7 :: __main__.Node
r8, r9 :: int
r10 :: short_int
L0:
r0 = self.next
r1 = None
r2 = box(None, r1)
r3 = r0 is r2
r4 = !r3
if r4 goto L1 else goto L2 :: bool
L1:
r5 = 1
r6 = self.next
r7 = cast(__main__.Node, r6)
r8 = r7.length()
r9 = r5 + r8 :: int
return r9
L2:
r10 = 1
return r10
[case testSubclass]
class A:
def __init__(self) -> None:
self.x = 10
class B(A):
def __init__(self) -> None:
self.x = 20
self.y = 30
[out]
def A.__init__(self):
self :: __main__.A
r0 :: short_int
r1 :: bool
r2 :: None
L0:
r0 = 10
self.x = r0; r1 = is_error
r2 = None
return r2
def B.__init__(self):
self :: __main__.B
r0 :: short_int
r1 :: bool
r2 :: short_int
r3 :: bool
r4 :: None
L0:
r0 = 20
self.x = r0; r1 = is_error
r2 = 30
self.y = r2; r3 = is_error
r4 = None
return r4
[case testAttrLvalue]
class O(object):
def __init__(self) -> None:
self.x = 1
def increment(o: O) -> O:
o.x += 1
return o
[out]
def O.__init__(self):
self :: __main__.O
r0 :: short_int
r1 :: bool
r2 :: None
L0:
r0 = 1
self.x = r0; r1 = is_error
r2 = None
return r2
def increment(o):
o :: __main__.O
r0 :: int
r1 :: short_int
r2 :: int
r3 :: bool
L0:
r0 = o.x
r1 = 1
r2 = r0 += r1 :: int
o.x = r2; r3 = is_error
return o
[case testSubclassSpecialize2]
class A:
def foo(self, x: int) -> object:
return str(x)
class B(A):
def foo(self, x: object) -> object:
return x
class C(B):
def foo(self, x: object) -> int:
return id(x)
def use_a(x: A, y: int) -> object:
return x.foo(y)
def use_b(x: B, y: object) -> object:
return x.foo(y)
def use_c(x: C, y: object) -> int:
return x.foo(y)
[out]
def A.foo(self, x):
self :: __main__.A
x :: int
r0 :: object
r1 :: str
L0:
r0 = box(int, x)
r1 = str r0 :: object
return r1
def B.foo(self, x):
self :: __main__.B
x :: object
L0:
return x
def B.foo__A_glue(self, x):
self :: __main__.B
x :: int
r0, r1 :: object
L0:
r0 = box(int, x)
r1 = B.foo(self, r0)
return r1
def C.foo(self, x):
self :: __main__.C
x :: object
r0 :: int
L0:
r0 = id x :: object
return r0
def C.foo__B_glue(self, x):
self :: __main__.C
x :: object
r0 :: int
r1 :: object
L0:
r0 = C.foo(self, x)
r1 = box(int, r0)
return r1
def C.foo__A_glue(self, x):
self :: __main__.C
x :: int
r0 :: object
r1 :: int
r2 :: object
L0:
r0 = box(int, x)
r1 = C.foo(self, r0)
r2 = box(int, r1)
return r2
def use_a(x, y):
x :: __main__.A
y :: int
r0 :: object
L0:
r0 = x.foo(y)
return r0
def use_b(x, y):
x :: __main__.B
y, r0 :: object
L0:
r0 = x.foo(y)
return r0
def use_c(x, y):
x :: __main__.C
y :: object
r0 :: int
L0:
r0 = x.foo(y)
return r0
[case testSubclass_toplevel]
from typing import TypeVar, Generic
from mypy_extensions import trait
T = TypeVar('T')
class C:
pass
@trait
class S:
pass
class D(C, S, Generic[T]):
pass
[out]
def __top_level__():
r0, r1 :: object
r2 :: bool
r3 :: str
r4, r5, r6 :: object
r7 :: bool
r8 :: str
r9, r10 :: object
r11 :: dict
r12 :: str
r13 :: object
r14 :: str
r15 :: bool
r16 :: str
r17 :: object
r18 :: str
r19 :: bool
r20, r21 :: object
r22 :: bool
r23 :: str
r24, r25 :: object
r26 :: dict
r27 :: str
r28 :: object
r29 :: str
r30 :: bool
r31 :: str
r32 :: dict
r33 :: str
r34, r35 :: object
r36 :: dict
r37 :: str
r38 :: bool
r39 :: object
r40 :: str
r41, r42 :: object
r43 :: bool
r44 :: str
r45 :: tuple
r46 :: bool
r47 :: dict
r48 :: str
r49 :: bool
r50 :: object
r51 :: str
r52, r53 :: object
r54 :: str
r55 :: tuple
r56 :: bool
r57 :: dict
r58 :: str
r59 :: bool
r60, r61 :: object
r62 :: dict
r63 :: str
r64 :: object
r65 :: dict
r66 :: str
r67, r68 :: object
r69 :: tuple
r70 :: str
r71, r72 :: object
r73 :: bool
r74, r75 :: str
r76 :: tuple
r77 :: bool
r78 :: dict
r79 :: str
r80 :: bool
r81 :: None
L0:
r0 = builtins :: module
r1 = builtins.None :: object
r2 = r0 is not r1
if r2 goto L2 else goto L1 :: bool
L1:
r3 = unicode_0 :: static ('builtins')
r4 = import r3 :: str
builtins = r4 :: module
L2:
r5 = typing :: module
r6 = builtins.None :: object
r7 = r5 is not r6
if r7 goto L4 else goto L3 :: bool
L3:
r8 = unicode_1 :: static ('typing')
r9 = import r8 :: str
typing = r9 :: module
L4:
r10 = typing :: module
r11 = __main__.globals :: static
r12 = unicode_2 :: static ('TypeVar')
r13 = getattr r10, r12
r14 = unicode_2 :: static ('TypeVar')
r15 = r11.__setitem__(r14, r13) :: dict
r16 = unicode_3 :: static ('Generic')
r17 = getattr r10, r16
r18 = unicode_3 :: static ('Generic')
r19 = r11.__setitem__(r18, r17) :: dict
r20 = mypy_extensions :: module
r21 = builtins.None :: object
r22 = r20 is not r21
if r22 goto L6 else goto L5 :: bool
L5:
r23 = unicode_4 :: static ('mypy_extensions')
r24 = import r23 :: str
mypy_extensions = r24 :: module
L6:
r25 = mypy_extensions :: module
r26 = __main__.globals :: static
r27 = unicode_5 :: static ('trait')
r28 = getattr r25, r27
r29 = unicode_5 :: static ('trait')
r30 = r26.__setitem__(r29, r28) :: dict
r31 = unicode_6 :: static ('T')
r32 = __main__.globals :: static
r33 = unicode_2 :: static ('TypeVar')
r34 = r32[r33] :: dict
r35 = py_call(r34, r31)
r36 = __main__.globals :: static
r37 = unicode_6 :: static ('T')
r38 = r36.__setitem__(r37, r35) :: dict
r39 = <error> :: object
r40 = unicode_7 :: static ('__main__')
r41 = __main__.C_template :: type
r42 = pytype_from_template(r41, r39, r40)
r43 = C_trait_vtable_setup()
r44 = unicode_8 :: static ('__mypyc_attrs__')
r45 = () :: tuple
r46 = setattr r42, r44, r45
__main__.C = r42 :: type
r47 = __main__.globals :: static
r48 = unicode_9 :: static ('C')
r49 = r47.__setitem__(r48, r42) :: dict
r50 = <error> :: object
r51 = unicode_7 :: static ('__main__')
r52 = __main__.S_template :: type
r53 = pytype_from_template(r52, r50, r51)
r54 = unicode_8 :: static ('__mypyc_attrs__')
r55 = () :: tuple
r56 = setattr r53, r54, r55
__main__.S = r53 :: type
r57 = __main__.globals :: static
r58 = unicode_10 :: static ('S')
r59 = r57.__setitem__(r58, r53) :: dict
r60 = __main__.C :: type
r61 = __main__.S :: type
r62 = __main__.globals :: static
r63 = unicode_3 :: static ('Generic')
r64 = r62[r63] :: dict
r65 = __main__.globals :: static
r66 = unicode_6 :: static ('T')
r67 = r65[r66] :: dict
r68 = r64[r67] :: object
r69 = (r60, r61, r68) :: tuple
r70 = unicode_7 :: static ('__main__')
r71 = __main__.D_template :: type
r72 = pytype_from_template(r71, r69, r70)
r73 = D_trait_vtable_setup()
r74 = unicode_8 :: static ('__mypyc_attrs__')
r75 = unicode_11 :: static ('__dict__')
r76 = (r75) :: tuple
r77 = setattr r72, r74, r76
__main__.D = r72 :: type
r78 = __main__.globals :: static
r79 = unicode_12 :: static ('D')
r80 = r78.__setitem__(r79, r72) :: dict
r81 = None
return r81
[case testIsInstance]
class A: pass
class B(A): pass
def f(x: A) -> B:
if isinstance(x, B):
return x
return B()
[out]
def f(x):
x :: __main__.A
r0 :: object
r1 :: bool
r2, r3 :: __main__.B
L0:
r0 = __main__.B :: type
r1 = type_is x, r0
if r1 goto L1 else goto L2 :: bool
L1:
r2 = cast(__main__.B, x)
return r2
L2:
r3 = B()
return r3
[case testIsInstanceTuple]
from typing import Union
class R: pass
class A(R): pass
class B(R): pass
class C(R): pass
def f(x: R) -> Union[A, B]:
if isinstance(x, (A, B)):
return x
return A()
[out]
def f(x):
x :: __main__.R
r0 :: object
r1, r2 :: bool
r3 :: object
r4 :: bool
r5 :: union[__main__.A, __main__.B]
r6 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = type_is x, r0
if r1 goto L1 else goto L2 :: bool
L1:
r2 = r1
goto L3
L2:
r3 = __main__.B :: type
r4 = type_is x, r3
r2 = r4
L3:
if r2 goto L4 else goto L5 :: bool
L4:
r5 = cast(union[__main__.A, __main__.B], x)
return r5
L5:
r6 = A()
return r6
[case testIsInstanceFewSubclasses]
class R: pass
class A(R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return A()
[out]
def f(x):
x, r0 :: object
r1, r2 :: bool
r3 :: object
r4 :: bool
r5 :: __main__.R
r6 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = type_is x, r0
if r1 goto L1 else goto L2 :: bool
L1:
r2 = r1
goto L3
L2:
r3 = __main__.R :: type
r4 = type_is x, r3
r2 = r4
L3:
if r2 goto L4 else goto L5 :: bool
L4:
r5 = cast(__main__.R, x)
return r5
L5:
r6 = A()
return r6
[case testIsInstanceFewSubclassesTrait]
from mypy_extensions import trait
class B: pass
@trait
class R: pass
class A(B, R): pass
class C(B, R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return A()
[out]
def f(x):
x, r0 :: object
r1, r2 :: bool
r3 :: object
r4 :: bool
r5 :: __main__.R
r6 :: __main__.A
L0:
r0 = __main__.A :: type
r1 = type_is x, r0
if r1 goto L1 else goto L2 :: bool
L1:
r2 = r1
goto L3
L2:
r3 = __main__.C :: type
r4 = type_is x, r3
r2 = r4
L3:
if r2 goto L4 else goto L5 :: bool
L4:
r5 = cast(__main__.R, x)
return r5
L5:
r6 = A()
return r6
[case testIsInstanceManySubclasses]
class R: pass
class A(R): pass
class B(R): pass
class C(R): pass
def f(x: object) -> R:
if isinstance(x, R):
return x
return B()
[out]
def f(x):
x, r0 :: object
r1 :: bool
r2 :: __main__.R
r3 :: __main__.B
L0:
r0 = __main__.R :: type
r1 = isinstance x, r0
if r1 goto L1 else goto L2 :: bool
L1:
r2 = cast(__main__.R, x)
return r2
L2:
r3 = B()
return r3
[case testFakeSuper]
class A:
def __init__(self, x: int) -> None:
self.x = x
class B(A):
def __init__(self, x: int, y: int) -> None:
A.__init__(self, x)
self.y = y
[out]
def A.__init__(self, x):
self :: __main__.A
x :: int
r0 :: bool
r1 :: None
L0:
self.x = x; r0 = is_error
r1 = None
return r1
def B.__init__(self, x, y):
self :: __main__.B
x, y :: int
r0 :: None
r1 :: bool
r2 :: None
L0:
r0 = A.__init__(self, x)
self.y = y; r1 = is_error
r2 = None
return r2
[case testClassMethod]
class C:
@staticmethod
def foo(x: int) -> int: return 10 + x
@classmethod
def bar(cls, x: int) -> int: return 10 + x
def lol() -> int:
return C.foo(1) + C.bar(2)
[out]
def C.foo(x):
x :: int
r0 :: short_int
r1 :: int
L0:
r0 = 10
r1 = r0 + x :: int
return r1
def C.bar(cls, x):
cls :: object
x :: int
r0 :: short_int
r1 :: int
L0:
r0 = 10
r1 = r0 + x :: int
return r1
def lol():
r0 :: short_int
r1 :: int
r2 :: object
r3 :: short_int
r4, r5 :: int
L0:
r0 = 1
r1 = C.foo(r0)
r2 = __main__.C :: type
r3 = 2
r4 = C.bar(r2, r3)
r5 = r1 + r4 :: int
return r5
[case testSuper1]
class A:
def __init__(self, x: int) -> None:
self.x = x
class B(A):
def __init__(self, x: int, y: int) -> None:
super().__init__(x)
self.y = y
[out]
def A.__init__(self, x):
self :: __main__.A
x :: int
r0 :: bool
r1 :: None
L0:
self.x = x; r0 = is_error
r1 = None
return r1
def B.__init__(self, x, y):
self :: __main__.B
x, y :: int
r0 :: None
r1 :: bool
r2 :: None
L0:
r0 = A.__init__(self, x)
self.y = y; r1 = is_error
r2 = None
return r2
[case testSuper2]
from mypy_extensions import trait
@trait
class T:
def foo(self) -> None: pass
class X(T):
def foo(self) -> None:
super().foo()
[out]
def T.foo(self):
self :: __main__.T
r0 :: None
L0:
r0 = None
return r0
def X.foo(self):
self :: __main__.X
r0, r1 :: None
L0:
r0 = T.foo(self)
r1 = None
return r1
[case testClassVariable]
from typing import ClassVar
class A:
x = 10 # type: ClassVar[int]
def f() -> int:
return A.x
[out]
def f():
r0 :: object
r1 :: str
r2 :: object
r3 :: int
L0:
r0 = __main__.A :: type
r1 = unicode_6 :: static ('x')
r2 = getattr r0, r1
r3 = unbox(int, r2)
return r3
[case testNoEqDefined]
class A:
pass
def f(a: A, b: A) -> bool:
return a == b
def f2(a: A, b: A) -> bool:
return a != b
[out]
def f(a, b):
a, b :: __main__.A
r0 :: bool
L0:
r0 = a is b
return r0
def f2(a, b):
a, b :: __main__.A
r0 :: bool
L0:
r0 = a is not b
return r0
[case testEqDefined]
class Base:
def __eq__(self, other: object) -> bool:
return False
class Derived(Base):
def __eq__(self, other: object) -> bool:
return True
def f(a: Base, b: Base) -> bool:
return a == b
def f2(a: Base, b: Base) -> bool:
return a != b
def fOpt(a: Derived, b: Derived) -> bool:
return a == b
def fOpt2(a: Derived, b: Derived) -> bool:
return a != b
[out]
def Base.__eq__(self, other):
self :: __main__.Base
other :: object
r0 :: bool
r1 :: object
L0:
r0 = False
r1 = box(bool, r0)
return r1
def Base.__ne__(self, rhs):
self :: __main__.Base
rhs, r0, r1 :: object
r2, r3 :: bool
r4 :: object
L0:
r0 = self.__eq__(rhs)
r1 = NotImplemented
r2 = r0 is r1
if r2 goto L2 else goto L1 :: bool
L1:
r3 = not r0
r4 = box(bool, r3)
return r4
L2:
return r1
def Derived.__eq__(self, other):
self :: __main__.Derived
other :: object
r0 :: bool
r1 :: object
L0:
r0 = True
r1 = box(bool, r0)
return r1
def f(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = a == b
r1 = unbox(bool, r0)
return r1
def f2(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = a != b
r1 = unbox(bool, r0)
return r1
def fOpt(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__eq__(b)
r1 = unbox(bool, r0)
return r1
def fOpt2(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__ne__(b)
r1 = unbox(bool, r0)
return r1
[case testEqDefinedLater]
def f(a: 'Base', b: 'Base') -> bool:
return a == b
def f2(a: 'Base', b: 'Base') -> bool:
return a != b
def fOpt(a: 'Derived', b: 'Derived') -> bool:
return a == b
def fOpt2(a: 'Derived', b: 'Derived') -> bool:
return a != b
class Base:
pass
class Derived(Base):
def __eq__(self, other: object) -> bool:
return True
[out]
def f(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = a == b
r1 = unbox(bool, r0)
return r1
def f2(a, b):
a, b :: __main__.Base
r0 :: object
r1 :: bool
L0:
r0 = a != b
r1 = unbox(bool, r0)
return r1
def fOpt(a, b):
a, b :: __main__.Derived
r0 :: object
r1 :: bool
L0:
r0 = a.__eq__(b)
r1 = unbox(bool, r0)
return r1
def fOpt2(a, b):
a, b :: __main__.Derived
r0 :: str
r1 :: object
r2 :: bool
L0:
r0 = unicode_1 :: static ('__ne__')
r1 = py_method_call(a, r0, b)
r2 = unbox(bool, r1)
return r2
def Derived.__eq__(self, other):
self :: __main__.Derived
other :: object
r0 :: bool
r1 :: object
L0:
r0 = True
r1 = box(bool, r0)
return r1
def Derived.__ne__(self, rhs):
self :: __main__.Derived
rhs, r0, r1 :: object
r2, r3 :: bool
r4 :: object
L0:
r0 = self.__eq__(rhs)
r1 = NotImplemented
r2 = r0 is r1
if r2 goto L2 else goto L1 :: bool
L1:
r3 = not r0
r4 = box(bool, r3)
return r4
L2:
return r1
[case testDefaultVars]
from typing import ClassVar, Optional
class A:
x = 10
def lol(self) -> None:
self.x = 100
LOL = 'lol'
class B(A):
y = LOL
z: Optional[str] = None
b = True
bogus = None # type: int
[out]
def A.lol(self):
self :: __main__.A
r0 :: short_int
r1 :: bool
r2 :: None
L0:
r0 = 100
self.x = r0; r1 = is_error
r2 = None
return r2
def A.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.A
r0 :: short_int
r1, r2 :: bool
L0:
r0 = 10
__mypyc_self__.x = r0; r1 = is_error
r2 = True
return r2
def B.__mypyc_defaults_setup(__mypyc_self__):
__mypyc_self__ :: __main__.B
r0 :: short_int
r1 :: bool
r2 :: dict
r3 :: str
r4 :: object
r5 :: str
r6 :: bool
r7 :: None
r8 :: object
r9, r10, r11, r12 :: bool
L0:
r0 = 10
__mypyc_self__.x = r0; r1 = is_error
r2 = __main__.globals :: static
r3 = unicode_9 :: static ('LOL')
r4 = r2[r3] :: dict
r5 = cast(str, r4)
__mypyc_self__.y = r5; r6 = is_error
r7 = None
r8 = box(None, r7)
__mypyc_self__.z = r8; r9 = is_error
r10 = True
__mypyc_self__.b = r10; r11 = is_error
r12 = True
return r12
[case testSubclassDictSpecalized]
from typing import Dict
class WelpDict(Dict[str, int]):
pass
def foo(x: WelpDict) -> None:
# we care that the specalized op gets used
x.update(x)
[out]
def foo(x):
x :: dict
r0 :: bool
r1, r2 :: None
L0:
r0 = x.update(x) :: dict
r1 = None
r2 = None
return r2