Skip to content

Commit 95fef31

Browse files
authored
Add a challenge for variadic generics (#76)
1 parent 8131ee4 commit 95fef31

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
TODO:
3+
4+
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types.
5+
"""
6+
7+
8+
class Array:
9+
def __add__(self, other):
10+
...
11+
12+
13+
## End of your code ##
14+
from typing import assert_type
15+
16+
a: Array[float, int] = Array()
17+
b: Array[float, int] = Array()
18+
assert_type(a + b, Array[float, int])
19+
20+
c: Array[float, int, str] = Array()
21+
assert_type(a + c, Array[float, int, str]) # expect-type-error
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
TODO:
3+
4+
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types.
5+
"""
6+
7+
from typing import Generic, TypeVar, TypeVarTuple, assert_type
8+
9+
T = TypeVar("T")
10+
Ts = TypeVarTuple("Ts")
11+
12+
13+
class Array(Generic[*Ts]):
14+
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
15+
...
16+
17+
18+
## End of your code ##
19+
from typing import assert_type
20+
21+
a: Array[float, int] = Array()
22+
b: Array[float, int] = Array()
23+
assert_type(a + b, Array[float, int])
24+
25+
c: Array[float, int, str] = Array()
26+
assert_type(a + c, Array[float, int, str]) # expect-type-error
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
TODO:
3+
4+
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types.
5+
"""
6+
7+
from typing import assert_type
8+
9+
10+
class Array[*Ts]:
11+
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]":
12+
...
13+
14+
15+
## End of your code ##
16+
from typing import assert_type
17+
18+
a: Array[float, int] = Array()
19+
b: Array[float, int] = Array()
20+
assert_type(a + b, Array[float, int])
21+
22+
c: Array[float, int, str] = Array()
23+
assert_type(a + c, Array[float, int, str]) # expect-type-error

0 commit comments

Comments
 (0)