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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Features added
* #11892: Improved performance when resolving cross references in cpp domain.
Patch by Rouslan Korneychuk.

* #11981: pycode "UnparseVisitor": Added support for slice objects in subscriptions
MatrixEditor marked this conversation as resolved.
Show resolved Hide resolved

Bugs fixed
----------

Expand Down
6 changes: 6 additions & 0 deletions sphinx/pycode/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,9 @@ def visit_Tuple(self, node: ast.Tuple) -> str:

def generic_visit(self, node: ast.AST) -> NoReturn:
raise NotImplementedError('Unable to parse %s object' % type(node).__name__)

def visit_Slice(self, node: ast.Slice) -> str:
picnixz marked this conversation as resolved.
Show resolved Hide resolved
start = self.visit(node.lower) if node.lower else ""
stop = self.visit(node.upper) if node.upper else ""
step = self.visit(node.step) if node.step else ""
return f"{start}:{stop}:{step}"
2 changes: 2 additions & 0 deletions tests/test_pycode/test_pycode_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"lambda x=0, /, y=1, *args, z, **kwargs: ..."), # posonlyargs
("0x1234", "0x1234"), # Constant
("1_000_000", "1_000_000"), # Constant
("Tuple[:,:]", "Tuple[::, ::]"), # Index, Subscript, Slice
picnixz marked this conversation as resolved.
Show resolved Hide resolved
("Tuple[1:2:3]", "Tuple[1:2:3]"), # Index, Subscript, Slice
])
def test_unparse(source, expected):
module = ast.parse(source)
Expand Down