Skip to content

Commit

Permalink
feat: unify solution for python 3.12 (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisrall authored Feb 2, 2024
1 parent 902d87e commit dd60cb8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
4 changes: 2 additions & 2 deletions challenges/advanced-decorator/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from collections.abc import Callable
from typing import TypeVar

# Python < 3.12
# For Python < 3.12
#
# T = TypeVar("T", bound=Callable)
#
# def decorator(message: str) -> Callable[[T], T]:
# return func


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

Expand Down
7 changes: 5 additions & 2 deletions challenges/advanced-overload-literal/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"""
from typing import Any, Literal, overload, TypeVar

T = TypeVar("T")
# Before 3.12 you have to write:
# T = TypeVar('T')
#
# def foo(value: T, flag: Any) -> T:


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


@overload
def foo(value: T, flag: Any) -> T:
def foo[T](value: T, flag: Any) -> T:
...


Expand Down
10 changes: 5 additions & 5 deletions challenges/basic-typealias/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
Create a new type called Vector, which is a list of float.
"""

from typing import TypeAlias
# Before 3.12 you have to write
# from typing import TypeAlias
#
# Vector: TypeAlias = list[float]

Vector: TypeAlias = list[float]

# Python >= 3.12
# type Vector = list[float]
type Vector = list[float]


## End of your code ##
Expand Down
10 changes: 6 additions & 4 deletions challenges/extreme-constructor/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"""
from typing import Callable, ParamSpec, TypeVar

T = TypeVar("T")
P = ParamSpec("P")
R = TypeVar("R")
# Before 3.12 you have to write
# T = TypeVar("T")
# P = ParamSpec("P")
# R = TypeVar("R")
# def constructor_parameter(


def constructor_parameter(
def constructor_parameter[**P, T, R](
cls: Callable[P, T],
) -> Callable[[Callable[[T], R]], Callable[P, R]]:
...
Expand Down
4 changes: 2 additions & 2 deletions challenges/intermediate-decorator/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"""
from typing import Callable, TypeVar

# Python < 3.12
# For Python < 3.12
#
# T = TypeVar("T", bound=Callable)
#
# def decorator(func: T) -> T:
# return func


# Python >= 3.12
# For Python >= 3.12
def decorator[T: Callable](func: T) -> T:
return func

Expand Down
18 changes: 10 additions & 8 deletions challenges/intermediate-generic/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
The function `add` accepts two arguments and returns a value, they all have the same type.
"""

from typing import TypeVar

T = TypeVar("T")
# Before 3.12 you have to write
# from typing import TypeVar
#
# T = TypeVar("T")
#
#
# def add(a: T, b: T) -> T:
# return a


def add(a: T, b: T) -> T:
# For Python >= 3.12
def add[T](a: T, b: T) -> T:
return a


# For Python >= 3.12
# def add[T](a: T, b: T) -> T:
# return a

## End of your code ##
from typing import List, assert_type

Expand Down
18 changes: 10 additions & 8 deletions challenges/intermediate-generic2/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
The function `add` accepts two arguments and returns a value, they all have the same type.
The type can only be str or int (or their subclasses).
"""
from typing import TypeVar

T = TypeVar("T", int, str)
# For Python < 3.12
# from typing import TypeVar
#
# T = TypeVar("T", int, str)
#
#
# def add(a: T, b: T) -> T:
# return a


def add(a: T, b: T) -> T:
# For Python >= 3.12
def add[T: (str, int)](a: T, b: T) -> T:
return a


# For Python >= 3.12
# def add[T: (str,int)](a: T, b: T) -> T:
# return a

## End of your code ##
from typing import assert_type

Expand Down
18 changes: 10 additions & 8 deletions challenges/intermediate-generic3/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
The function `add` accepts one argument and returns a value, they all have the same type.
The type can only be int or subclasses of int.
"""
from typing import TypeVar

T = TypeVar("T", bound=int)
# For Python < 3.12
# from typing import TypeVar
#
# T = TypeVar("T", bound=int)
#
#
# def add(a: T) -> T:
# return a


def add(a: T) -> T:
# For Python >= 3.12
def add[T: int](a: T) -> T:
return a


# For Python >= 3.12
# def add[T: int](a: T) -> T:
# return a

## End of your code ##
from typing import assert_type

Expand Down

0 comments on commit dd60cb8

Please sign in to comment.