Typelate 0.1.0

11-03-2024

The format method

With PEP 3101 came a new way to format strings, this formatting technique later established what we know as Python's f-strings (in PEP 498). Shortly, it adds placeholders in strings marked with {} those placeholders can be later then formatted with values using the format method.

In the last years Python and type safety came closely related when the use of type annotations became the standard. But those template using the format method stayed unsafe. Which made me think why not incorporate type annotations into those template string.

Introducing typelate

With the typelate module, you can create templates and add type annotations to the placeholders later will be replaced by actual values. Let's create a simple example

1
2
3
from typelate import Template

template = Template("Hello, {name: str}!")

Here template is a simple template that has a name placeholder of type str. We can use it by calling the template (__call__) with the correct parameters.

1
2
>>> template(name="World")
"Hello, World!"

Now, let's pass an invalid type.

1
2
>>> template(name=123)
TypeError: Incorrect type for replacement 'name', expected: <class 'str'>.

Advanced functionality

  1. Format specifiers can be used with typelate templates as well:
1
2
3
>>> template = Template("pi is: {number: float: .2f}")
>>> template(number=math.pi)
"pi is 3.14"
  1. You can use local or global classes and types in your template:
1
2
3
4
5
6
from typelate import Template

class A:
    pass

template = Template("A global class: {a: A}")
  1. The Template is wrapped around str, which means all str methods can be used on Templates:
1
2
3
4
5
from typelate import Template

template = Template("Hello, {name: str}!")

assert template.count("l") == 2

Installation

typelate can be installed via pip:

1
$ pip install typelate