covariance v. contravariance in Callable
This commit is contained in:
1 parent
646427f393
commit
434f8d26b6
1 file changed
+22
@@ -0,0 +1,22 @@
|
|||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
|
def update( # <1>
|
||||||
|
probe: Callable[[], float], # <2>
|
||||||
|
display: Callable[[float], None] # <3>
|
||||||
|
) -> None:
|
||||||
|
temperature = probe()
|
||||||
|
# imagine lots of control code here
|
||||||
|
display(temperature)
|
||||||
|
|
||||||
|
def probe_ok() -> int: # <4>
|
||||||
|
return 42
|
||||||
|
|
||||||
|
def display_wrong(temperature: int) -> None: # <5>
|
||||||
|
print(hex(temperature))
|
||||||
|
|
||||||
|
update(probe_ok, display_wrong) # type error # <6>
|
||||||
|
|
||||||
|
def display_ok(temperature: complex) -> None: # <7>
|
||||||
|
print(temperature)
|
||||||
|
|
||||||
|
update(probe_ok, display_ok) # OK # <8>
|
||||||
Reference in new issue
Block a user