Open ldorigo opened 6 months ago
Hi @ldorigo ,
Thank you for the catch!
Is there any way or workaround to currently type it properly? I'm having to add #type: ignore all over our codebase 😞
Yeah in general I would like the typing system to look at a Link[T]
and recognize it's actually more like a Union[T, Link[T]]
.
Just to mention that I've run into this issue as well, so I think the only workaround for now is to ignore the type check on these links or to do casting? That is, either
MyDocument.linked_type # type: ignore[attr-defined]
or
from typing import cast
cast(MyDocument.linked_type, UnderlyingType)
I'd rather have a more robust solution that was built in with Beanie. But I've had success doing the following as a temporary measure.
from typing import TypeAlias
from beanie import Link as BeanieLink
if TYPE_CHECKING:
_T = TypeVar("_T", bound=Document)
Link: TypeAlias = _T
else:
Link = BeanieLink
What it does is when type checking it aliases Link to be the Document class it's wrapping.
Describe the bug
Links to related documents annotated as
Link[xx]
get the typeLink[xx]
, which gives type errors when trying to access e.g.obj.linkedobj
.To Reproduce