There are times when working on critical or complex systems where issues manifest but it is difficult to pin down the source.
A set of code may write data and succeed with no issues but then another pieces comes along to read and finds that the data is wrong or incompatible. That is not horrific but when there are several actors it can be difficult to test all those scenerios and when things go south figure out which part introduced the issue.
Functional tests can help but with complex system it is impossible to create test cases for all scenerios.
One way I have found useful for this class of issues is called defensive programming.
The basic idea is that at runtime parts of the code are aggressive that things are as expected.
This is more than about typing. It is about validating from a logic perspective things are correct.
This can be many things from checking that a value is in a certain range, maybe has a certain structure, even validating update operations return the what is expected, and so forth.
Once something is a miss it is the job of that code to shut it down.
By identifying things and breaking once found it is easier to find the chain that caused it. Often some code introduces a problem but only until it complete breaks things does an issue occur and it is hard to trace which part of the code intiated it.
With defensive programming the idea is that your code trusts nothing it gets and nopes out if it gets something it doesn't expect or like. With the hope that you short circuit difficult hard to trace bugs.
Outside of just stopping the train wreck, I find that using this style of programming really makes it easy to understand a piece of code. To me defensive programming are like pratical comments.
It also forces you to think and codify your assumptions.
In Python this is easy to add just by using the assert statement.
assert isinstance(input_param, int) assert input_param in range(0,5)
This document has a great list of different ways to use asserts. https://python.plainenglish.io/defensive-programming-in-python-af0266e65dfd
Tools like Pydantic can also handle certain classes of defensive programming like data validation.