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.
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:
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:
This will always throw a
AnnotationErrorsince 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 ..."]orPyTree[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
Arrayshapes: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
beartypeas my typechecker.