pymc-devs / pymc-devs/pytensor
Typing: More informative return typing for ops
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 644
- Forks
- 208
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 16
Description
Description
The return type of .graph.op.Op.__call__ is Union[Variable, List[Variable]]. However, some child classes (e.g. .tensor.basic.Alloc) only ever return a single output, in which case an output type of Variable would be more intuitive (aside from the case where return_list=True) as the union causes annoying issues with mypy as per seen in this PR.
I feel like something should be possible with typing.overload but can't really see how to implement.
However, for the case that return_list=True I think we could do the following:
from typing import Literal, overload
# -- snip --
@overload
def __call__(self, *inputs: Any, return_list: Literal[True] = True, **kwargs) -> List[Variable]:
...
def __call__(self, *inputs: Any, **kwargs) -> Union[Variable, List[Variable]]:
r"""Construct an `Apply` node using :meth:`Op.make_node` and return its outputs.
# -- snip --
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 with .graph.op.Op.call and the single-output behavior of .tensor.basic.Alloc, then review the linked PyMC PR and Python's typing.overload guidance. Define how return_list=True should be typed separately from the default call and how single-output child classes should expose Variable; confirm the result addresses the reported mypy problems.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100