Skip to content

Commit f74813d

Browse files
MarkDaoustcopybara-github
authored andcommitted
Fix broken caching for custom page_info classes.
This is why the raw_ops page is broken: https://www.tensorflow.org/api_docs/python/tf/raw_ops PiperOrigin-RevId: 437232519
1 parent 2da2058 commit f74813d

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def write_docs(
563563

564564
try:
565565
path.parent.mkdir(exist_ok=True, parents=True)
566-
path.write_text(page_info.text, encoding='utf-8')
566+
path.write_text(page_info.page_text, encoding='utf-8')
567567
num_docs_output += 1
568568
except OSError as e:
569569
raise OSError('Cannot write documentation for '

tools/tensorflow_docs/api_generator/pretty_docs/base_page.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# ==============================================================================
1515
"""Base classes for page construction."""
1616
import abc
17-
import functools
17+
import os
1818
import pathlib
19+
import posixpath
1920
import textwrap
2021
from typing import Any, ClassVar, Dict, List, NamedTuple, Optional, Sequence, Tuple, Type
2122

@@ -103,6 +104,7 @@ class PageInfo:
103104
search_hints: If true include metadata search hints, else include a
104105
"robots: noindex"
105106
text: The resulting page text.
107+
page_text: The cached result.
106108
"""
107109
DEFAULT_BUILDER_CLASS: ClassVar[Type[PageBuilder]] = TemplatePageBuilder
108110

@@ -122,6 +124,7 @@ def __init__(
122124
that need to be added to the markdown pages created.
123125
search_hints: If true include metadata search hints, else include a
124126
"robots: noindex"
127+
125128
"""
126129
self.full_name = full_name
127130
self.py_object = py_object
@@ -131,22 +134,53 @@ def __init__(
131134
self._defined_in = None
132135
self._aliases = None
133136
self._doc = None
134-
self._text = None
137+
self._page_text = None
135138

136139
def collect_docs(self, parser_config: config.ParserConfig):
137140
"""Collects additional information from the `config.ParserConfig`."""
138141
pass
139142

143+
def docs_for_object(self, parser_config):
144+
duplicate_names = parser_config.duplicates.get(self.full_name, [])
145+
if self.full_name in duplicate_names:
146+
duplicate_names.remove(self.full_name)
147+
148+
relative_path = os.path.relpath(
149+
path='.',
150+
start=os.path.dirname(parser.documentation_path(self.full_name)) or '.')
151+
152+
# Convert from OS-specific path to URL/POSIX path.
153+
relative_path = posixpath.join(*relative_path.split(os.path.sep))
154+
155+
with parser_config.reference_resolver.temp_prefix(relative_path):
156+
self.set_doc(
157+
parser.parse_md_docstring(
158+
self.py_object,
159+
self.full_name,
160+
parser_config,
161+
self._extra_docs,
162+
))
163+
164+
self.collect_docs(parser_config)
165+
166+
self.set_aliases(duplicate_names)
167+
168+
self.set_defined_in(parser.get_defined_in(self.py_object, parser_config))
169+
170+
self._page_text = self.build()
171+
172+
return self._page_text
173+
140174
def build(self) -> str:
141175
"""Builds the documentation."""
142176
cls = self.DEFAULT_BUILDER_CLASS
143-
text = cls(self).build()
144-
self._text = text
145-
return text
177+
return cls(self).build()
146178

147179
@property
148-
def text(self):
149-
return self._text
180+
def page_text(self):
181+
if self._page_text is None:
182+
self._page_text = self.build()
183+
return self._page_text
150184

151185
def __eq__(self, other):
152186
if isinstance(other, PageInfo):
@@ -198,7 +232,6 @@ def set_doc(self, doc: parser.DocstringInfo):
198232
self._doc = doc
199233

200234

201-
202235
class MemberInfo(NamedTuple):
203236
"""Describes an attribute of a class or module."""
204237
short_name: str

tools/tensorflow_docs/api_generator/pretty_docs/docs_for_object.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515
"""Create a `pretty_docs.base_page.PageInfo` from a python object."""
16-
import os
17-
import posixpath
18-
1916
from typing import Any, Dict, Optional, Type
2017

2118
from tensorflow_docs.api_generator import config
2219
from tensorflow_docs.api_generator import doc_controls
2320
from tensorflow_docs.api_generator import obj_type as obj_type_lib
24-
from tensorflow_docs.api_generator import parser
2521
from tensorflow_docs.api_generator.pretty_docs import base_page
2622
from tensorflow_docs.api_generator.pretty_docs import class_page
2723
from tensorflow_docs.api_generator.pretty_docs import function_page
@@ -75,9 +71,6 @@ def docs_for_object(
7571

7672
# Which other aliases exist for the object referenced by full_name?
7773
main_name = parser_config.reference_resolver.py_main_name(full_name)
78-
duplicate_names = parser_config.duplicates.get(main_name, [])
79-
if main_name in duplicate_names:
80-
duplicate_names.remove(main_name)
8174

8275
if page_builder_classes is None:
8376
page_builder_classes = _DEFAULT_PAGE_BUILDER_CLASSES
@@ -93,28 +86,6 @@ def docs_for_object(
9386
search_hints=search_hints,
9487
extra_docs=extra_docs)
9588

96-
relative_path = os.path.relpath(
97-
path='.',
98-
start=os.path.dirname(parser.documentation_path(full_name)) or '.')
99-
100-
# Convert from OS-specific path to URL/POSIX path.
101-
relative_path = posixpath.join(*relative_path.split(os.path.sep))
102-
103-
with parser_config.reference_resolver.temp_prefix(relative_path):
104-
page_info.set_doc(
105-
parser.parse_md_docstring(
106-
py_object,
107-
full_name,
108-
parser_config,
109-
extra_docs,
110-
))
111-
112-
page_info.collect_docs(parser_config)
113-
114-
page_info.set_aliases(duplicate_names)
115-
116-
page_info.set_defined_in(parser.get_defined_in(py_object, parser_config))
117-
118-
page_info.build()
89+
page_info.docs_for_object(parser_config)
11990

12091
return page_info

0 commit comments

Comments
 (0)