Fix: Respect required=False in add_lightning_class_args when subclass_mode=False #20856
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Additionally, a test case was added to verify that this behavior works as expected.
Fixes #20851 #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)
Anyone in the community is welcome to review the PR.
Yes :)📚 Documentation preview 📚: https://pytorch-lightning--20856.org.readthedocs.build/en/20856/