Debugging Kubernetes

23 Jul 2024

Helpful tips for debugging applications running in k8s

build/k8s.png

Handling multiple errors in Rust iterator adapters

17 Dec 2023

Approaches for handling multiple errors within iterator adapters

build/rust.png

Better FastAPI Background Jobs

29 Aug 2022

A more featureful background task runner for Async apps like FastAPI or Discord bots

build/fastapi_logo.png

Useful Linux Examples

21 Dec 2021

A plethora of helpful tips for working in Linux

build/bash_logo.png
Continue to all blog posts

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'):
    ...