Skip to content

Commit

Permalink
Use the doctest module in get_example_data (#308)
Browse files Browse the repository at this point in the history
Fixes #282 

Still several todos here:

- [x] Clean up code (probably don't need to define the class inside of
the function)
- [ ] Add tests
- [ ] Check that the generated JSON is as desired (the execution status
is not actually included. I'm not clear why)
- [ ] Make it so that any doctest anywhere is run, not just ones in
"Examples" (this doesn't necessarily need to be done in this PR)
- [ ] Allow libraries to configure doctest options (like ELLIPSIS)

Here's an example:

```py
def docstring(x):
    """
    Examples
    ========

    >>> from test_mod import docstring
    >>> a = docstring(1)
    >>> a
    2

    >>> 1 + a
    3

    >>> import matplotlib.pyplot as plt
    >>> plt.plot([0, 1], [0, 1])
    >>> plt.show()

    >>> 1 + 1
    2

    >>> syntax error

    >>> 1/0 # exception
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ZeroDivisionError: division by zero

    >>> 1/0 # unexpected exception
    """
    return x + 1

__version__ = '0'
```

```toml
[global]
module = 'test_mod'
```

Generates

```json
  "example_section_data": {
    "children": [
      {
        "type": "code",
        "value": "from test_mod import docstring\n"
      },
      {
        "type": "code",
        "value": "a = docstring(1)\n"
      },
      {
        "type": "code",
        "value": "a\n"
      },
      {
        "type": "code",
        "value": "1 + a\n"
      },
      {
        "type": "code",
        "value": "import matplotlib.pyplot as plt\n"
      },
      {
        "type": "code",
        "value": "plt.plot([0, 1], [0, 1])\n"
      },
      {
        "type": "code",
        "value": "plt.show()\n"
      },
      {
        "type": "Fig",
        "value": {
          "kind": "assets",
          "module": "test_mod",
          "path": "fig-test_mod:docstring-0-c8430bd5.png",
          "type": "RefInfo",
          "version": "0"
        }
      },
      {
        "type": "code",
        "value": "1 + 1\n"
      },
      {
        "type": "code",
        "value": "syntax error\n"
      },
      {
        "type": "code",
        "value": "1/0 # exception\n"
      },
      {
        "type": "code",
        "value": "1/0 # unexpected exception\n"
      }
    ],
```
  • Loading branch information
Carreau authored Jan 10, 2024
2 parents ccc963a + 81071b9 commit 1861202
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 163 deletions.
2 changes: 1 addition & 1 deletion examples/IPython.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module = 'IPython'
logo = "img/ipython-logo.png"
exec_failure = "fallback"
docs_path = "~/dev/IPython/docs/source"
exec = true
execute_doctests = true
execute_exclude_patterns = [
'IPython.lib.display:Audio',
'IPython.core.display_functions:display'
Expand Down
1 change: 1 addition & 0 deletions examples/dask.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ exclude = [ "dask.utils:Dispatch",

#docs_path = "~/dev/dask/docs/source"
exec_failure = 'fallback'
execute_doctests = false
1 change: 1 addition & 0 deletions examples/distributed.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exclude = [
"distributed.shuffle._comms:CommShardsBuffer",
] #docs_path = "~/dev/dask/docs/source"
exec_failure = 'fallback'
execute_doctests = false

[global.expected_errors]
NumpydocParseError = [
Expand Down
2 changes: 1 addition & 1 deletion examples/matplotlib.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ examples_exclude = [
"lines_bars_and_markers/marker_reference.py",
"lines_bars_and_markers/multivariate_marker_plot.py",
]
exec = true
execute_doctests = true

[global.expected_errors]
IncorrectInternalDocsLen = [
Expand Down
2 changes: 1 addition & 1 deletion examples/networkx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ submodules = []

# docs_path = '~/dev/numpy/doc/source'

exec = true
execute_doctests = true
exclude_jedi = ['networkx.algorithms.planarity.PlanarEmbedding']
exec_failure = 'fallback'
source = 'https://github.com/networkx/networkx'
Expand Down
3 changes: 2 additions & 1 deletion examples/numpy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exclude = [
]

execute_exclude_patterns = [
'numpy:set_numeric_ops',
'numpy._',
'numpy.testing._priv',
'numpy.errstate',
Expand Down Expand Up @@ -62,7 +63,7 @@ submodules = [

docs_path = '~/dev/numpy/doc/source'

exec = true
execute_doctests = true
narrative_exclude = [
'doc/source/reference/arrays.ndarray.rst',
'doc/source/user/how-to-how-to.rst',
Expand Down
4 changes: 4 additions & 0 deletions examples/pandas.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exec_failure = "fallback"
submodules = []
exclude = [""
]
execute_doctests = false
[pandas.expected_errors]
IncorrectInternalDocsLen = [
"pandas.core.indexes.period:PeriodIndex.asof_locs",
Expand Down Expand Up @@ -43,3 +44,6 @@ WrongTypeAtField = [
"pandas.core.resample:Resampler.interpolate",
"pandas.core.generic:NDFrame.to_pickle",
]

[global.implied_imports]
pd = 'pandas'
2 changes: 1 addition & 1 deletion examples/papyri.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ submodules = ['examples']
examples_folder = '~/dev/papyri/docs/examples/'
logo = "../papyri-logo.png"
docs_path = "~/dev/papyri/docs"
exec = true
execute_doctests = true
exec_failure = 'raise'
exclude= ["papyri.utils:FullQual","papyri.utils:Cannonical" ]
[global.directives]
Expand Down
2 changes: 1 addition & 1 deletion examples/scipy.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[global]
module = 'scipy'
logo = "img/scipy_logo.png"
exec = true
execute_doctests = true
submodules = [ "signal","misc"]
exclude = [
# OTHER.
Expand Down
2 changes: 1 addition & 1 deletion examples/xarray.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ execute_exclude_patterns = []
submodules = []


exec = true
execute_doctests = true
exclude = [
]
exec_failure = 'fallback'
Expand Down
22 changes: 11 additions & 11 deletions papyri/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ async def example1(
Examples
--------
>>> import matplotlib.pyplot as plt
... fig, ax = plt.subplots()
... ax.plot(range(10))
>>> fig, ax = plt.subplots()
>>> ax.plot(range(10))
And now
>>> ax.plot(range(5, 15))
... plt.show()
>>> plt.show()
"""
return "ok"
Expand All @@ -243,24 +243,24 @@ def example2():
Examples
--------
>>> import numpy as np
... import matplotlib.pyplot as plt
... from scipy.interpolate import UnivariateSpline
>>> import matplotlib.pyplot as plt
>>> from scipy.interpolate import UnivariateSpline
>>> x = np.linspace(-3, 3, 50)
... y = np.exp(-(x ** 2)) + 0.1 * np.random.randn(50)
... plt.plot(x, y, "ro", ms=5)
>>> y = np.exp(-(x ** 2)) + 0.1 * np.random.randn(50)
>>> plt.plot(x, y, "ro", ms=5)
Use the default value for the smoothing parameter:
>>> spl = UnivariateSpline(x, y)
... xs = np.linspace(-3, 3, 1000)
... plt.plot(xs, spl(xs), "g", lw=3)
>>> xs = np.linspace(-3, 3, 1000)
>>> plt.plot(xs, spl(xs), "g", lw=3)
Manually change the amount of smoothing:
>>> spl.set_smoothing_factor(0.5)
... plt.plot(xs, spl(xs), "b", lw=3)
... plt.show()
>>> plt.plot(xs, spl(xs), "b", lw=3)
>>> plt.show()
"""
pass
Expand Down
Loading

0 comments on commit 1861202

Please sign in to comment.