Skip to content

Commit

Permalink
Add a challenge for variadic generics (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
daya0576 authored Dec 23, 2023
1 parent 8131ee4 commit 95fef31
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
21 changes: 21 additions & 0 deletions challenges/advanced-variadic-generics/question.py
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
26 changes: 26 additions & 0 deletions challenges/advanced-variadic-generics/solution.py
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
23 changes: 23 additions & 0 deletions challenges/advanced-variadic-generics/solution2.py
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

0 comments on commit 95fef31

Please sign in to comment.