Skip to content

Wrong behavior of help function on module #77849

Open

Description

@OlegOleinik
BPO 33668
Nosy @1st1, @ilevkivskyi, @timofurrer, @corona10
PRs
  • bpo-33668: Fix inspect.getmembers to traverse mro when object is class #8284
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2018-05-28.14:22:23.146>
    labels = ['3.7', 'type-bug', 'library']
    title = 'Wrong behavior of help function on module'
    updated_at = <Date 2019-08-01.05:15:50.897>
    user = 'https://bugs.python.org/OlegOleinik'

    bugs.python.org fields:

    activity = <Date 2019-08-01.05:15:50.897>
    actor = 'corona10'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2018-05-28.14:22:23.146>
    creator = 'Oleg.Oleinik'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33668
    keywords = ['patch']
    message_count = 6.0
    messages = ['317852', '318335', '318336', '321653', '321654', '348838']
    nosy_count = 5.0
    nosy_names = ['yselivanov', 'levkivskyi', 'tuxtimo', 'corona10', 'Oleg.Oleinik']
    pr_nums = ['8284']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33668'
    versions = ['Python 3.7']

    Activity

    1. OlegOleinik commented on May 28, 2018

      OlegOleinikmannequin
      MannequinAuthor

      I write file test.py:
      def __getattr__(key):
      return None
      help(name)

      when I try to run it I've got error:
      Traceback (most recent call last):
        File "test.py", line 5, in <module>
          help(__name__)
        File "C:\Program Files\Python37\lib\_sitebuiltins.py", line 103, in __call__
          return pydoc.help(*args, **kwds)
        File "C:\Program Files\Python37\lib\pydoc.py", line 1894, in __call__
          self.help(request)
        File "C:\Program Files\Python37\lib\pydoc.py", line 1944, in help
          elif request: doc(request, 'Help on %s:', output=self._output)
        File "C:\Program Files\Python37\lib\pydoc.py", line 1674, in doc
          pager(render_doc(thing, title, forceload))
        File "C:\Program Files\Python37\lib\pydoc.py", line 1667, in render_doc
          return title % desc + '\n\n' + renderer.document(object, name)
        File "C:\Program Files\Python37\lib\pydoc.py", line 385, in document
          if inspect.ismodule(object): return self.docmodule(*args)
        File "C:\Program Files\Python37\lib\pydoc.py", line 1136, in docmodule
          for key, value in inspect.getmembers(object, inspect.isclass):
        File "C:\Program Files\Python37\lib\inspect.py", line 330, in getmembers
          for base in object.__bases__:
      TypeError: 'NoneType' object is not iterable

      If I change definition of __getattr__ function to:
      def __getattr__(key):
      raise AttributeError()
      then help function runs without errors. But I need to return None value from __getattr__ function.
      When I wrote this function in class definition there is also no errors during runtime

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on May 28, 2018
    3. ilevkivskyi commented on May 31, 2018

      @ilevkivskyi
      Member

      Hm, replacing the return with a random string, this leads to another crash:

      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/Users/ilevkivskyi/src/cpython/Lib/_sitebuiltins.py", line 103, in __call__
          return pydoc.help(*args, **kwds)
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1895, in __call__
          self.help(request)
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1954, in help
          else: doc(request, 'Help on %s:', output=self._output)
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1674, in doc
          pager(render_doc(thing, title, forceload))
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1667, in render_doc
          return title % desc + '\n\n' + renderer.document(object, name)
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 385, in document
          if inspect.ismodule(object): return self.docmodule(*args)
        File "/Users/ilevkivskyi/src/cpython/Lib/pydoc.py", line 1157, in docmodule
          for importer, modname, ispkg in pkgutil.iter_modules(object.__path__):
        File "/Users/ilevkivskyi/src/cpython/Lib/pkgutil.py", line 123, in iter_modules
          raise ValueError("path must be None or list of paths to look for "
      ValueError: path must be None or list of paths to look for modules in

      The reason is that __getattr__ is also triggered when a special attribute is looked up. I am not sure what to do with this. This is a bit inconsistent with how classes behave, where e.g. __len__ is never searched on an instance. But modules are special in many other ways, so maybe we can just fix pydoc (and other tools like inspect) to expect some ill-typed values in special module attributes and fail gracefully?

    4. ilevkivskyi commented on May 31, 2018

      @ilevkivskyi
      Member

      Adding Yury as an inspect expert. I don't think this is something urgent, we can probably postpone this to 3.7.1.

    5. corona10 commented on Jul 14, 2018

      @corona10
      Member

      This script works well on Python2 but not on Python3.
      I've submitted a PR. Please take a look.

    6. corona10 commented on Jul 14, 2018

      @corona10
      Member

      I've checked both cases are now working on my patch.

    7. corona10 commented on Aug 1, 2019

      @corona10
      Member

      I close PR 8284.

      If there is a chance to work with this issue.
      Please let me know

      Thanks!

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions