Skip to content

Commit

Permalink
[Typing] not render type ignore (#6680)
Browse files Browse the repository at this point in the history
  • Loading branch information
megemini authored Jun 14, 2024
1 parent f6da494 commit 86e31cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ci_scripts/doc-build-config/en/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,20 @@ def handle_api_aliases():
exec(f'{oname}.__name__ = "{tn}"')


def remove_doctest_directives(app, what, name, obj, options, lines):
def remove_directives(app, what, name, obj, options, lines):
"""
Remove `doctest` directives from docstring
Remove `doctest` and `mypy` `type: ignore: xxx` directives from docstring
"""
# Modify the lines inplace
# remove doctest directive
pattern_doctest = re.compile(r"\s*>>>\s*#\s*x?doctest:\s*.*")
lines[:] = [line for line in lines if not pattern_doctest.match(line)]
# remove `# type: ignore ...`
pattern_typing_ignore = re.compile(r"# type:[^#]*")
lines[:] = [
pattern_typing_ignore.sub("", line)
for line in lines
if not pattern_doctest.match(line)
]

# remove blank ps(`>>>`)
lines[:] = [line for line in lines if not line.strip() == ">>>"]
Expand Down Expand Up @@ -398,4 +404,4 @@ def setup(app):
app.add_transform(AutoStructify)

# remove doctest directives and blank ps
app.connect("autodoc-process-docstring", remove_doctest_directives)
app.connect("autodoc-process-docstring", remove_directives)
6 changes: 6 additions & 0 deletions docs/api/copy_codes_from_en_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def instert_codes_into_cn_rst_if_need(cnrstfilename):
rst_lines, copy_from_info = read_rst_lines_and_copy_info(cnrstfilename)
update_needed = False
pattern_doctest = re.compile(r"\s*>>>\s*#\s*doctest:\s*.*")
pattern_typing_ignore = re.compile(r"# type:[^#]*")

if copy_from_info:
logger.info(
Expand All @@ -266,6 +267,11 @@ def instert_codes_into_cn_rst_if_need(cnrstfilename):
cb_new.append("")
indent += 4
for line in cb_need["codes"].splitlines():
# remove `# type: ignore ...`
line = pattern_typing_ignore.sub("", line)
if line.strip() == ">>>":
continue

if not pattern_doctest.match(line):
cb_new.append(" " * indent + line)

Expand Down

0 comments on commit 86e31cc

Please sign in to comment.