Skip to content

TypeError: 'NoneType' object is not iterable #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lucas03 opened this issue Dec 11, 2018 · 4 comments
Closed

TypeError: 'NoneType' object is not iterable #66

lucas03 opened this issue Dec 11, 2018 · 4 comments
Labels

Comments

@lucas03
Copy link

lucas03 commented Dec 11, 2018

Hi, I encountered this issue when building docs and included sphinx-autodoc-typehints:

sphinx-build -M html docs docs/_build -q 2>&1

Exception occurred:
  File "/usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py", line 122, in format_annotation
    extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
TypeError: 'NoneType' object is not iterable
The full traceback has been saved in /tmp/sphinx-err-yvc80trd.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!
/srv/backend/booking # nano /tmp/sphinx-err-yvc80trd.log
# Sphinx version: 1.8.2
# Python version: 3.6.7 (CPython)
# Docutils version: 0.14
# Jinja2 version: 2.10
# Last messages:
#   reading sources... [  6%] kw/kw.backoff.models.additional_services
#
#   reading sources... [  7%] kw/kw.mambo
#
#   reading sources... [  8%] kw/kw.mambo.constants
#
#   reading sources... [  9%] kw/kw.mambo.handlers
#
#   reading sources... [  9%] kw/kw.mambo.models
#
# Loaded extensions:
#   sphinx.ext.mathjax (1.8.2) from /usr/local/lib/python3.6/site-packages/sphinx/ext/mathjax.py
#   alabaster (0.7.12) from /usr/local/lib/python3.6/site-packages/alabaster/__init__.py
#   sphinx.ext.autodoc (1.8.2) from /usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py
#   sphinx_autodoc_typehints (unknown version) from /usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py
#   sphinx.ext.viewcode (1.8.2) from /usr/local/lib/python3.6/site-packages/sphinx/ext/viewcode.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sphinx/cmd/build.py", line 304, in build_main
    app.build(args.force_all, filenames)
  File "/usr/local/lib/python3.6/site-packages/sphinx/application.py", line 341, in build
    self.builder.build_update()
  File "/usr/local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 347, in build_update
    len(to_build))
  File "/usr/local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 360, in build
    updated_docnames = set(self.read())
  File "/usr/local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 468, in read
    self._read_serial(docnames)
  File "/usr/local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 490, in _read_serial
    self.read_doc(docname)
  File "/usr/local/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 534, in read_doc
    doctree = read_doc(self.app, self.env, self.env.doc2path(docname))
  File "/usr/local/lib/python3.6/site-packages/sphinx/io.py", line 318, in read_doc
    pub.publish()
  File "/usr/local/lib/python3.6/site-packages/docutils/core.py", line 217, in publish
    self.settings)
  File "/usr/local/lib/python3.6/site-packages/docutils/readers/__init__.py", line 72, in read
    self.parse()
...
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/directive.py", line 131, in run
    documenter.generate(more_content=self.content)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 819, in generate
    self.document_members(all_members)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 740, in document_members
    check_module=members_check_module and not isattr)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 1247, in generate
    all_members=all_members)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 816, in generate
    self.add_content(more_content)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 1229, in add_content
    ModuleLevelDocumenter.add_content(self, more_content)
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 554, in add_content
    for i, line in enumerate(self.process_doc(docstrings)):
  File "/usr/local/lib/python3.6/site-packages/sphinx/ext/autodoc/__init__.py", line 514, in process_doc
    self.options, docstringlines)
  File "/usr/local/lib/python3.6/site-packages/sphinx/application.py", line 510, in emit
    return self.events.emit(event, self, *args)
  File "/usr/local/lib/python3.6/site-packages/sphinx/events.py", line 80, in emit
    results.append(callback(*args))
  File "/usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py", line 197, in process_docstring
    formatted_annotation = format_annotation(annotation)
  File "/usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py", line 106, in format_annotation
    extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
  File "/usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py", line 106, in <genexpr>
    extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
  File "/usr/local/lib/python3.6/site-packages/sphinx_autodoc_typehints.py", line 122, in format_annotation
    extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
TypeError: 'NoneType' object is not iterable
@bpeake-illuscio
Copy link

bpeake-illuscio commented Dec 13, 2018

Hello, I've run into the same error and think I've found a fix.

For me it's happening when I inherit a Generic Type as a concrete type. The line in question throwing the error for me was:

params = (getattr(annotation, '__parameters__', None) or
          getattr(annotation, '__args__', None))

'\\[{}]'.format(', '.join(format_annotation(param) for param in params))

Below is some replacement code, with comments explaining the problem and solution:

params = (getattr(annotation, '__parameters__', None) or
          getattr(annotation, '__args__', None))

try:
    # join type annotation together in a formatted string
    extra = '\\[{}]'.format(', '.join(format_annotation(param) for param in params))
except TypeError:
    # in some cases, a "Generic" has been inherited from another Generic
    # with a concrete type, and therefore cannot be assigned a Type
    # annotation. In this case, ``params`` will return None. Consider the
    # following:
    #
    # T = TypeVar('T')
    #
    # class A(Generic[T])
    #     def method() -> T
    #         pass
    #
    #
    # class B(A[str])
    #     pass
    #
    #
    # def some_method_for_a(value: A[int]) -> None:
    #
    #
    # def some_method_for_b(value: B) -> None:
    #     pass
    #
    # In the above, B inherits from a Generic type, but supplies a concrete
    # type (str). As such, it does not get an additional annotation when
    # used in a signature, since it will always be B[str]. This is unlike A,
    # which is truly Generic. See the difference between the signatures of
    # the two example functions above.
    #
    # B still passes as a Generic type, since it inherits from one, but has
    # no annotations, since its Type Variable is set upon class definition.
    # This causes, ``params`` above to return None, and throw a TypeError
    # when joined.
    #
    # Here we need to detect this case, and just set ``extra`` to a blank
    # string, so no additional annotation information is returned for B in
    # signature annotations.
    extra = ''

I am going to make a PR with the above code. It does not break any of the existing tests, and I've added a test for this use case.

@bpeake-illuscio
Copy link

Created a PR with the fix here: https://github.com/agronholm/sphinx-autodoc-typehints/pull/67/files

@agronholm
Copy link
Collaborator

Fixed via 645fa6b.

@lucas03
Copy link
Author

lucas03 commented Dec 28, 2018

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants