def sized_len(dataloader: object) -> Optional[int]:
    """Try to get the length of an object, return ``None`` otherwise."""
    try:
        # try getting the length
        length = len(dataloader)  # type: ignore [arg-type]
    except (TypeError, NotImplementedError):
        length = None
    return length
Try to get the length of an object, return ``None`` otherwise.
src/lightning/fabric/utilities/data.py