Skip to content

Commit 9ed343e

Browse files
MarkDaoustcopybara-github
authored andcommitted
Some fixes for python 3.9
* Strip the extra parens from bool default-values. * Handle generic `descriptors` not just properties (the namedtuple implementation changed). I test-ran this on `tensorflow` and the only diff was some descriptors being corrected. They were handled like Class Attributes, but now they're handled like regular attributes. PiperOrigin-RevId: 414770107
1 parent 70a4173 commit 9ed343e

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

tools/tensorflow_docs/api_generator/doc_generator_visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def maybe_singleton(py_object: Any) -> bool:
4545
is_immutable_type = isinstance(py_object, immutable_types)
4646

4747
# Check if the object is the empty tuple.
48-
return is_immutable_type or py_object is () # pylint: disable=literal-comparison
48+
return is_immutable_type or py_object == ()
4949

5050

5151
class ApiTreeNode(object):

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ObjType(enum.Enum):
4848
MODULE = 'module'
4949
CLASS = 'class'
5050
CALLABLE = 'callable'
51+
# properties or any `descriptor`
5152
PROPERTY = 'property'
5253
OTHER = 'other'
5354

@@ -62,7 +63,9 @@ def get_obj_type(py_obj: Any) -> ObjType:
6263
return ObjType.CLASS
6364
elif callable(py_obj):
6465
return ObjType.CALLABLE
65-
elif isinstance(py_obj, property):
66+
elif hasattr(py_obj, '__get__'):
67+
# This handles any descriptor not only properties.
68+
# https://docs.python.org/3/howto/descriptor.html
6669
return ObjType.PROPERTY
6770
else:
6871
return ObjType.OTHER
@@ -956,6 +959,8 @@ def _get_other_member_doc(
956959
# breaks on the site.
957960
info = pprint.pformat(obj).replace('`', r'\`')
958961
info = f'`{info}`'
962+
elif get_obj_type(obj) is ObjType.PROPERTY:
963+
info = None
959964
else:
960965
class_full_name = parser_config.reverse_index.get(id(type(obj)), None)
961966
if class_full_name is None:
@@ -967,12 +972,10 @@ def _get_other_member_doc(
967972
class_full_name = f'{module}.{class_name}'
968973
info = f'Instance of `{class_full_name}`'
969974

970-
if description is None:
971-
result = info
972-
else:
973-
result = f'{info}\n\n{description}'
975+
parts = [info, description]
976+
parts = [item for item in parts if item is not None]
974977

975-
return result
978+
return '\n\n'.join(parts)
976979

977980

978981
def _parse_md_docstring(
@@ -1086,13 +1089,13 @@ def visit_AnnAssign(self, node) -> None: # pylint: disable=invalid-name
10861089
class ASTDefaultValueExtractor(ast.NodeVisitor):
10871090
"""Extracts the default values by parsing the AST of a function."""
10881091

1089-
_PAREN_NUMBER_RE = re.compile(r'^\(([0-9.e-]+)\)')
1092+
_PAREN_NUMBER_RE = re.compile(r'^\((True|False|[0-9.e-]+)\)')
10901093

10911094
def __init__(self):
10921095
self.ast_args_defaults = {}
10931096
self.ast_kw_only_defaults = {}
10941097

1095-
def _preprocess(self, val: str) -> str:
1098+
def _preprocess(self, val) -> str:
10961099
text_default_val = astor.to_source(val).strip().replace(
10971100
'\t', '\\t').replace('\n', '\\n').replace('"""', "'")
10981101
text_default_val = self._PAREN_NUMBER_RE.sub('\\1', text_default_val)
@@ -2208,7 +2211,7 @@ def _augment_attributes(self,
22082211
# namedtuple fields first, in order.
22092212
for name, desc in self._namedtuplefields.items():
22102213
# If a namedtuple field has been filtered out, it's description will
2211-
# not have been set in the `member_info` loop, so skip fields with `None`
2214+
# not have been set in loop in `collect_docs`, so skip fields with `None`
22122215
# as the description.
22132216
if desc is not None:
22142217
attrs[name] = desc

0 commit comments

Comments
 (0)