Skip to content

Commit 657a8f4

Browse files
authored
Transform sorting function specification file from Markdown to RST (#333)
* Transform sorting functions from md to rst file * Fix the warning message * Add autosummary with signatures for the sorting functions * Bump CI python to 3.8 * Complete signatures and add types files * Remove module names and add autodoc typehints * Bump Sphinx version * Remove module import from signature and return annonation * Add jinja template for autosummary * Put return section style as parameters and remove return type section
1 parent 545d7f1 commit 657a8f4

File tree

10 files changed

+150
-87
lines changed

10 files changed

+150
-87
lines changed

Diff for: .circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ _defaults: &defaults
55
docker:
66
# CircleCI maintains a library of pre-built images
77
# documented at https://circleci.com/docs/2.0/circleci-images/
8-
- image: circleci/python:3.7.0
8+
- image: circleci/python:3.8.0
99
working_directory: ~/repo
1010

1111
jobs:

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ spec/_build/
22
build/
33
.vscode/
44
node_modules/
5+
__pycache__/
6+
*.pyc

Diff for: requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sphinx==3.1.1
1+
sphinx==4.3.0
22
sphinx-material==0.0.30
33
myst-parser
44
sphinx_markdown_tables

Diff for: spec/API_specification/signatures/__init__.py

Whitespace-only changes.

Diff for: spec/API_specification/signatures/_types.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
This file defines the types for type annotations.
3+
4+
The type variables should be replaced with the actual types for a given
5+
library, e.g., for NumPy TypeVar('array') would be replaced with ndarray.
6+
"""
7+
8+
from dataclasses import dataclass
9+
from typing import Any, List, Literal, Optional, Sequence, Tuple, TypeVar, Union
10+
11+
array = TypeVar('array')
12+
device = TypeVar('device')
13+
dtype = TypeVar('dtype')
14+
SupportsDLPack = TypeVar('SupportsDLPack')
15+
SupportsBufferProtocol = TypeVar('SupportsBufferProtocol')
16+
PyCapsule = TypeVar('PyCapsule')
17+
# ellipsis cannot actually be imported from anywhere, so include a dummy here
18+
# to keep pyflakes happy. https://github.com/python/typeshed/issues/3556
19+
ellipsis = TypeVar('ellipsis')
20+
21+
@dataclass
22+
class finfo_object:
23+
bits: int
24+
eps: float
25+
max: float
26+
min: float
27+
smallest_normal: float
28+
29+
@dataclass
30+
class iinfo_object:
31+
bits: int
32+
max: int
33+
min: int
34+
35+
# This should really be recursive, but that isn't supported yet.
36+
NestedSequence = Sequence[Sequence[Any]]
37+
38+
__all__ = ['Any', 'List', 'Literal', 'NestedSequence', 'Optional',
39+
'PyCapsule', 'SupportsBufferProtocol', 'SupportsDLPack', 'Tuple', 'Union',
40+
'array', 'device', 'dtype', 'ellipsis', 'finfo_object', 'iinfo_object']
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from ._types import array
2+
3+
def argsort(x: array, /, *, axis: int = -1, descending: bool = False, stable: bool = True) -> array:
4+
"""
5+
Returns the indices that sort an array ``x`` along a specified axis.
6+
7+
Parameters
8+
----------
9+
x : array
10+
input array.
11+
axis: int
12+
axis along which to sort. If set to ``-1``, the function must sort along the last axis. Default: ``-1``.
13+
descending: bool
14+
sort order. If ``True``, the returned indices sort ``x`` in descending order (by value). If ``False``, the returned indices sort ``x`` in ascending order (by value). Default: ``False``.
15+
stable: bool
16+
sort stability. If ``True``, the returned indices must maintain the relative order of ``x`` values which compare as equal. If ``False``, the returned indices may or may not maintain the relative order of ``x`` values which compare as equal (i.e., the relative order of ``x`` values which compare as equal is implementation-dependent). Default: ``True``.
17+
18+
Returns
19+
-------
20+
out : array
21+
an array of indices. The returned array must have the same shape as ``x``. The returned array must have the default array index data type.
22+
"""
23+
24+
def sort(x: array, /, *, axis: int = -1, descending: bool = False, stable: bool = True) -> array:
25+
"""
26+
Returns a sorted copy of an input array ``x``.
27+
28+
Parameters
29+
----------
30+
x: array
31+
input array.
32+
axis: int
33+
axis along which to sort. If set to ``-1``, the function must sort along the last axis. Default: ``-1``.
34+
descending: bool
35+
sort order. If ``True``, the array must be sorted in descending order (by value). If ``False``, the array must be sorted in ascending order (by value). Default: ``False``.
36+
stable: bool
37+
sort stability. If ``True``, the returned array must maintain the relative order of ``x`` values which compare as equal. If ``False``, the returned array may or may not maintain the relative order of ``x`` values which compare as equal (i.e., the relative order of ``x`` values which compare as equal is implementation-dependent). Default: ``True``.
38+
39+
Returns
40+
-------
41+
out : array
42+
a sorted array. The returned array must have the same data type and shape as ``x``.
43+
"""
44+
45+
__all__ = ['argsort', 'sort']

Diff for: spec/API_specification/sorting_functions.md

-81
This file was deleted.

Diff for: spec/API_specification/sorting_functions.rst

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Sorting Functions
2+
=================
3+
4+
Array API specification for sorting functions.
5+
6+
A conforming implementation of the array API standard must provide and support the following functions adhering to the following conventions.
7+
8+
* Positional parameters must be `positional-only <https://www.python.org/dev/peps/pep-0570/>`_ parameters. Positional-only parameters have no externally-usable name. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order.
9+
* Optional parameters must be `keyword-only <https://www.python.org/dev/peps/pep-3102/>`_ arguments.
10+
* Unless stated otherwise, functions must support the data types defined in :ref:`data-types`.
11+
12+
.. note::
13+
14+
For floating-point input arrays, the sort order of NaNs and signed zeros is unspecified and thus implementation-dependent.
15+
16+
Implementations may choose to sort signed zeros (``-0 < +0``) or may choose to rely solely on value equality (``==``).
17+
18+
Implementations may choose to sort NaNs (e.g., to the end or to the beginning of a returned array) or leave them in-place. Should an implementation sort NaNs, the sorting convention should be clearly documented in the conforming implementation's documentation.
19+
20+
While defining a sort order for IEEE 754 floating-point numbers is recommended in order to facilitate reproducible and consistent sort results, doing so is not currently required by this specification.
21+
22+
.. currentmodule:: signatures.sorting_functions
23+
24+
Objects in API
25+
--------------
26+
..
27+
NOTE: please keep the functions in alphabetical order
28+
29+
.. autosummary::
30+
:toctree: generated
31+
:template: method.rst
32+
33+
argsort
34+
sort

Diff for: spec/_templates/method.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. currentmodule:: {{ module }}
2+
3+
{{ name.split('.')[-1] | underline }}
4+
5+
.. autofunction:: {{ name }}

Diff for: spec/conf.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
# add these directories to sys.path here. If the directory is relative to the
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
13-
# import os
14-
# import sys
15-
# sys.path.insert(0, os.path.abspath('.'))
16-
13+
import os
14+
import sys
1715
import sphinx_material
16+
sys.path.insert(0, os.path.abspath('./API_specification'))
1817

1918
# -- Project information -----------------------------------------------------
2019

@@ -38,8 +37,16 @@
3837
'sphinx.ext.todo',
3938
'sphinx_markdown_tables',
4039
'sphinx_copybutton',
40+
'sphinx.ext.autosummary',
41+
'sphinx.ext.napoleon',
42+
'sphinx.ext.autodoc',
4143
]
4244

45+
autosummary_generate = True
46+
autodoc_typehints = 'signature'
47+
add_module_names = False
48+
napoleon_custom_sections = [('Returns', 'params_style')]
49+
4350
# Add any paths that contain templates here, relative to this directory.
4451
templates_path = ['_templates']
4552

@@ -145,3 +152,14 @@
145152
"dudir": ("http://docutils.sourceforge.net/docs/ref/rst/" "directives.html#%s", ""),
146153
"pypa": ("https://packaging.python.org/%s", ""),
147154
}
155+
156+
157+
def process_signature(app, what, name, obj, options, signature, return_annotation):
158+
if signature:
159+
signature = signature.replace("signatures._types.", "")
160+
if return_annotation:
161+
return_annotation = return_annotation.replace("signatures._types.", "")
162+
return signature, return_annotation
163+
164+
def setup(app):
165+
app.connect("autodoc-process-signature", process_signature)

0 commit comments

Comments
 (0)