Repository: Lightning-AI/pytorch-lightning
File: tests/trainer/test_optimizers.py (Python)

Code before:
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert optim == [opt_a, opt_b]
    assert len(lr_sched) == len(freq) == 0

    # opt tuple of 2 lists
    model.configure_optimizers = lambda: ([opt_a], [scheduler_a])
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1 and len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt tuple of 1 list
    model.configure_optimizers = lambda: ([opt_a], scheduler_a)
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1 and len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt single dictionary
    model.configure_optimizers = lambda: {"optimizer": opt_a, "lr_scheduler": scheduler_a}
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1 and len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt multiple dictionaries with frequencies
    model.configure_optimizers = lambda: (
        {"optimizer": opt_a, "lr_scheduler": scheduler_a, "frequency": 1},
        {"optimizer": opt_b, "lr_scheduler": scheduler_b, "frequency": 5},
    )
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == len(freq) == 2
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )
    assert freq == [1, 5]


def test_none_optimizer_warning():

    trainer = Trainer()

    model = EvalModelTemplate()
    model.configure_optimizers = model.configure_optimizers__empty

Review comment (suggestion):
```suggestion
    assert len(optim) == len(lr_sched) == 1
    assert len(freq) == 0
```

Code after:
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert optim == [opt_a, opt_b]
    assert len(lr_sched) == len(freq) == 0

    # opt tuple of 2 lists
    model.configure_optimizers = lambda: ([opt_a], [scheduler_a])
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1
    assert len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt tuple of 1 list
    model.configure_optimizers = lambda: ([opt_a], scheduler_a)
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1
    assert len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt single dictionary
    model.configure_optimizers = lambda: {"optimizer": opt_a, "lr_scheduler": scheduler_a}
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == 1
    assert len(freq) == 0
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )

    # opt multiple dictionaries with frequencies
    model.configure_optimizers = lambda: (
        {"optimizer": opt_a, "lr_scheduler": scheduler_a, "frequency": 1},
        {"optimizer": opt_b, "lr_scheduler": scheduler_b, "frequency": 5},
    )
    optim, lr_sched, freq = trainer.init_optimizers(model)
    assert len(optim) == len(lr_sched) == len(freq) == 2
    assert optim[0] == opt_a
    assert lr_sched[0] == dict(
        scheduler=scheduler_a, interval='epoch', frequency=1, reduce_on_plateau=False, monitor=None, strict=True
    )
    assert freq == [1, 5]


def test_none_optimizer_warning():

    trainer = Trainer()