Skip to content

Commit 3752702

Browse files
MarkDaoustcopybara-github
authored andcommitted
Split parser.py: move obj_type out of parser.py
PiperOrigin-RevId: 416572714
1 parent acb9d0d commit 3752702

File tree

2 files changed

+80
-55
lines changed

2 files changed

+80
-55
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Lint as: python3
2+
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ==============================================================================
16+
"""Turn Python docstrings into Markdown for TensorFlow documentation."""
17+
18+
19+
import enum
20+
import inspect
21+
22+
from typing import Any
23+
24+
25+
class ObjType(enum.Enum):
26+
"""Enum to standardize object type checks."""
27+
TYPE_ALIAS = 'type_alias'
28+
MODULE = 'module'
29+
CLASS = 'class'
30+
CALLABLE = 'callable'
31+
# properties or any `descriptor`
32+
PROPERTY = 'property'
33+
OTHER = 'other'
34+
35+
@classmethod
36+
def get(cls, py_obj: Any) -> 'ObjType':
37+
"""Get the `ObjType` for the `py_object`."""
38+
if (getattr(py_obj, '__args__', None) and
39+
getattr(py_obj, '__origin__', None)):
40+
return cls.TYPE_ALIAS
41+
elif inspect.ismodule(py_obj):
42+
return cls.MODULE
43+
elif inspect.isclass(py_obj):
44+
return cls.CLASS
45+
elif callable(py_obj):
46+
return cls.CALLABLE
47+
elif hasattr(py_obj, '__get__'):
48+
# This handles any descriptor not only properties.
49+
# https://docs.python.org/3/howto/descriptor.html
50+
return cls.PROPERTY
51+
else:
52+
return cls.OTHER

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,12 @@
3838
from tensorflow_docs.api_generator import config
3939
from tensorflow_docs.api_generator import doc_controls
4040
from tensorflow_docs.api_generator import doc_generator_visitor
41+
from tensorflow_docs.api_generator import obj_type as obj_type_lib
4142
from tensorflow_docs.api_generator import public_api
4243

4344
from google.protobuf.message import Message as ProtoMessage
4445

4546

46-
class ObjType(enum.Enum):
47-
"""Enum to standardize object type checks."""
48-
TYPE_ALIAS = 'type_alias'
49-
MODULE = 'module'
50-
CLASS = 'class'
51-
CALLABLE = 'callable'
52-
# properties or any `descriptor`
53-
PROPERTY = 'property'
54-
OTHER = 'other'
55-
56-
57-
def get_obj_type(py_obj: Any) -> ObjType:
58-
"""Get the `ObjType` for the `py_object`."""
59-
if getattr(py_obj, '__args__', None) and getattr(py_obj, '__origin__', None):
60-
return ObjType.TYPE_ALIAS
61-
elif inspect.ismodule(py_obj):
62-
return ObjType.MODULE
63-
elif inspect.isclass(py_obj):
64-
return ObjType.CLASS
65-
elif callable(py_obj):
66-
return ObjType.CALLABLE
67-
elif hasattr(py_obj, '__get__'):
68-
# This handles any descriptor not only properties.
69-
# https://docs.python.org/3/howto/descriptor.html
70-
return ObjType.PROPERTY
71-
else:
72-
return ObjType.OTHER
73-
74-
7547
@dataclasses.dataclass
7648
class _FileLocation(object):
7749
"""This class indicates that the object is defined in a regular file.
@@ -152,12 +124,12 @@ def _get_raw_docstring(py_object):
152124
The docstring, or the empty string if no docstring was found.
153125
"""
154126

155-
if get_obj_type(py_object) is ObjType.TYPE_ALIAS:
127+
if obj_type_lib.ObjType.get(py_object) is obj_type_lib.ObjType.TYPE_ALIAS:
156128
if inspect.getdoc(py_object) != inspect.getdoc(py_object.__origin__):
157129
result = inspect.getdoc(py_object)
158130
else:
159131
result = ''
160-
elif get_obj_type(py_object) is not ObjType.OTHER:
132+
elif obj_type_lib.ObjType.get(py_object) is not obj_type_lib.ObjType.OTHER:
161133
result = inspect.getdoc(py_object) or ''
162134
else:
163135
result = ''
@@ -329,10 +301,11 @@ def from_visitor(cls, visitor, **kwargs):
329301
"""
330302
is_fragment = {}
331303
for full_name, obj in visitor.index.items():
332-
obj_type = get_obj_type(obj)
333-
if obj_type in (ObjType.CLASS, ObjType.MODULE):
304+
obj_type = obj_type_lib.ObjType.get(obj)
305+
if obj_type in (obj_type_lib.ObjType.CLASS, obj_type_lib.ObjType.MODULE):
334306
is_fragment[full_name] = False
335-
elif obj_type in (ObjType.CALLABLE, ObjType.TYPE_ALIAS):
307+
elif obj_type in (obj_type_lib.ObjType.CALLABLE,
308+
obj_type_lib.ObjType.TYPE_ALIAS):
336309
if is_class_attr(full_name, visitor.index):
337310
is_fragment[full_name] = True
338311
else:
@@ -924,7 +897,7 @@ def _get_other_member_doc(
924897
# breaks on the site.
925898
info = pprint.pformat(obj).replace('`', r'\`')
926899
info = f'`{info}`'
927-
elif get_obj_type(obj) is ObjType.PROPERTY:
900+
elif obj_type_lib.ObjType.get(obj) is obj_type_lib.ObjType.PROPERTY:
928901
info = None
929902
else:
930903
class_full_name = parser_config.reverse_index.get(id(type(obj)), None)
@@ -967,7 +940,7 @@ def _parse_md_docstring(
967940
A _DocstringInfo object, all fields will be empty if no docstring was found.
968941
"""
969942

