Lightning-AI / utilities

Common Python utilities and GitHub Actions in Lightning Ecosystem
Apache License 2.0
49 stars 14 forks source link

Non roundtrippable sequence subclasses raise error in `apply_to_collection` #249

Open ap-- opened 5 months ago

ap-- commented 5 months ago

🐛 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