Handling multiple errors in Rust iterator adapters
17 Dec 2023
Approaches for handling multiple errors within iterator adapters
Better FastAPI Background Jobs
29 Aug 2022
A more featureful background task runner for Async apps like FastAPI or Discord bots
Resolving Cyclic Imports with Typing
Recently when working on a non-trivial pygame toy project, I found I was having issues with implementing some typehints, it would cause my program to fail to startup when the code ran fine at runtime
I would get errors like:
AttributeError: partially initialized module 'module_x' has no attribute 'Thing' (most likely due to a circular import)
Debugging
To understand a bit deeper the cause of the cycle, you can look for repeated files in your traceback
Solution
If this is the issue you face, you can make use of some features introduced in pep-484/#forward-references (with considerations in pep-563) to easily solve it.
For example, change the following:
import module_x
def a_function(thing: module_x.Thing):
...
to this:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import module_x
def a_function(thing: 'module_x.Thing'):
...