Skip to content

Commit 73ee804

Browse files
authored
Reference Docs (#1684)
- Use `griffe` and some custom templating code to emit clean markdown files for our reference docs - Go through docstrings, fix issues, add additional information
1 parent dc073cd commit 73ee804

File tree

74 files changed

+2262
-526
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2262
-526
lines changed

check/mypy-dev-tools

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cd "${topdir}" || exit $?
1515
CONFIG_FILE='mypy.ini'
1616

1717
echo -e -n "\033[31m"
18-
mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" dev_tools/
18+
mypy --config-file=dev_tools/conf/$CONFIG_FILE --no-namespace-packages "$@" dev_tools/
1919
result=$?
2020
echo -e -n "\033[0m"
2121

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from qualtran_dev_tools.git_tools import get_git_root
16+
from qualtran_dev_tools.make_reference_docs import make_reference_docs
17+
18+
if __name__ == "__main__":
19+
make_reference_docs(get_git_root())

dev_tools/conf/mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ignore_missing_imports = true
1616
# 3rd-party libs for which we don't have stubs
1717

1818
# Google
19-
[mypy-google.api_core.*,google.auth.*,google.colab.*,google.protobuf.text_format.*,google.cloud.*]
19+
[mypy-google.api_core.*,google.auth.*,google.colab.*,google.protobuf.text_format.*,google.cloud.*,google.*]
2020
follow_imports = silent
2121
ignore_missing_imports = true
2222

dev_tools/execute-notebooks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
def parse_args():
2121
p = argparse.ArgumentParser()
2222
p.add_argument('--output-nbs', action=argparse.BooleanOptionalAction, default=True)
23-
p.add_argument('--output-html', action=argparse.BooleanOptionalAction, default=False)
23+
p.add_argument('--output-md', action=argparse.BooleanOptionalAction, default=False)
2424
p.add_argument('--only-out-of-date', action=argparse.BooleanOptionalAction, default=True)
2525
args = p.parse_args()
2626
execute_and_export_notebooks(
27-
output_nbs=args.output_nbs,
28-
output_html=args.output_html,
29-
only_out_of_date=args.only_out_of_date,
27+
output_nbs=args.output_nbs, output_md=args.output_md, only_out_of_date=args.only_out_of_date
3028
)
3129

3230

dev_tools/qualtran_dev_tools/jupyter_autogen.py

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import abc
1818
import inspect
19-
import io
2019
import re
2120
import textwrap
2221
from pathlib import Path
@@ -29,6 +28,7 @@
2928
from qualtran import BloqDocSpec, BloqExample
3029

3130
from .parse_docstrings import get_markdown_docstring_lines
31+
from .write_if_different import WriteIfDifferent
3232

3333
_IMPORTS = """\
3434
from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register
@@ -310,84 +310,6 @@ def _init_notebook(
310310
return nb, nb_path
311311

312312

313-
class WriteIfDifferent:
314-
"""A file-like object that only writes to disk if the new content
315-
differs from the existing content.
316-
317-
Args:
318-
path: The path to write, which may already exist.
319-
"""
320-
321-
def __init__(self, path: Path):
322-
self.path = path
323-
self._buffer = io.StringIO()
324-
325-
def write(self, s: str):
326-
return self._buffer.write(s)
327-
328-
def writelines(self, lines):
329-
for line in lines:
330-
self.write(line)
331-
332-
def flush(self):
333-
self._buffer.flush()
334-
335-
def close(self):
336-
"""Closes the adapter.
337-
338-
This triggers the comparison of buffered content
339-
with the disk file's content and writes to disk only if different.
340-
"""
341-
new_content = self._buffer.getvalue()
342-
self._buffer.close()
343-
344-
existing_content = None
345-
if self.path.is_file():
346-
with self.path.open('r') as f_read:
347-
existing_content = f_read.read()
348-
if new_content == existing_content:
349-
print(f"{self.path} unchanged.")
350-
return
351-
352-
with self.path.open('w') as f_write:
353-
f_write.write(new_content)
354-
355-
def __enter__(self):
356-
return self
357-
358-
def __exit__(self, exc_type, exc_val, exc_tb):
359-
self.close()
360-
# Do not suppress exceptions from the 'with' block body.
361-
return False
362-
363-
@property
364-
def closed(self):
365-
return self._buffer.closed
366-
367-
def readable(self):
368-
"""Returns False, as this adapter is write-only like a file from `open('w')`."""
369-
return False
370-
371-
def writable(self):
372-
"""Returns True if the adapter is not closed, False otherwise."""
373-
return self._buffer.writable()
374-
375-
def seekable(self):
376-
"""Returns False, as this adapter is not seekable like a disk file opened in 'w' mode."""
377-
return False
378-
379-
def tell(self):
380-
"""Returns the current stream position in the internal buffer."""
381-
return self._buffer.tell()
382-
383-
def truncate(self, size=None):
384-
"""
385-
Resizes the internal buffer to the given number of bytes.
386-
If size is not specified, resizes to the current position.
387-
"""
388-
return self._buffer.truncate(size)
389-
390-
391313
def render_notebook(nbspec: NotebookSpecV2) -> None:
392314
# 1. get a notebook (existing or empty)
393315
nb, nb_path = _init_notebook(path_stem=nbspec.path_stem, directory=nbspec.directory)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ._make import make_reference_docs
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from typing import Dict
15+
16+
17+
def get_aliases_str(pref_dotpath: str, *, aliases_d: Dict[str, str]) -> str:
18+
"""From a complete dictionary of aliases, return a formatted string with our aliases.
19+
20+
This will return just `pref_dotpath` if there are none,
21+
otherwise '{pref} **Alias(es):** {...}'
22+
"""
23+
aliases = [k for k, v in aliases_d.items() if v == pref_dotpath and k != pref_dotpath]
24+
s = f'`{pref_dotpath}`'
25+
alias_str = ', '.join(f'`{a}`' for a in aliases)
26+
if len(aliases) > 1:
27+
s += f". **Aliases:** {alias_str}"
28+
elif len(aliases) == 1:
29+
s += f". **Alias:** {alias_str}"
30+
return s

0 commit comments

Comments
 (0)