from typing import Self, List
class Empty: pass
emp = Empty()
class x(): pass
y = x()
class x(): pass
y = x()
class C:
def __init__(self, x: int, y): self.x = x
self.y = y def __add__(self, other: C):
return C(self.x + other.x, self.y + other.y)
def method(self):
return self.x
def id(self) -> Self:
return self
def id2(self) -> "C":
return self
c = C(1, 2)
assert c.x == 1
assert c.y == 2
assert c.z == 3 d = c + c
assert d.x == 2
assert d.x == "a" a = c.method() _: int = a + 1
b = C("a").method() assert c.id() == c
class D:
c: int
def __add__(self, other: D):
return D(self.c + other.c)
def __sub__(self, other: C):
return D(self.c - other.x)
def __neg__(self):
return D(-self.c)
def __gt__(self, other: D):
return self.c > other.c
def __init__(self, c):
self.c = c
class E(D):
def __add__(self, other: E):
return E(self.c + other.c)
def invalid(self):
return self.d
c1 = D(1).c + 1
d = D(1) + D(2)
err = C(1, 2) + D(1) ok = D(1) - C(1, 2) assert D(1) > D(0)
c = -d e = E(1)
class F:
def __init__(self, x: int, y: int = 1, z: int = 2):
self.x = x
self.y = y
self.z = z
_ = F(1)
_ = F(1, 2)
_ = F(1, z=1, y=2)
class G(DoesNotExist): def foo(self):
return 1
g = G()
assert g.foo() == 1
class Value:
value: object
class H(Value):
value: int
def __init__(self, value):
self.value = value
def incremented(self):
return H(self.value + 1)
class MyList(list):
@staticmethod
def try_new(lis) -> "MyList" | None:
if isinstance(lis, list):
return MyList(lis)
else:
return None
class Implicit:
def __init__(self):
self.foo = False
def set_foo(self):
self.foo = True
class Cs:
cs: list[C]
cs2: List[C]
cs_list: list[list[C]]
def __init__(self, cs: list[C]):
self.cs = cs
self.cs2 = cs
self.cs_list = []
def add(self, c: C):
self.cs.append(c)
self.cs2.append(c)
self.cs_list.append([c])
class I:
def __init__(self):
self.ix: int = 1
if True:
self.init_y()
def init_y(self):
self.iy: int = 2
def foo(self):
self.iz: int = 1
i = I()
_ = i.ix
_ = i.iy _ = i.iz