viewcode: viewcode-find-source event is emitted for each reference

Open
#8,758 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
documentation

Research direction

Reproduce the issue with conf.py, index.rst, and example.py using the sphinx.ext.autodoc and sphinx.ext.viewcode extensions. Then trace where the viewcode-find-source event is emitted and determine how to avoid repeated source lookups; done means the handler is not called once per referenced class while source viewing still works.

Written by the indexing model from the issue text.

Description

extensions:viewcode type:bug

Describe the bug
viewcode: viewcode-find-source event is emitted for each reference

To Reproduce

Add an event handler.

# conf.py
def viewcode_find_source(app, modname):
    print(modname)


def setup(app):
    app.connect('viewcode-find-source', viewcode_find_source)
# index.rst
.. automodule:: example
   :members:
   :undoc-members:
# example.py
class Foo1: pass
class Foo2: pass
class Foo3: pass
class Foo4: pass
class Foo5: pass
class Foo6: pass
class Foo7: pass
class Foo8: pass
class Foo9: pass
class Foo10: pass
class Foo11: pass
class Foo12: pass
class Foo13: pass
class Foo14: pass
class Foo15: pass
class Foo16: pass
class Foo17: pass
class Foo18: pass
class Foo19: pass
class Foo20: pass

As a result, the viewcode-find-source event is called 20 times!
It seems viewcode tries to read the source code for each reference. It's a waste.

Expected behavior
It should be called as possible as less.

Your project
No

Screenshots
No

Environment info

  • OS: Mac
  • Python version: 3.9.1
  • Sphinx version: HEAD of 3.x
  • Sphinx extensions: sphinx.ext.autodoc, sphinx.ext.viewcode
  • Extra tools: No

Additional context
No

Dominant language
Python
Stars
8k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sphinx-doc/sphinx

All issues in sphinx-doc/sphinx

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.