Code: Select all
class C:
pass
class D(C):
pass
class E[T: C = C]:
def g(self, f: Callable[[], T]) -> T:
return f()
x: E = E[D]() # error
< /code>
Rückgabetypen sind kovariante. Typecks: < /p>
x = E() # x is E[C]
x.g(D) # f is Callable[[], D] which fits on Callable[[], C]
Code: Select all
class E[T: C = C]:
def g(self, f: Callable[[], None]) -> T:
return cast(T, f()) # anything
x: E = E[D]() # OK