Skip to content
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

PyCode "Unparse": Added support for slice objects in subscriptions #11981

Merged
merged 12 commits into from Feb 14, 2024

Conversation

MatrixEditor
Copy link
Contributor

Subject: Fix documentation issue when slice objects are used in subscriptions

Feature or Bugfix

  • Feature

Purpose

  • Added function to handle parsed slice objects

Detail

  • For instance, consider the following class definition:
    class Foo: ...
    
    class Bar(BaseType):
        #: doc comment
        field1: int
        field2: Foo[1:2:3]
        """doc comment"""
    Internally, sphinx will raise an exception while parsing this file:
    ...
    File "venv-3.12/lib/python3.12/site-packages/sphinx/pycode/ast.py", line 170, in visit_Subscript
      return f"{self.visit(node.value)}[{self.visit(node.slice)}]"
                                         ^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/lib/python3.12/ast.py", line 407, in visit
      return visitor(node)
             ^^^^^^^^^^^^^
    File "venv-3.12/lib/python3.12/site-packages/sphinx/pycode/ast.py", line 188, in generic_visit
      raise NotImplementedError('Unable to parse %s object' % type(node).__name__)
    NotImplementedError: Unable to parse Slice object
    
  • The implemented function does not alter existing code

Relates

@MatrixEditor MatrixEditor changed the title PyCode "Unparser": Added support for slice objects in subscriptions PyCode "Unparse": Added support for slice objects in subscriptions Feb 14, 2024
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests and a CHANGES please?

sphinx/pycode/ast.py Outdated Show resolved Hide resolved
tests/test_pycode/test_pycode_ast.py Outdated Show resolved Hide resolved
("x[:, np.newaxis, :, :]",
"x[:, np.newaxis, :, :]"), # Index, Subscript, numpy extended syntax
("y[:, 1:3][np.array([0, 2, 4]), :]",
"y[:, 1:3][np.array([0, 2, 4]), :]"), # Index, 2x Subscript, numpy extended syntax
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just by looking at all those identity checks, I think we should have used a sentinel value, say IDENTICAL, to indicate that the output is the same as the input. This is an easy follow-up PR though.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can you check that #10817 is indeed solved?

CHANGES.rst Outdated Show resolved Hide resolved
@MatrixEditor
Copy link
Contributor Author

Yes, but that will take me a few moments.

@picnixz
Copy link
Member

picnixz commented Feb 14, 2024

Don't worry, you're fast enough (it also allows me to work on something else in the meantime)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@MatrixEditor
Copy link
Contributor Author

Yes, #10817 is indeed fixed:

temp/__init__.py

Ac = "float64[:, :]"


def myfunc(arg: Ac):
    """Some docs"""
    ...

index.rst

Welcome to temp's documentation!
================================

.. autofunction:: temp.myfunc


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

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

Results in:
image

@picnixz
Copy link
Member

picnixz commented Feb 14, 2024

Is it possible to include this one as a test as well?

@picnixz picnixz merged commit 1820531 into sphinx-doc:master Feb 14, 2024
22 checks passed
@picnixz
Copy link
Member

picnixz commented Feb 14, 2024

Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants