@@ -586,6 +586,46 @@ def check_result(i: float, result: float) -> bool:
586
586
587
587
588
588
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
+
589
629
match = r_special_cases .search (docstring )
590
630
if match is None :
591
631
return []
0 commit comments