Skip to content

Use original line numbering when using pyobject include #11653

@saurabheights

Description

@saurabheights

Describe the bug

This issue is similar to #1583 but when using pyobject.

I did a quick test with latest sphinx version, used a simply python file with main method at line 4-5. Instead of showing line number 4-5, sphinx shows line number 1,2.

I can add :lineno-start: 4 to fix line number but that defeats the advantage pyobject brings, i.e. as long as function name and behaviour doesnt change, including whole method is the right thing to do in the docs and any changes in file before the method should automatically fix the line numbers.

print("Test")


def main():
    print("main at line 5")


if __name__ == "__main__":
    main()

The result is
Screenshot from 2023-08-28 22-30-32

How to Reproduce

Index.rst

.. asas documentation master file, created by
   sphinx-quickstart on Mon Aug 28 22:25:07 2023.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to asas's documentation!
================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

.. literalinclude:: ../line_width.py
    :language: python
    :pyobject: main
    :linenos:
    :lineno-start: 3


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

line_width.py

print("Test")


def main():
    print("main at line 5")


if __name__ == "__main__":
    main()

Environment Information

Platform:              linux; (Linux-6.2.0-26-generic-x86_64-with-glibc2.35)
Python version:        3.10.11 (main, May 16 2023, 00:28:57) [GCC 11.2.0])
Python implementation: CPython
Sphinx version:        7.1.2
Docutils version:      0.20.1
Jinja2 version:        3.1.2
Pygments version:      2.15.1

Sphinx extensions

No extension used. Autogenerate conf.py file, no change done there.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions