Failing to infer correct return type from partially stringified type annotation (original) (raw)

Summary

I encountered a problem where ty fails to correctly infer the return type when calling the get class method of a Beanie ODM (v2.1.0) model. The implementation of the called method can be found here.

from beanie import Document, PydanticObjectId

class Foo(Document): bar: str = "baz"

async def qux() -> None: foo = await Foo.get(PydanticObjectId("69d761e5b107b9e5f753515e")) # Unknown | None print(foo.bar) # Attribute bar is not defined on None in union Unknown | None

It's correct to error out when accessing bar, but the inferred type is wrong. For comparison, mypy correctly infers Foo | None, here.

Version

ty 0.0.29