Sphinx: inheritance_diagram AttributeError: 'NoneType' object has no attribute 'groups'

Created on 23 Aug 2017  Â·  8Comments  Â·  Source: sphinx-doc/sphinx

Subject: Exception occurs on make latexpdf in try_import of sphinx/ext/inheritance_diagram.py

Problem

Most Propably a user Error

Exception occurred:
  File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 83, in try_import
    modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'
The full traceback has been saved in /tmp/sphinx-err-0kr1ytc1.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!
Makefile:27: die Regel für Ziel „latexpdf“ scheiterte
make: *** [latexpdf] Fehler 1

Procedure to reproduce the problem

make latexpdf

Error logs / results

> # Sphinx version: 1.6.3
> # Python version: 3.6.0 (CPython)
> # Docutils version: 0.14 
> # Jinja2 version: 2.9.6
> # Last messages:
> #   updating environment:
> #   
> #   [extensions changed] 8 added, 0 changed, 0 removed
> #   
> #   reading sources... [ 12%] aus
> #   
> #   base-path: /home/user/some
> #   
> #   reading sources... [ 25%] fileformat
> #   
> # Loaded extensions:
> #   alabaster (0.7.10) from /home/johannes/anaconda3/lib/python3.6/site-packages/alabaster/__init__.py
> #   sphinx.ext.autodoc (1.6.3) from /home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/autodoc.py
> #   sphinx.ext.graphviz (1.6.3) from /home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/graphviz.py
> #   sphinx.ext.inheritance_diagram (1.6.3) from /home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py
> Traceback (most recent call last):
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 80, in try_import
>     __import__(objname)
> ModuleNotFoundError: No module named '.'
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/cmdline.py", line 306, in main
>     app.build(opts.force_all, filenames)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/application.py", line 339, in build
>     self.builder.build_update()
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 326, in build_update
>     self.build(['__all__'], to_build)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 344, in build
>     updated_docnames = set(self.env.update(self.config, self.srcdir, self.doctreedir))
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 583, in update
>     self._read_serial(docnames, self.app)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 602, in _read_serial
>     self.read_doc(docname, app)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 723, in read_doc
>     pub.publish()
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/core.py", line 217, in publish
>     self.settings)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/io.py", line 74, in read
>     self.parse()
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/readers/__init__.py", line 78, in parse
>     self.parser.parse(self.input, document)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/__init__.py", line 191, in parse
>     self.statemachine.run(inputlines, document, inliner=self.inliner)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 171, in run
>     input_source=document['source'])
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 239, in run
>     context, state, transitions)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 460, in check_line
>     return method(match, context, next_state)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2753, in underline
>     self.section(title, source, style, lineno - 1, messages)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 327, in section
>     self.new_subsection(title, lineno, messages)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 395, in new_subsection
>     node=section_node, match_titles=True)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 282, in nested_parse
>     node=node, match_titles=match_titles)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 196, in run
>     results = StateMachineWS.run(self, input_lines, input_offset)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 239, in run
>     context, state, transitions)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 460, in check_line
>     return method(match, context, next_state)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2328, in explicit_markup
>     self.explicit_list(blank_finish)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2358, in explicit_list
>     match_titles=self.state_machine.match_titles)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 319, in nested_list_parse
>     node=node, match_titles=match_titles)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 196, in run
>     results = StateMachineWS.run(self, input_lines, input_offset)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 239, in run
>     context, state, transitions)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/statemachine.py", line 460, in check_line
>     return method(match, context, next_state)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2631, in explicit_markup
>     nodelist, blank_finish = self.explicit_construct(match)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2338, in explicit_construct
>     return method(self, expmatch)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2081, in directive
>     directive_class, match, type_name, option_presets)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/docutils/parsers/rst/states.py", line 2130, in run_directive
>     result = directive_instance.run()
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 342, in run
>     private_bases='private-bases' in self.options)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 144, in __init__
>     classes = self._import_classes(class_names, currmodule)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 156, in _import_classes
>     classes.extend(import_classes(name, currmodule))
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 100, in import_classes
>     target = try_import(currmodule + '.' + name)
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 83, in try_import
>     modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
> AttributeError: 'NoneType' object has no attribute 'groups'

