|
38 | 38 | from tensorflow_docs.api_generator import config
|
39 | 39 | from tensorflow_docs.api_generator import doc_controls
|
40 | 40 | from tensorflow_docs.api_generator import doc_generator_visitor
|
| 41 | +from tensorflow_docs.api_generator import obj_type as obj_type_lib |
41 | 42 | from tensorflow_docs.api_generator import public_api
|
42 | 43 |
|
43 | 44 | from google.protobuf.message import Message as ProtoMessage
|
44 | 45 |
|
45 | 46 |
|
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 |
| - |
75 | 47 | @dataclasses.dataclass
|
76 | 48 | class _FileLocation(object):
|
77 | 49 | """This class indicates that the object is defined in a regular file.
|
@@ -152,12 +124,12 @@ def _get_raw_docstring(py_object):
|
152 | 124 | The docstring, or the empty string if no docstring was found.
|
153 | 125 | """
|
154 | 126 |
|
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: |
156 | 128 | if inspect.getdoc(py_object) != inspect.getdoc(py_object.__origin__):
|
157 | 129 | result = inspect.getdoc(py_object)
|
158 | 130 | else:
|
159 | 131 | 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: |
161 | 133 | result = inspect.getdoc(py_object) or ''
|
162 | 134 | else:
|
163 | 135 | result = ''
|
@@ -329,10 +301,11 @@ def from_visitor(cls, visitor, **kwargs):
|
329 | 301 | """
|
330 | 302 | is_fragment = {}
|
331 | 303 | 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): |
334 | 306 | 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): |
336 | 309 | if is_class_attr(full_name, visitor.index):
|
337 | 310 | is_fragment[full_name] = True
|
338 | 311 | else:
|
@@ -924,7 +897,7 @@ def _get_other_member_doc(
|
924 | 897 | # breaks on the site.
|
925 | 898 | info = pprint.pformat(obj).replace('`', r'\`')
|
926 | 899 | 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: |
928 | 901 | info = None
|
929 | 902 | else:
|
930 | 903 | class_full_name = parser_config.reverse_index.get(id(type(obj)), None)
|
@@ -967,7 +940,7 @@ def _parse_md_docstring(
|
967 | 940 | A _DocstringInfo object, all fields will be empty if no docstring was found.
|
968 | 941 | """
|
969 | 942 |
|
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: |
971 | 944 | raw_docstring = _get_other_member_doc(
|
972 | 945 | obj=py_object, parser_config=parser_config, extra_docs=extra_docs)
|
973 | 946 | else:
|
@@ -2087,17 +2060,17 @@ def _add_member(
|
2087 | 2060 | parser_config: config.ParserConfig,
|
2088 | 2061 | ) -> None:
|
2089 | 2062 | """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) |
2091 | 2064 |
|
2092 |
| - if obj_type is ObjType.PROPERTY: |
| 2065 | + if obj_type is obj_type_lib.ObjType.PROPERTY: |
2093 | 2066 | self._add_property(member_info)
|
2094 |
| - elif obj_type is ObjType.CLASS: |
| 2067 | + elif obj_type is obj_type_lib.ObjType.CLASS: |
2095 | 2068 | if defining_class is None:
|
2096 | 2069 | return
|
2097 | 2070 | self._add_class(member_info)
|
2098 |
| - elif obj_type is ObjType.CALLABLE: |
| 2071 | + elif obj_type is obj_type_lib.ObjType.CALLABLE: |
2099 | 2072 | 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: |
2101 | 2074 | # Exclude members defined by protobuf that are useless
|
2102 | 2075 | if issubclass(self.py_object, ProtoMessage):
|
2103 | 2076 | if (member_info.short_name.endswith('_FIELD_NUMBER') or
|
@@ -2306,16 +2279,16 @@ def get_metadata_html(self):
|
2306 | 2279 |
|
2307 | 2280 | def _add_member(self, member_info: MemberInfo) -> None:
|
2308 | 2281 | """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: |
2311 | 2284 | self._add_module(member_info)
|
2312 |
| - elif obj_type is ObjType.CLASS: |
| 2285 | + elif obj_type is obj_type_lib.ObjType.CLASS: |
2313 | 2286 | self._add_class(member_info)
|
2314 |
| - elif obj_type is ObjType.CALLABLE: |
| 2287 | + elif obj_type is obj_type_lib.ObjType.CALLABLE: |
2315 | 2288 | self._add_function(member_info)
|
2316 |
| - elif obj_type is ObjType.TYPE_ALIAS: |
| 2289 | + elif obj_type is obj_type_lib.ObjType.TYPE_ALIAS: |
2317 | 2290 | self._add_type_alias(member_info)
|
2318 |
| - elif obj_type is ObjType.OTHER: |
| 2291 | + elif obj_type is obj_type_lib.ObjType.OTHER: |
2319 | 2292 | self._add_other_member(member_info)
|
2320 | 2293 |
|
2321 | 2294 | def collect_docs(self, parser_config):
|
@@ -2396,17 +2369,17 @@ def docs_for_object(
|
2396 | 2369 | if main_name in duplicate_names:
|
2397 | 2370 | duplicate_names.remove(main_name)
|
2398 | 2371 |
|
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: |
2401 | 2374 | page_info = ClassPageInfo(
|
2402 | 2375 | 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: |
2404 | 2377 | page_info = FunctionPageInfo(
|
2405 | 2378 | 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: |
2407 | 2380 | page_info = ModulePageInfo(
|
2408 | 2381 | 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: |
2410 | 2383 | page_info = TypeAliasPageInfo(
|
2411 | 2384 | full_name=main_name, py_object=py_object, extra_docs=extra_docs)
|
2412 | 2385 | else:
|
@@ -2536,11 +2509,11 @@ def generate_global_index(library_name, index, reference_resolver):
|
2536 | 2509 | """
|
2537 | 2510 | symbol_links = []
|
2538 | 2511 | 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): |
2541 | 2514 | continue
|
2542 | 2515 | # 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: |
2544 | 2517 | if is_class_attr(full_name, index):
|
2545 | 2518 | continue
|
2546 | 2519 | with reference_resolver.temp_prefix('..'):
|
|
0 commit comments