docs(constraints): add docstrings to constraint classes and Module.register_constraint - #2760
Merged
Merged
Conversation
Adds Args, Returns, and Example sections to: - Interval.__init__ (expanded existing minimal docstring) - Interval.transform (fix typo: self.transform -> self._transform) - Interval.inverse_transform - Interval.initial_value - GreaterThan.__init__ - Positive.__init__ - LessThan.__init__ The Interval docstring also documents the transform/inv_transform/initial_value parameters that were previously undocumented. The typo at Interval.transform's docstring (line 76) is corrected: the constraint applies self._transform, not self.transform. Refs #640. Sphinx-style docstrings per CONTRIBUTING.md.
The Interval class docstring says 'see gpytorch.module.Module.register_constraint for usage details' but that method had no docstring. Fill in the gap with a full sphinx docstring covering Args (param_name, constraint, replace), Raises, and a worked Example showing a custom constrained parameter via a small Module subclass. Refs #640.
The auto-generated page was just a list of classes with no context. Add: - A short overview of when to use constraints and how they interact with parameter storage and the optimizer. - A bullet list of the available constraint classes (Positive, GreaterThan, LessThan, Interval). - A worked example showing how to register a Positive constraint on a custom parameter and read the constrained value. Refs #640.
The previous title 'Parameter Constraints (full reference)' was 38 chars but the underline was only 29 dashes. With fail_on_warning: true in .readthedocs.yml, sphinx rejected the build. Renamed the second section header to 'Constraint Reference' (20 chars, fits the 29-dash underline) which also avoids the duplicate 'Parameter Constraints' header in the page TOC and matches the descriptive section-naming convention used in kernels.rst and models.rst.
gpleiss
approved these changes
Jul 10, 2026
Member
|
Thanks @umi008 ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #640
Context
The
Intervalclass ingpytorch/constraints/constraints.pyand theModule.register_constraintmethod ingpytorch/module.pyhad minimal or no docstrings, making it hard for users to discover how to use custom constraints (e.g. softplus-encoded positive or interval-bounded parameters). The current docs page atdocs/source/constraints.rsthad no introductory overview, and the in-class docstring ofInterval.transformcontained a stale typo (self.transformshould referenceself._transform).This issue is tagged
good first issueandhelp wantedsince 2019; two prior PRs (#2252, #2256) were opened by an external contributor but did not merge. The split into three focused commits below is intended to be more review-friendly.Changes
gpytorch/constraints/constraints.pyInterval.__init__docstring; add docstrings toGreaterThan,Positive,LessThan, andInterval.inverse_transform/initial_value; fix staleself.transformtypo inInterval.transformgpytorch/module.pyModule.register_constraintdocs/source/constraints.rstregister_constraintAll docstrings use the sphinx style (per CONTRIBUTING.md), not Google-style.
Test plan
test/constraints/test_constraints.pypassIntervaltransformandinverse_transformround-trip correctly;register_constraintsets up a softplus-encoded positive parameterpre-commit run --files gpytorch/constraints/constraints.py gpytorch/module.py docs/source/constraints.rstpassescd docs && SKIP_EXAMPLES=true make htmlshould produce no new warnings (per CONTRIBUTING.md zero-warning policy; verified in CI)