Skip to content

Using delegates on a method breaks TypeDispatch #538

@warner-benjamin

Description

@warner-benjamin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions