pytorch-lightning icon indicating copy to clipboard operation
pytorch-lightning copied to clipboard

Fix: Respect required=False in add_lightning_class_args when subclass_mode=False

Open KAVYANSHTYAGI opened this issue 7 months ago • 0 comments

What does this PR do?

This PR fixes an inconsistency in LightningCLI.add_lightning_class_args() where the required=False flag was being ignored when subclass_mode=False (which is the default). According to the documentation, setting required=False should make providing the corresponding config optional — but in practice, argument registration was enforced regardless, leading to errors if required constructor parameters (e.g., dirpath in ModelCheckpoint) were not provided.

This change adds a conditional check that skips argument registration when:

subclass_mode=False

required=False

and the config key (nested_key) is not provided by the user

Additionally, a test case was added to verify that this behavior works as expected.

Fixes #20851 https://github.com/Lightning-AI/pytorch-lightning/issues/20851

Before submitting

Was this discussed/agreed via a GitHub issue? yes

Did you read the contributor guideline, Pull Request section? yes

Did you make sure your PR does only one thing, instead of bundling different changes together? yes

Did you write any new necessary tests? yes

Did you verify new and existing tests pass locally with your changes? yes

Did you update the documentation if necessary?

Did you list all the breaking changes introduced by this pull request?

Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.

Yes :)

📚 Documentation preview 📚: https://pytorch-lightning--20856.org.readthedocs.build/en/20856/

KAVYANSHTYAGI avatar May 25 '25 04:05 KAVYANSHTYAGI