Expected results

No Exception even if object is None

Reproducible project / your project

not necessary

Environment info

  • OS:
  • Python version: Python 3.6.0 |Anaconda custom (64-bit)
  • Sphinx version: Sphinx-1.6.3-py2.py3-none-any.whl
  • sphinx.ext.inheritance_diagram
bug extensions

Most helpful comment

I noticed invalid modname causes an AttributeError.

.. inheritance-diagram:: :)
...
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 357, in run
    top_classes=node['top-classes'])
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 140, in __init__
    classes = self._import_classes(class_names, currmodule)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 152, in _import_classes
    classes.extend(import_classes(name, currmodule))
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 100, in import_classes
    target = try_import(name)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 79, in try_import
    modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'

Exception occurred:
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 79, in try_import
    modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'

@dpizetta could you make a PR please?

All 8 comments

Hi guys, I have the same problem here and I propose a solution here.

In the function try_import the parameter objname is unicode, so this solution I proposed is treating the consequence of passing a non-unicode parameter, in this case, None - NoneType.

This error breaks the making process, so I am using this fix until get into the real cause.

What do you think about this fix? Tks

the error happens, for example, when there are modules, that can not be found, which should not break the making process. I would agree to your solution, and additionally an ERROR Message, that at least shouts the name of the module that can not be found, would be nice to have.

Your suggestion, is a good one too, but particularly don't like to call two times the function, one for test and another for using it if passes. Both should be enough for now.

I looked into the code and found that the error could be generated by the [docutils parser (https://github.com/dpizetta/sphinx/blob/master/sphinx/ext/inheritance_diagram.py#L332). The parser includes strings ":parts:" and "1" as classes into the list of classes.

.. inheritance-diagram:: aa.bb.cc
   :parts: 1

This part is extracted from the module doc. Just one of my files get this problem, but in this file I have lots of imports (~20), so I couldn't find why this is generated.

More details: The function try_import() is called from import_classes() that receives the list of classes from InheritanceGraph class. This one is created in InheritanceDiagram, that is reimplemented from Directive of docutils which parses files.

Finally: I think that is not necessary throw an error message this moment if the problem that occurs in your code is the same, because saying just that some file has this problem (with "parts" and "1") may not help, returning None is enough to not break the making process. If anyone is getting the problem that it not finds some "real" module I agree to include the error message.

Could you test if this problem is the same (getting names)? Any other suggestions?

Sorry, in your text there is the name of the not found module, that already produces a error message:

> Traceback (most recent call last):
>   File "/home/johannes/anaconda3/lib/python3.6/site-packages/sphinx/ext/inheritance_diagram.py", line 80, in try_import
>     __import__(objname)
> ModuleNotFoundError: No module named '.'
> 
> During handling of the above exception, another exception occurred:`

, but it tries to handle that exception and then goes to another one:

>     modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
> AttributeError: 'NoneType' object has no attribute 'groups'

So, if it was the case, just return None is suficient for the problem.

yep, for me it really is, I'm happy :-)

Is there anything I can do for this?
I also would like to fix this, but I don't know how to reproduce.

Hi, I can submit my PR. The problem is that I don't figure out what is the real cause of the problem. So I can't create a test for it. I'm using this fix, and I didn't find any problem with it. For me, this is just a logic problem (return of match is None), and it still keeps warning about the not found module. May you can accept the PR. Any other ideas?

modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'

I noticed invalid modname causes an AttributeError.

.. inheritance-diagram:: :)
...
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 357, in run
    top_classes=node['top-classes'])
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 140, in __init__
    classes = self._import_classes(class_names, currmodule)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 152, in _import_classes
    classes.extend(import_classes(name, currmodule))
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 100, in import_classes
    target = try_import(name)
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 79, in try_import
    modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'

Exception occurred:
  File "/Users/tkomiya/work/sphinx/sphinx/ext/inheritance_diagram.py", line 79, in try_import
    modname, attrname = module_sig_re.match(objname).groups()  # type: ignore
AttributeError: 'NoneType' object has no attribute 'groups'

@dpizetta could you make a PR please?

Was this page helpful?
0 / 5 - 0 ratings