Skip to content

Feature Request: Allow for unbound composite structures as return types #357

@pdcook

Description

@pdcook

Suppose one has a commonly used type with a given structure that is type aliased, and another commonly used type that should have this structure as a prefix:

ModelType: TypeAlias = PyTree[Any, "M"]
ModelParams: TypeAlias = PyTree[Any, "M ..."]

This works well, except for in the (in my opinion) reasonable case of a argument-less function that involves this second type as a return type:

def get_params() -> ModelParams:
    ...

This will always throw a AnnotationError since the structure name "M" had not been seen in the scope of this function.

The obvious workaround is to manually write either PyTree[Any, "M ..."] or PyTree[Any] every single time, depending on if "M" can be bound or not. But this is exceedingly opaque to any user and is tedious for more complicated and real-world types.

It's possible there would be a downside to allowing for unbound structures in return types, but we already have this functionality for Array shapes:

def get_array() -> Num[Array, "d ..."]:
    ....

works perfectly fine, despite "d" not having been seen in the scope of the function.

For reference, in case it makes a difference, I'm using beartype as my typechecker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions