code: def test_invalid_lr_scheduler_with_custom_step_method(override):
      """Test that custom lr scheduler raises an error if it doesn't follow PyTorch LR Scheduler API."""
  
      class CustomScheduler:
          def __init__(self, optimizer):
              self.optimizer = optimizer
  
          def step(
              self, foobar
          ):  # breaks the API, forces user to override `lr_scheduler_step`
              ...
  
          def state_dict(self):
              ...
  
          def load_state_dict(self, state_dict):
              ...
  
      class CustomBoringModel(BoringModel):
          def configure_optimizers(self):
              opt = torch.optim.SGD(self.parameters(), lr=1e-2)
              lr_scheduler = CustomScheduler(opt)
              return {"optimizer": opt, "lr_scheduler": lr_scheduler}
  
      model = CustomBoringModel()
      model.trainer = Trainer()
      if override:
  
          def lr_scheduler_step(*_):
              ...
  
          # the user did override the hook, no error
          model.lr_scheduler_step = lr_scheduler_step
          _init_optimizers_and_lr_schedulers(model)
      else:
          with pytest.raises(
              MisconfigurationException,
              match="CustomScheduler` doesn't follow",
          ):
              _init_optimizers_and_lr_schedulers(model)
  
imports: from unittest import mock
  from unittest.mock import call
  import pytest
  import torch
  from lightning.pytorch import Trainer
  from lightning.pytorch.callbacks import ModelCheckpoint
  from lightning.pytorch.core.optimizer import _configure_optimizers, _configure_schedulers_automatic_opt, _init_optimizers_and_lr_schedulers
  from lightning.pytorch.demos.boring_classes import BoringDataModule, BoringModel
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.types import LRSchedulerConfig
  from torch import optim
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 27 for calling a global function with example usage: test_invalid_lr_scheduler_with_custom_step_method(override) and returns: dict