blob: 04d0766d52c00d3a2d7f5a97f61fdace2a9b7a29 [file] [view] [edit]
# Tests for Jupyter Notebooks
## Notebook Invalid
```scrut
$ echo -e "x: int = 1" > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
ERROR * Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON* (glob)
--> */notebook.ipynb:1:1 (glob)
|
|
[1]
```
## Notebook Invalid JSON
```scrut
$ echo -e "{}" > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
ERROR * This file does not match the schema expected of Jupyter Notebooks: missing field `cells`* (glob)
--> */notebook.ipynb:1:1 (glob)
|
|
[1]
```
## Notebook Top Level Await
```scrut
$ echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["import asyncio\\nawait asyncio.sleep(1)"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
[0]
```
## Notebook Top Level Async With
```scrut
$ echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["import asyncio\\nasync with asyncio.timeout(1):\\n await asyncio.sleep(0.5)"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
[0]
```
## Notebook Top Level Async For
```scrut
$ echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["async def arange(n):\\n for i in range(n):\\n yield i\\nasync for x in arange(5):\\n pass"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
[0]
```
## Shebang Notebook Top Level Await
```scrut
$ echo -e '#!/usr/bin/env -S notebookrunner --kernel default\nimport asyncio\nawait asyncio.sleep(1)' > $TMPDIR/notebook.py && \
> $PYREFLY check $TMPDIR/notebook.py
[0]
```
## Shebang Notebook Top Level Async With
```scrut
$ echo -e '#!/usr/bin/env -S notebookrunner --kernel default\nimport asyncio\nasync with asyncio.timeout(1):\n await asyncio.sleep(0.5)' > $TMPDIR/notebook.py && \
> $PYREFLY check $TMPDIR/notebook.py
[0]
```
## Shebang Notebook Top Level Async For
```scrut
$ echo -e '#!/usr/bin/env -S notebookrunner --kernel default\nasync def arange(n):\n for i in range(n):\n yield i\nasync for x in arange(5):\n pass' > $TMPDIR/notebook.py && \
> $PYREFLY check $TMPDIR/notebook.py
[0]
```
## Non-Notebook Python Top Level Await Rejected
```scrut
$ echo -e 'import asyncio\nawait asyncio.sleep(1)' > $TMPDIR/regular.py && \
> $PYREFLY check $TMPDIR/regular.py
ERROR `await` can only be used inside an async function [invalid-syntax]
--> */regular.py:2:1 (glob)
|
* (glob)
* (glob)
|
[1]
```
## Notebook Directive
```scrut
$ echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["%matplotlib inline"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
[0]
```
## Notebook Error
```scrut
$ touch $TMPDIR/pyrefly.toml && \
> echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x: bool = 5"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
ERROR `Literal[5]` is not assignable to `bool` [bad-assignment]
--> */notebook.ipynb#1:1:11 (glob)
|
1 | x: bool = 5
| ---- ^
| |
| declared type
|
[1]
```
## Notebook Error Second Cell
```scrut
$ touch $TMPDIR/pyrefly.toml && \
> echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x: bool = True"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["x: bool = 5"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
ERROR `Literal[5]` is not assignable to `bool` [bad-assignment]
--> */notebook.ipynb#2:1:11 (glob)
|
1 | x: bool = 5
| ---- ^
| |
| declared type
|
[1]
```
## Notebook Suppressed Error
```scrut
$ echo -e '{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["# type: ignore\\nx: bool = 5"]}],"metadata":{"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":4}' > $TMPDIR/notebook.ipynb && \
> $PYREFLY check $TMPDIR/notebook.ipynb
[0]
```