Skip to content

Add recommendations for documenting Starlark APIs#320

Open
tetromino wants to merge 5 commits into
bazelbuild:masterfrom
tetromino:conventions
Open

Add recommendations for documenting Starlark APIs#320
tetromino wants to merge 5 commits into
bazelbuild:masterfrom
tetromino:conventions

Conversation

@tetromino
Copy link
Copy Markdown
Collaborator

Formalize the basics of what e.g. Stardoc expects from .bzl files.

Working towards bazelbuild/stardoc#202

Formalize the basics of what e.g. Stardoc expects from .bzl files.
@tetromino tetromino requested a review from comius August 12, 2025 18:45
@tetromino tetromino requested a review from brandjon as a code owner August 12, 2025 18:45
Comment thread conventions.md Outdated
Comment thread conventions.md
Comment thread conventions.md Outdated
Comment thread conventions.md Outdated
Comment thread conventions.md Outdated
Comment thread conventions.md
Comment thread conventions.md Outdated
```

If both the leading and trailing doc comment is specified, the trailing doc
comment takes precedence.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that Sphinx behavior? Seems error prone -- the special #: notation combined with the requirement that there be no intervening lines is a pretty clear signal that both comments are intended as documentation, so I don't see the utility of not failing fast.

I'd consider this to be in the same category as double-documenting the same variable at two different assignments (usually only possible in the local case, since standard Starlark dialect bans global reassignment).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, preferring the trailing comment is Sphinx behavior.

I suppose I should omit the statement about precedence entirely, since this is a recommendation doc - and users should not do that.

Comment thread conventions.md Outdated
docstring](https://peps.python.org/pep-0257/) - a string literal which is the
first line of the function's body.
API documentation for a Starlark module or function should be provided in a
*docstring* - a string literal which is the first line of the module or of the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"first line of" -> "first statement of" (allows for intervening comments and whitespace)

Comment thread conventions.md Outdated
Documentation processors should take care to dedent common leading whitespace
from a multiline docstring's lines (note that the first line could have no
leading whitespace).
No particular markup format for the text is prescribed, but Markdown and HTML
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify that applications and organizations may specify additional best practices (for readers who might otherwise take this doc as definitive for google3 or for .bzl).

Comment thread conventions.md
Comment thread conventions.md Outdated

An uninterrupted block of doc comments attaches to the symbol on the left hand
side of the immediately following assignment statement:
An uninterrupted block of doc comments attaches to the symbol(s) on the left
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"uninterrupted" -- is it clear that there can't be a blank line between the comment and assignment statement?

#: doesn't work

x =1

#: but this does
#:
y = 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a non-trailing comment must not trail some other statement, as in

pass  #: stuff about x
x  = 1

(Verify that's consistent with sphinx.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified text.

Comment thread conventions.md Outdated
An uninterrupted block of doc comments attaches to the symbol on the left hand
side of the immediately following assignment statement:
An uninterrupted block of doc comments attaches to the symbol(s) on the left
hand side of the immediately following assignment statement:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to specify anything about what happens to a doc comment that cannot be associated with any variable/assignment? I.e., floating free, or in the degenerate but valid assignments:

[] = []
() = ()

My guess is it's fine to not say anything specific.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, we could make it fail-fast on docgen. But that sounds annoying for commenting out variable declarations, like how Go requires you to update imports during experimental debugging edits. And it's not like the doc comment would accidentally apply to a different var when the first one is commented out.

Comment thread conventions.md Outdated
```

If both the leading and trailing doc comment is specified, the trailing doc
comment takes precedence.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update for offline discussion wherein we said it's illegal to specify a doc comment twice for the same var, including cases where global variable reassignment is allowed (not standard but permissible in some implementations), and in the case where there's both a preceding and trailing doc comment.

"illegal" in this context means for doc purposes, not rejecting the program at the parser/evaluator level (or else it should be in spec.md).

Also, since you apply doc comments to all LHS vars, you may have this issue in the form

#: doc for x and y
x, y = ...
#: doc for y and z
y, z = ...

But only when reassignment of y is allowed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified. I don't think it's worth specifically mentioning multiple assignment - just say that a documentation processor is allowed to error out if it sees multiple doc comments on the same variable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"allowed to error out" -- and if it doesn't, it might take only the first, or only the last, or concatenate them? If that's the case, it's basically "unspecified behavior". Which might be alright, since this shouldn't come up in practice.

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.

2 participants