Skip to content

Commit 6c802c3

Browse files
committed
Document parse_unary_docstring()
1 parent 180b152 commit 6c802c3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

array_api_tests/test_special_cases.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,46 @@ def check_result(i: float, result: float) -> bool:
586586

587587

588588
def parse_unary_docstring(docstring: str) -> List[UnaryCase]:
589+
"""
590+
Parses a Sphinx-formatted docstring of a unary function to return a list of
591+
codified unary cases, e.g.
592+
593+
>>> def sqrt(x: array, /) -> array:
594+
... '''
595+
... Calculates the square root
596+
...
597+
... **Special Cases**
598+
...
599+
... For floating-point operands,
600+
...
601+
... - If ``x_i`` is ``NaN``, the result is ``NaN``.
602+
... - If ``x_i`` is less than ``0``, the result is ``NaN``.
603+
... - If ``x_i`` is ``+0``, the result is ``+0``.
604+
... - If ``x_i`` is ``-0``, the result is ``-0``.
605+
... - If ``x_i`` is ``+infinity``, the result is ``+infinity``.
606+
...
607+
... Parameters
608+
... ----------
609+
... x: array
610+
... input array. Should have a floating-point data type
611+
...
612+
... Returns
613+
... -------
614+
... out: array
615+
... an array containing the square root of each element in ``x``
616+
... '''
617+
... ...
618+
>>> unary_cases = parse_unary_docstring(sqrt.__doc__)
619+
>>> for case in unary_cases:
620+
... print(repr(case))
621+
UnaryCase(x_i == NaN -> NaN)
622+
UnaryCase(x_i < 0 -> NaN)
623+
UnaryCase(x_i == +0 -> +0)
624+
UnaryCase(x_i == -0 -> -0)
625+
UnaryCase(x_i == +infinity -> +infinity)
626+
627+
"""
628+
589629
match = r_special_cases.search(docstring)
590630
if match is None:
591631
return []

0 commit comments

Comments
 (0)