Hi guys, I'm dealing with a circular import caused by explicit typeclass registrations. I'm a bit confused what to do next.
I'm using github.com/dry-python/classes which is somewhat similar to functools.singledispatch.
So, I have to_dict, I'll dump only a part of the code to put into the context of ```py
Copyright (c) 2025 Osyah
SPDX-License-Identifier: MIT
from future import annotations
all: typing.Sequence[str] = ("to_dict",)
import typing
from classes import typeclass
from pletyvo.protocol.dapp.event import (
AuthHeader,
EventInput,
Event,
...
)
if typing.TYPE_CHECKING:
from typing import Any
@typeclass
def to_dict(instance) -> dict[str, Any]: ...
@to_dict.instance(AuthHeader)
def _to_dict_dapp_auth_header(instance: AuthHeader) -> dict[str, Any]:
from base64 import b64encode
return {
"sch": instance.sch,
"pub": b64encode(instance.pub).decode(),
"sig": b64encode(instance.sig).decode(),
}
@to_dict.instance(EventInput)
def _to_dict_dapp_event_input(instance: EventInput) -> dict[str, Any]:
return {
"body": str(instance.body),
"auth": to_dict(instance.auth),
}
@to_dict.instance(Event)
def _to_dict_dapp_event(instance: Event) -> dict[str, Any]:
return {
"id": str(instance.id),
"body": str(instance.body),
"auth": to_dict(instance.auth),
}
...
So, the problem I'm facing is that `classes.typeclass` requires passing the actual class object (e.g. `@to_dict.instance(AuthHeader))`, which forces me to import everything eagerly. One of those modules (e.g. `pletyvo.protocol.dapp.event`) ends up importing the serialiser (`to_dict`) that registers it - and boom, circular import. Tricks like `TYPE_CHECKING` or deferring inside functions don't help because as i said registration happens at import time.
Anyone dealt with this?