forked from google/python-fire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_descriptions.py
More file actions
71 lines (59 loc) · 2.57 KB
/
custom_descriptions.py
File metadata and controls
71 lines (59 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Custom descriptions and summaries for the builtin types.
The docstrings for objects of primitive types reflect the type of the object,
rather than the object itself. For example, the docstring for any dict is this:
> print({'key': 'value'}.__doc__)
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
As you can see, this docstring is more pertinant to the function `dict` and
would be suitable as the result of `dict.__doc__`, but is wholely unsuitable
as a description for the dict `{'key': 'value'}`.
This modules aims to resolve that problem, providing custom summaries and
descriptions for primitive typed values.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import six
def NeedsCustomDescription(component):
"""Whether the component should use a custom description and summary.
Components of primitive type, such as ints, floats, dicts, lists, and others
have messy builtin docstrings. These are inappropriate for display as
descriptions and summaries in a CLI. This function determines whether the
provided component has one of these docstrings.
Note that an object such as `int` has the same docstring as an int like `3`.
The docstring is OK for `int`, but is inappropriate as a docstring for `3`.
Args:
component: The component of interest.
Returns:
Whether the component should use a custom description and summary.
"""
type_ = type(component)
if (type_ in six.string_types
or type_ in six.integer_types
or type_ is six.text_type
or type_ is six.binary_type
or type_ in (float, complex, bool)
or type_ in (dict, tuple, list, set, frozenset)
):
return True
return False