utilities icon indicating copy to clipboard operation
utilities copied to clipboard

Non roundtrippable sequence subclasses raise error in `apply_to_collection`

Open ap-- opened this issue 1 year ago • 0 comments

🐛 Bug

Some of our dataloaders use custom Sequence subclasses, that cause errors in apply_to_collection.

The reason for this is, that these classes won't roundtrip, which is assumed by the implementation of apply_to_collection.

# minimal example
class X(list):
    def __init__(self, x):
        super().__init__(range(x))

data = X(4)

assert data == type(data)(list(data))  # is False, because of how class X's constructor is implemented
                                       # but this is how apply_to_collection handles Sequences

The above is assumed implicitly here: https://github.com/Lightning-AI/utilities/blob/7db19e0336f4a5a11407d3c4bd8eaf59d740ecab/src/lightning_utilities/core/apply_func.py#L123-L137

To Reproduce

See example above.

Expected behavior

If an instance of a sequence subclass can't roundtrip it should just be passed through.

Additional context

N/A

Cheers, Andreas

ap-- avatar Mar 29 '24 17:03 ap--