Skip to content

Commit 434f8d2

Browse files
committed
covariance v. contravariance in Callable
1 parent 646427f commit 434f8d2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections.abc import Callable
2+
3+
def update( # <1>
4+
probe: Callable[[], float], # <2>
5+
display: Callable[[float], None] # <3>
6+
) -> None:
7+
temperature = probe()
8+
# imagine lots of control code here
9+
display(temperature)
10+
11+
def probe_ok() -> int: # <4>
12+
return 42
13+
14+
def display_wrong(temperature: int) -> None: # <5>
15+
print(hex(temperature))
16+
17+
update(probe_ok, display_wrong) # type error # <6>
18+
19+
def display_ok(temperature: complex) -> None: # <7>
20+
print(temperature)
21+
22+
update(probe_ok, display_ok) # OK # <8>

0 commit comments

Comments
 (0)