Skip to content

Commit

Permalink
docstrings for type aliases in devsite API docs (#3275)
Browse files Browse the repository at this point in the history
The devsite API docs generation script doesn't pick up docstrings for our type aliases currently. This fixes it. 
Inspired by https://github.com/tensorflow/tensorflow/blob/40a314cab8d6c7afbc62456433a033c1cab8d74f/tensorflow/python/types/doc_typealias.py#L24:5
  • Loading branch information
balopat committed Aug 27, 2020
1 parent 749468b commit 39e6fb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cirq/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Workaround for associating docstrings with public constants."""

from typing import Any, Dict, NamedTuple, Optional

DocProperties = NamedTuple(
Expand Down Expand Up @@ -44,4 +45,20 @@ def document(value: Any, doc_string: Optional[str] = None):
"""
docs = DocProperties(doc_string=doc_string)
RECORDED_CONST_DOCS[id(value)] = docs

## this is how the devsite API generator picks up
## docstrings for type aliases
try:
value.__doc__ = doc_string
except AttributeError:
# we have a couple (~ 7) global constants of type list, tuple, dict,
# that fail here as their __doc__ attribute is read-only.
# For the time being these are not going to be part of the generated
# API docs. See https://github.com/quantumlib/Cirq/issues/3276 for
# more info.

# to list them, uncomment these lines and run `import cirq`:
# print(f"WARNING: {e}")
# print(traceback.format_stack(limit=2)[0])
pass
return value
2 changes: 1 addition & 1 deletion cirq/google/gate_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
}

document(NAMED_GATESETS,
""""A convenience mapping from gateset names to gatesets""")
"""A convenience mapping from gateset names to gatesets""")

GOOGLE_GATESETS = [
SYC_GATESET,
Expand Down

0 comments on commit 39e6fb8

Please sign in to comment.