Skip to content

Commit dd60cb8

Browse files
authored
feat: unify solution for python 3.12 (#117)
1 parent 902d87e commit dd60cb8

File tree

8 files changed

+50
-39
lines changed

8 files changed

+50
-39
lines changed

challenges/advanced-decorator/solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
from collections.abc import Callable
88
from typing import TypeVar
99

10-
# Python < 3.12
10+
# For Python < 3.12
1111
#
1212
# T = TypeVar("T", bound=Callable)
1313
#
1414
# def decorator(message: str) -> Callable[[T], T]:
1515
# return func
1616

1717

18-
# Python >= 3.12
18+
# For Python >= 3.12
1919
def decorator[T: Callable](message: str) -> Callable[[T], T]:
2020
...
2121

challenges/advanced-overload-literal/solution.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
"""
77
from typing import Any, Literal, overload, TypeVar
88

9-
T = TypeVar("T")
9+
# Before 3.12 you have to write:
10+
# T = TypeVar('T')
11+
#
12+
# def foo(value: T, flag: Any) -> T:
1013

1114

1215
@overload
@@ -25,7 +28,7 @@ def foo(value: Any, flag: Literal[3]) -> list[Any]:
2528

2629

2730
@overload
28-
def foo(value: T, flag: Any) -> T:
31+
def foo[T](value: T, flag: Any) -> T:
2932
...
3033

3134

challenges/basic-typealias/solution.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
Create a new type called Vector, which is a list of float.
55
"""
66

7-
from typing import TypeAlias
7+
# Before 3.12 you have to write
8+
# from typing import TypeAlias
9+
#
10+
# Vector: TypeAlias = list[float]
811

9-
Vector: TypeAlias = list[float]
10-
11-
# Python >= 3.12
12-
# type Vector = list[float]
12+
type Vector = list[float]
1313

1414

1515
## End of your code ##

challenges/extreme-constructor/solution.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
"""
88
from typing import Callable, ParamSpec, TypeVar
99

10-
T = TypeVar("T")
11-
P = ParamSpec("P")
12-
R = TypeVar("R")
10+
# Before 3.12 you have to write
11+
# T = TypeVar("T")
12+
# P = ParamSpec("P")
13+
# R = TypeVar("R")
14+
# def constructor_parameter(
1315

1416

15-
def constructor_parameter(
17+
def constructor_parameter[**P, T, R](
1618
cls: Callable[P, T],
1719
) -> Callable[[Callable[[T], R]], Callable[P, R]]:
1820
...

challenges/intermediate-decorator/solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
"""
66
from typing import Callable, TypeVar
77

8-
# Python < 3.12
8+
# For Python < 3.12
99
#
1010
# T = TypeVar("T", bound=Callable)
1111
#
1212
# def decorator(func: T) -> T:
1313
# return func
1414

1515

16-
# Python >= 3.12
16+
# For Python >= 3.12
1717
def decorator[T: Callable](func: T) -> T:
1818
return func
1919

challenges/intermediate-generic/solution.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
The function `add` accepts two arguments and returns a value, they all have the same type.
55
"""
66

7-
from typing import TypeVar
8-
9-
T = TypeVar("T")
7+
# Before 3.12 you have to write
8+
# from typing import TypeVar
9+
#
10+
# T = TypeVar("T")
11+
#
12+
#
13+
# def add(a: T, b: T) -> T:
14+
# return a
1015

1116

12-
def add(a: T, b: T) -> T:
17+
# For Python >= 3.12
18+
def add[T](a: T, b: T) -> T:
1319
return a
1420

1521

16-
# For Python >= 3.12
17-
# def add[T](a: T, b: T) -> T:
18-
# return a
19-
2022
## End of your code ##
2123
from typing import List, assert_type
2224

challenges/intermediate-generic2/solution.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
The function `add` accepts two arguments and returns a value, they all have the same type.
55
The type can only be str or int (or their subclasses).
66
"""
7-
from typing import TypeVar
8-
9-
T = TypeVar("T", int, str)
7+
# For Python < 3.12
8+
# from typing import TypeVar
9+
#
10+
# T = TypeVar("T", int, str)
11+
#
12+
#
13+
# def add(a: T, b: T) -> T:
14+
# return a
1015

1116

12-
def add(a: T, b: T) -> T:
17+
# For Python >= 3.12
18+
def add[T: (str, int)](a: T, b: T) -> T:
1319
return a
1420

1521

16-
# For Python >= 3.12
17-
# def add[T: (str,int)](a: T, b: T) -> T:
18-
# return a
19-
2022
## End of your code ##
2123
from typing import assert_type
2224

challenges/intermediate-generic3/solution.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
The function `add` accepts one argument and returns a value, they all have the same type.
55
The type can only be int or subclasses of int.
66
"""
7-
from typing import TypeVar
8-
9-
T = TypeVar("T", bound=int)
7+
# For Python < 3.12
8+
# from typing import TypeVar
9+
#
10+
# T = TypeVar("T", bound=int)
11+
#
12+
#
13+
# def add(a: T) -> T:
14+
# return a
1015

1116

12-
def add(a: T) -> T:
17+
# For Python >= 3.12
18+
def add[T: int](a: T) -> T:
1319
return a
1420

1521

16-
# For Python >= 3.12
17-
# def add[T: int](a: T) -> T:
18-
# return a
19-
2022
## End of your code ##
2123
from typing import assert_type
2224

0 commit comments

Comments
 (0)