-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a challenge for variadic generics (#76)
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
TODO: | ||
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. | ||
""" | ||
|
||
|
||
class Array: | ||
def __add__(self, other): | ||
... | ||
|
||
|
||
## End of your code ## | ||
from typing import assert_type | ||
|
||
a: Array[float, int] = Array() | ||
b: Array[float, int] = Array() | ||
assert_type(a + b, Array[float, int]) | ||
|
||
c: Array[float, int, str] = Array() | ||
assert_type(a + c, Array[float, int, str]) # expect-type-error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
TODO: | ||
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. | ||
""" | ||
|
||
from typing import Generic, TypeVar, TypeVarTuple, assert_type | ||
|
||
T = TypeVar("T") | ||
Ts = TypeVarTuple("Ts") | ||
|
||
|
||
class Array(Generic[*Ts]): | ||
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": | ||
... | ||
|
||
|
||
## End of your code ## | ||
from typing import assert_type | ||
|
||
a: Array[float, int] = Array() | ||
b: Array[float, int] = Array() | ||
assert_type(a + b, Array[float, int]) | ||
|
||
c: Array[float, int, str] = Array() | ||
assert_type(a + c, Array[float, int, str]) # expect-type-error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
TODO: | ||
Define an `Array` type that supports element-wise addition of arrays with identical dimensions and types. | ||
""" | ||
|
||
from typing import assert_type | ||
|
||
|
||
class Array[*Ts]: | ||
def __add__(self, other: "Array[*Ts]") -> "Array[*Ts]": | ||
... | ||
|
||
|
||
## End of your code ## | ||
from typing import assert_type | ||
|
||
a: Array[float, int] = Array() | ||
b: Array[float, int] = Array() | ||
assert_type(a + b, Array[float, int]) | ||
|
||
c: Array[float, int, str] = Array() | ||
assert_type(a + c, Array[float, int, str]) # expect-type-error |