patrick-kidger / patrick-kidger/jaxtyping
Feature Request: Allow for unbound composite structures as return types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 96
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing jaxtyping's runtime annotation handling for PyTree structure names and compare it with the existing Array shape behavior shown in the issue. Reproduce the argument-less get_params() return annotation, then add coverage showing the intended behavior for unbound composite structures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100