Template literal types

Now that PEP 750 has been accepted, Python can perhaps have template literal types similar to what TypeScript has:

def emphasize[T: LiteralString](v: T, /) -> Literal[t'<em>{T}</em>']: ...

reveal_type(emphasize('Lorem'))  # Literal['<em>Lorem</em>']

I’m not pursuing this idea any further, but it seems like something worth sharing.

3 Likes