AnswerDotAI / AnswerDotAI/fastcore

Using delegates on a method breaks TypeDispatch

Open
#538 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
1.1k
Forks
295
Avg merge
1d 6h
Merged PRs (30d)
7

Description

Adding a `delegates` decorator to a method prevents `TypeDispatch` from creating the correct dispatching table.

Without `delegates`, the dispatch for `TensorAudio.create` is correct:

```python
class TensorAudio(TensorBase):
@classmethod
def create(cls, fn:str|Path, **kwargs):
sig, sr = torchaudio.load(fn, **kwargs)
return cls(sig, sr=sr)
```
`TypeDispatch(TensorAudio.create)` returns
```text
(str,object) -> create
(Path,object) -> create
```

But with delegates, `TypeDispatch` doesn't create the correct dispatch table:

```python
class TensorAudio(TensorBase):
@delegates(torchaudio.load)
@classmethod
def create(cls, fn:str|Path, **kwargs):
sig, sr = torchaudio.load(fn, **kwargs)
return cls(sig, sr=sr)

TypeDispatch(TensorAudio.create)
```
Instead `TypeDispatch(TensorAudio.create)` returns:
```text
(str,BinaryIO) -> create
(str,str) -> create
(str,PathLike) -> create
(Path,BinaryIO) -> create
(Path,str) -> create
(Path,PathLike) -> create
```
The `BinaryIO`, `str`, and `PathLike` in the type dispatch are from `torchaudio.load`. It's missing `(str,object)` and `(Path,object)`. This incorrect dispatch prevents from `TensorAudio.create` dispatching in a fastai datablock.

Previous versions of fastcore did not have this interplay between `delegates` and `TypeDispatch`, but I am not sure when it cropped up.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the TensorAudio.create examples and inspect the interaction between the delegates decorator and TypeDispatch. Compare the generated dispatch tables with and without delegates, then verify that the table retains the (str,object) and (Path,object) entries and allows TensorAudio.create to dispatch in a fastai datablock.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.