Skip to content

Commit c830177

Browse files
dbiebercopybara-github
authored andcommitted
Move GetClassAttrsDict to inspectutils.
PiperOrigin-RevId: 297862432 Change-Id: I8b37f6715f70085a9b967b183e27aaa031480ad1
1 parent c8ea506 commit c830177

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

fire/completion.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,6 @@ def _FishScript(name, commands, default_options=None):
281281
)
282282

283283

284-
def GetClassAttrsDict(component):
285-
"""Gets the attributes of the component class, as a dict with name keys."""
286-
if not inspect.isclass(component):
287-
return None
288-
class_attrs_list = inspect.classify_class_attrs(component)
289-
return {
290-
class_attr.name: class_attr
291-
for class_attr in class_attrs_list
292-
}
293-
294-
295284
def MemberVisible(component, name, member, class_attrs=None, verbose=False):
296285
"""Returns whether a member should be included in auto-completion or help.
297286
@@ -328,7 +317,7 @@ def MemberVisible(component, name, member, class_attrs=None, verbose=False):
328317
if inspect.isclass(component):
329318
# If class_attrs has not been provided, compute it.
330319
if class_attrs is None:
331-
class_attrs = GetClassAttrsDict(class_attrs)
320+
class_attrs = inspectutils.GetClassAttrsDict(class_attrs)
332321
class_attr = class_attrs.get(name)
333322
if class_attr and class_attr.kind in ('method', 'property'):
334323
# methods and properties should be accessed on instantiated objects,
@@ -355,9 +344,9 @@ def VisibleMembers(component, class_attrs=None, verbose=False):
355344
Args:
356345
component: The component whose members to list.
357346
class_attrs: (optional) If component is a class, you may provide this as:
358-
GetClassAttrsDict(component). If not provided, it will be computed.
359-
If provided, this determines how class members will be treated for
360-
visibility. In particular, methods are generally hidden for
347+
inspectutils.GetClassAttrsDict(component). If not provided, it will be
348+
computed. If provided, this determines how class members will be treated
349+
for visibility. In particular, methods are generally hidden for
361350
non-instantiated classes, but if you wish them to be shown (e.g. for
362351
completion scripts) then pass in a different class_attr for them.
363352
verbose: Whether to include private members.
@@ -371,7 +360,7 @@ def VisibleMembers(component, class_attrs=None, verbose=False):
371360

372361
# If class_attrs has not been provided, compute it.
373362
if class_attrs is None:
374-
class_attrs = GetClassAttrsDict(component)
363+
class_attrs = inspectutils.GetClassAttrsDict(component)
375364
return [
376365
(member_name, member) for member_name, member in members
377366
if MemberVisible(component, member_name, member, class_attrs=class_attrs,

fire/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def _DictAsString(result, verbose=False):
309309
# We need to do 2 iterations over the items in the result dict
310310
# 1) Getting visible items and the longest key for output formatting
311311
# 2) Actually construct the output lines
312-
class_attrs = completion.GetClassAttrsDict(result)
312+
class_attrs = inspectutils.GetClassAttrsDict(result)
313313
result_visible = {
314314
key: value for key, value in result.items()
315315
if completion.MemberVisible(result, key, value,

fire/inspectutils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,14 @@ def IsNamedTuple(component):
268268

269269
has_fields = bool(getattr(component, '_fields', None))
270270
return has_fields
271+
272+
273+
def GetClassAttrsDict(component):
274+
"""Gets the attributes of the component class, as a dict with name keys."""
275+
if not inspect.isclass(component):
276+
return None
277+
class_attrs_list = inspect.classify_class_attrs(component)
278+
return {
279+
class_attr.name: class_attr
280+
for class_attr in class_attrs_list
281+
}

fire/value_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import inspect
2222

23-
from fire import completion
23+
from fire import inspectutils
2424
import six
2525

2626

@@ -78,7 +78,7 @@ def HasCustomStr(component):
7878
Whether `component` has a custom __str__ method.
7979
"""
8080
if hasattr(component, '__str__'):
81-
class_attrs = completion.GetClassAttrsDict(type(component)) or {}
81+
class_attrs = inspectutils.GetClassAttrsDict(type(component)) or {}
8282
str_attr = class_attrs.get('__str__')
8383
if str_attr and str_attr.defining_class is not object:
8484
return True

0 commit comments

Comments
 (0)