970-
if get_obj_type(py_object) is ObjType.OTHER:
943+
if obj_type_lib.ObjType.get(py_object) is obj_type_lib.ObjType.OTHER:
971944
raw_docstring = _get_other_member_doc(
972945
obj=py_object, parser_config=parser_config, extra_docs=extra_docs)
973946
else:
@@ -2087,17 +2060,17 @@ def _add_member(
20872060
parser_config: config.ParserConfig,
20882061
) -> None:
20892062
"""Adds a member to the class page."""
2090-
obj_type = get_obj_type(member_info.py_object)
2063+
obj_type = obj_type_lib.ObjType.get(member_info.py_object)
20912064

2092-
if obj_type is ObjType.PROPERTY:
2065+
if obj_type is obj_type_lib.ObjType.PROPERTY:
20932066
self._add_property(member_info)
2094-
elif obj_type is ObjType.CLASS:
2067+
elif obj_type is obj_type_lib.ObjType.CLASS:
20952068
if defining_class is None:
20962069
return
20972070
self._add_class(member_info)
2098-
elif obj_type is ObjType.CALLABLE:
2071+
elif obj_type is obj_type_lib.ObjType.CALLABLE:
20992072
self._add_method(member_info, defining_class, parser_config)
2100-
elif obj_type is ObjType.OTHER:
2073+
elif obj_type is obj_type_lib.ObjType.OTHER:
21012074
# Exclude members defined by protobuf that are useless
21022075
if issubclass(self.py_object, ProtoMessage):
21032076
if (member_info.short_name.endswith('_FIELD_NUMBER') or
@@ -2306,16 +2279,16 @@ def get_metadata_html(self):
23062279

23072280
def _add_member(self, member_info: MemberInfo) -> None:
23082281
"""Adds members of the modules to the respective lists."""
2309-
obj_type = get_obj_type(member_info.py_object)
2310-
if obj_type is ObjType.MODULE:
2282+
obj_type = obj_type_lib.ObjType.get(member_info.py_object)
2283+
if obj_type is obj_type_lib.ObjType.MODULE:
23112284
self._add_module(member_info)
2312-
elif obj_type is ObjType.CLASS:
2285+
elif obj_type is obj_type_lib.ObjType.CLASS:
23132286
self._add_class(member_info)
2314-
elif obj_type is ObjType.CALLABLE:
2287+
elif obj_type is obj_type_lib.ObjType.CALLABLE:
23152288
self._add_function(member_info)
2316-
elif obj_type is ObjType.TYPE_ALIAS:
2289+
elif obj_type is obj_type_lib.ObjType.TYPE_ALIAS:
23172290
self._add_type_alias(member_info)
2318-
elif obj_type is ObjType.OTHER:
2291+
elif obj_type is obj_type_lib.ObjType.OTHER:
23192292
self._add_other_member(member_info)
23202293

23212294
def collect_docs(self, parser_config):
@@ -2396,17 +2369,17 @@ def docs_for_object(
23962369
if main_name in duplicate_names:
23972370
duplicate_names.remove(main_name)
23982371

2399-
obj_type = get_obj_type(py_object)
2400-
if obj_type is ObjType.CLASS:
2372+
obj_type = obj_type_lib.ObjType.get(py_object)
2373+
if obj_type is obj_type_lib.ObjType.CLASS:
24012374
page_info = ClassPageInfo(
24022375
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
2403-
elif obj_type is ObjType.CALLABLE:
2376+
elif obj_type is obj_type_lib.ObjType.CALLABLE:
24042377
page_info = FunctionPageInfo(
24052378
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
2406-
elif obj_type is ObjType.MODULE:
2379+
elif obj_type is obj_type_lib.ObjType.MODULE:
24072380
page_info = ModulePageInfo(
24082381
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
2409-
elif obj_type is ObjType.TYPE_ALIAS:
2382+
elif obj_type is obj_type_lib.ObjType.TYPE_ALIAS:
24102383
page_info = TypeAliasPageInfo(
24112384
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
24122385
else:
@@ -2536,11 +2509,11 @@ def generate_global_index(library_name, index, reference_resolver):
25362509
"""
25372510
symbol_links = []
25382511
for full_name, py_object in index.items():
2539-
obj_type = get_obj_type(py_object)
2540-
if obj_type in (ObjType.OTHER, ObjType.PROPERTY):
2512+
obj_type = obj_type_lib.ObjType.get(py_object)
2513+
if obj_type in (obj_type_lib.ObjType.OTHER, obj_type_lib.ObjType.PROPERTY):
25412514
continue
25422515
# In Python 3, unbound methods are functions, so eliminate those.
2543-
if obj_type is ObjType.CALLABLE:
2516+
if obj_type is obj_type_lib.ObjType.CALLABLE:
25442517
if is_class_attr(full_name, index):
25452518
continue
25462519
with reference_resolver.temp_prefix('..'):

0 commit comments

Comments
 (0)