Skip to content

Commit

Permalink
Merge pull request #173 from lesteve/fix-identify-names
Browse files Browse the repository at this point in the history
Fix backreferences.identify_names when module is used without attribute.
  • Loading branch information
Titan-C committed Nov 27, 2016
2 parents ec05b22 + e51429b commit 7969af6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sphinx_gallery/backreferences.py
Expand Up @@ -103,7 +103,13 @@ def identify_names(code):
for name, full_name in finder.get_mapping():
# name is as written in file (e.g. np.asarray)
# full_name includes resolved import path (e.g. numpy.asarray)
module, attribute = full_name.rsplit('.', 1)
splitted = full_name.rsplit('.', 1)
if len(splitted) == 1:
# module without attribute. This is not useful for
# backreferences
continue

module, attribute = splitted
# get shortened module name
module_short = get_short_module_name(module, attribute)
cobj = {'name': attribute, 'module': module,
Expand Down
30 changes: 30 additions & 0 deletions sphinx_gallery/tests/test_backreferences.py
Expand Up @@ -60,3 +60,33 @@ def test_backref_thumbnail_div():
"""

assert_equal(html_div, reference)


def test_identify_names():
code_str = """
import os
os
os.path.join
import sphinx_gallery.back_references as br
br.identify_names
from sphinx_gallery.back_references import identify_names
identify_names
"""
res = sg.identify_names(code_str)
expected = {
'os.path.join':
{'name': 'join', 'module': 'os.path', 'module_short': 'os.path'},
'br.identify_names':
{'name': 'identify_names',
'module': 'sphinx_gallery.back_references',
'module_short': 'sphinx_gallery.back_references'},
'identify_names':
{'name': 'identify_names',
'module': 'sphinx_gallery.back_references',
'module_short': 'sphinx_gallery.back_references'}
}

assert_equal(expected, res)

0 comments on commit 7969af6

Please sign in to comment.