Skip to content

Commit 1820531

Browse files
Added support for slice objects in subscriptions (#11981)
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 2fe103a commit 1820531

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Features added
2727
* #11892: Improved performance when resolving cross references in cpp domain.
2828
Patch by Rouslan Korneychuk.
2929

30+
* #11981: Improve rendering of signatures using ``slice`` syntax,
31+
e.g., ``def foo(arg: np.float64[:,:]) -> None: ...``.
32+
3033
Bugs fixed
3134
----------
3235

sphinx/pycode/ast.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ def visit_Name(self, node: ast.Name) -> str:
156156
def visit_Set(self, node: ast.Set) -> str:
157157
return "{" + ", ".join(self.visit(e) for e in node.elts) + "}"
158158

159+
def visit_Slice(self, node: ast.Slice) -> str:
160+
if not node.lower and not node.upper and not node.step:
161+
# Empty slice with default values -> [:]
162+
return ":"
163+
164+
start = self.visit(node.lower) if node.lower else ""
165+
stop = self.visit(node.upper) if node.upper else ""
166+
if not node.step:
167+
# Default step size -> [start:stop]
168+
return f"{start}:{stop}"
169+
170+
step = self.visit(node.step) if node.step else ""
171+
return f"{start}:{stop}:{step}"
172+
159173
def visit_Subscript(self, node: ast.Subscript) -> str:
160174
def is_simple_tuple(value: ast.expr) -> bool:
161175
return (

tests/roots/test-ext-autodoc/target/functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ async def asyncgenerator():
1717

1818
builtin_func = print
1919
partial_builtin_func = partial(print)
20+
21+
def slice_arg_func(arg: 'float64[:, :]'):
22+
pass

tests/test_extensions/test_ext_autodoc_autofunction.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,14 @@ def test_async_generator(app):
199199
' :async:',
200200
'',
201201
]
202+
203+
204+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
205+
def test_slice_function_arg(app):
206+
actual = do_autodoc(app, 'function', 'target.functions.slice_arg_func')
207+
assert list(actual) == [
208+
'',
209+
'.. py:function:: slice_arg_func(arg: float64[:, :])',
210+
' :module: target.functions',
211+
'',
212+
]

tests/test_pycode/test_pycode_ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
"lambda x=0, /, y=1, *args, z, **kwargs: ..."), # posonlyargs
5252
("0x1234", "0x1234"), # Constant
5353
("1_000_000", "1_000_000"), # Constant
54+
("Tuple[:,:]", "Tuple[:, :]"), # Index, Subscript, 2x Slice
55+
("Tuple[1:2]", "Tuple[1:2]"), # Index, Subscript, Slice(no-step)
56+
("Tuple[1:2:3]", "Tuple[1:2:3]"), # Index, Subscript, Slice
57+
("x[:, np.newaxis, :, :]",
58+
"x[:, np.newaxis, :, :]"), # Index, Subscript, numpy extended syntax
59+
("y[:, 1:3][np.array([0, 2, 4]), :]",
60+
"y[:, 1:3][np.array([0, 2, 4]), :]"), # Index, 2x Subscript, numpy extended syntax
5461
])
5562
def test_unparse(source, expected):
5663
module = ast.parse(source)

0 commit comments

Comments
 (0)