code: def test_dataloader_reinit_for_subclass():
      class CustomDataLoader(DataLoader):
          def __init__(
              self,
              dataset,
              batch_size=1,
              shuffle=False,
              sampler=None,
              batch_sampler=None,
              num_workers=0,
              collate_fn=None,
              pin_memory=False,
              drop_last=False,
              timeout=0,
              worker_init_fn=None,
              dummy_kwarg=None,
          ):
              super().__init__(
                  dataset,
                  batch_size,
                  shuffle,
                  sampler,
                  batch_sampler,
                  num_workers,
                  collate_fn,
                  pin_memory,
                  drop_last,
                  timeout,
                  worker_init_fn,
              )
              self.dummy_kwarg = dummy_kwarg
              self.something_unrelated = 1
  
      trainer = Trainer(
          accelerator="cpu", devices=2, strategy="ddp_spawn"
      )
      mode = RunningStage.TRAINING
  
      class CustomDummyObj:
          sampler = None
  
      result = trainer._data_connector._prepare_dataloader(
          CustomDummyObj(), shuffle=True, mode=mode
      )
      assert isinstance(
          result, CustomDummyObj
      ), "Wrongly reinstantiated data loader"
  
      dataset = list(range(10))
      result = trainer._data_connector._prepare_dataloader(
          CustomDataLoader(dataset), shuffle=True, mode=mode
      )
      assert isinstance(result, DataLoader)
      assert isinstance(result, CustomDataLoader)
      assert result.dummy_kwarg is None
  
      # Shuffled DataLoader should also work
      result = trainer._data_connector._prepare_dataloader(
          CustomDataLoader(dataset, shuffle=True),
          shuffle=True,
          mode=mode,
      )
      assert isinstance(result, DataLoader)
      assert isinstance(result, CustomDataLoader)
      assert result.dummy_kwarg is None
  
      class CustomSampler(Sampler):
          def __init__(self, data_source: Sized) -> None:
              super().__init__(data_source)
              self.data_source = data_source
  
          def __len__(self):
              return len(self.data_source)
  
          def __iter__(self):
              return iter(range(len(self)))
  
      # Should raise an error if existing sampler is being replaced
      dataloader = CustomDataLoader(
          dataset, sampler=CustomSampler(dataset)
      )
      result = trainer._data_connector._prepare_dataloader(
          dataloader, shuffle=False, mode=mode
      )
      result_dataset = list(result)
      assert len(result_dataset) == 5
      assert result_dataset == [Tensor([x]) for x in [0, 2, 4, 6, 8]]
      assert isinstance(result.sampler, DistributedSamplerWrapper)
      assert isinstance(result.sampler.dataset._sampler, CustomSampler)
  
imports: from re import escape
  from typing import Sized
  from unittest import mock
  from unittest.mock import Mock
  import lightning.fabric
  import pytest
  from lightning.fabric.utilities.distributed import DistributedSamplerWrapper
  from lightning.fabric.utilities.warnings import PossibleUserWarning
  from lightning.pytorch import Trainer
  from lightning.pytorch.demos.boring_classes import BoringDataModule, BoringModel, RandomDataset
  from lightning.pytorch.trainer.connectors.data_connector import _check_dataloader_iterable, _DataHookSelector, _DataLoaderSource, _worker_check, warning_cache
  from lightning.pytorch.trainer.states import RunningStage, TrainerFn
  from lightning.pytorch.utilities.combined_loader import CombinedLoader
  from lightning.pytorch.utilities.data import _update_dataloader
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning_utilities.test.warning import no_warning_call
  from torch import Tensor
  from torch.utils.data import BatchSampler, DataLoader, DistributedSampler, Sampler, SequentialSampler
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 8 for calling a global function with example usage: test_dataloader_reinit_for_subclass() and returns: len, iter