79395147

Date: 2025-01-28 20:53:16
Score: 1.5
Natty:
Report link

Did you used TypeVar correctly? Here is an example

from typing import Sequence, cast, TypeVar, Type

T = TypeVar("T")


def map_cast(T: Type[T], seq: Sequence) -> Sequence[T]:
    """Cast a sequence of elements to a sequence of elements of given type."""
    return [cast(T, x) for x in seq]


a = map_cast(str, [1, 2, 3]) # a will have type hint is Sequence[str]

print(a)
Reasons:
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Did you use
  • Low reputation (1):
Posted by: Vũ Trí Anh Hoàng