Disclaimer: The details mentioned below are just my thoughts on an approach towards software correctness. I consider myself not to be an expert in Python, Rust, software verification, or in general software development.

Please use your own judgement when designing/implementing software. That said, I am interested in practices to make software safer, and would highly appreciate any constructive criticism/feedback.

What is TypeState Pattern?

The basic idea behind TypeState pattern is to encode the state information inside types.

While TypeState pattern is a generic pattern that could be used in multiple languages, I  encountered this pattern first while developing software using Rust.

There are multitude of resources (see References below) that discuss TypeState pattern inside Rust in further detail, so I will not delve into details of TypeState pattern.

Example of TypeState pattern in Python

Let’s explore an example API: say we have a connection API over which we could establish connections and communicate with web servers using HTTP. The connection can exist in two states: the connection to remote end has not been established, and the connection to remote end has been established. There can be more than two states for the connection, but for this example, we will consider only these two states.

#design-patterns #python #programming

How to Implement TypeState Pattern in Python
3.10 GEEK