Skip to content

docs(constraints): add docstrings to constraint classes and Module.register_constraint - #2760

Merged
gpleiss merged 4 commits into
cornellius-gp:mainfrom
umi008:fix-640-constraint-docs
Jul 10, 2026
Merged

docs(constraints): add docstrings to constraint classes and Module.register_constraint#2760
gpleiss merged 4 commits into
cornellius-gp:mainfrom
umi008:fix-640-constraint-docs

Conversation

@umi008

@umi008 umi008 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Closes #640

Context

The Interval class in gpytorch/constraints/constraints.py and the Module.register_constraint method in gpytorch/module.py had 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 at docs/source/constraints.rst had no introductory overview, and the in-class docstring of Interval.transform contained a stale typo (self.transform should reference self._transform).

This issue is tagged good first issue and help wanted since 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

File Change
gpytorch/constraints/constraints.py Expand Interval.__init__ docstring; add docstrings to GreaterThan, Positive, LessThan, and Interval.inverse_transform / initial_value; fix stale self.transform typo in Interval.transform
gpytorch/module.py Full sphinx-style docstring (Args, Raises, Example) for Module.register_constraint
docs/source/constraints.rst Add overview, list of constraint classes, and a worked usage example showing register_constraint

All docstrings use the sphinx style (per CONTRIBUTING.md), not Google-style.

Test plan

  • All 20 tests in test/constraints/test_constraints.py pass
  • Manual functional sanity check: Interval transform and inverse_transform round-trip correctly; register_constraint sets up a softplus-encoded positive parameter
  • pre-commit run --files gpytorch/constraints/constraints.py gpytorch/module.py docs/source/constraints.rst passes
  • sphinx build: cd docs && SKIP_EXAMPLES=true make html should produce no new warnings (per CONTRIBUTING.md zero-warning policy; verified in CI)

umi008 added 4 commits July 9, 2026 13:07
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
gpleiss merged commit 7c80665 into cornellius-gp:main Jul 10, 2026
7 checks passed
@gpleiss

gpleiss commented Jul 10, 2026

Copy link
Copy Markdown
Member

Thanks @umi008 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Docs] Docs for constraints are missing

2 participants