fastai / fastcore

Python supercharged for the fastai library
http://fastcore.fast.ai
Apache License 2.0
954 stars 274 forks source link

Using delegates on a method breaks TypeDispatch #538

Open warner-benjamin opened 10 months ago

warner-benjamin commented 10 months ago

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

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

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

(str,object) -> create
(Path,object) -> create

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

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:

(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.

jph00 commented 4 months ago

Do you want to try fixing this @warner-benjamin ?