Skip to content

Commit

Permalink
MAINT: Do not emit empty Methods heading in np.info (#17498)
Browse files Browse the repository at this point in the history
Fixes the incompatible type comparison found in #17490.

This also corrects the logic to not print the heading when only private/magic methods are present.
  • Loading branch information
stevejoachim committed Oct 10, 2020
1 parent dbb8e99 commit aeb2374
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions numpy/lib/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,22 @@ def test_strided(self):
def test_assert_raises_regex_context_manager():
with assert_raises_regex(ValueError, 'no deprecation warning'):
raise ValueError('no deprecation warning')


def test_info_method_heading():
# info(class) should only print "Methods:" heading if methods exist

class NoPublicMethods:
pass

class WithPublicMethods:
def first_method():
pass

def _has_method_heading(cls):
out = StringIO()
utils.info(cls, output=out)
return 'Methods:' in out.getvalue()

assert _has_method_heading(WithPublicMethods)
assert not _has_method_heading(NoPublicMethods)
8 changes: 4 additions & 4 deletions numpy/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'):
print(inspect.getdoc(object), file=output)

methods = pydoc.allmethods(object)
if methods != []:

public_methods = [meth for meth in methods if meth[0] != '_']
if public_methods:
print("\n\nMethods:\n", file=output)
for meth in methods:
if meth[0] == '_':
continue
for meth in public_methods:
thisobj = getattr(object, meth, None)
if thisobj is not None:
methstr, other = pydoc.splitdoc(
Expand Down

0 comments on commit aeb2374

Please sign in to comment.