Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stricter lark grammar for notebook documents #143

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
79 changes: 38 additions & 41 deletions docs/Untitled42.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"i was printed from and my name is __main__\n"
]
}
],
"outputs": [],
"source": [
"from importnb import get_ipython\n",
"from pathlib import Path\n",
Expand Down Expand Up @@ -333,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -350,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -367,7 +359,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -384,25 +376,17 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"i'm only show when cell magics are active.\n"
]
}
],
"outputs": [],
"source": [
"%%python\n",
"print(\"i'm only show when cell magics are active.\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -430,7 +414,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -443,7 +427,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -472,7 +456,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -482,7 +466,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -505,20 +489,29 @@
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if get_ipython() and not where:\n",
" from importnb import loaders\n",
" display(loaders.Json.load_file(\"Untitled42.ipynb\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:root] *",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "conda-root-py"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -530,11 +523,13 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.3"
},
"vscode": {
"interpreter": {
"hash": "6624ee388a1c346f3d0811b591fe9e170807496b8a5fea1a5f5986a819dc2334"
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
Expand Down Expand Up @@ -571,9 +566,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:root] *",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "conda-root-py"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -585,11 +580,13 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.3"
},
"vscode": {
"interpreter": {
"hash": "6624ee388a1c346f3d0811b591fe9e170807496b8a5fea1a5f5986a819dc2334"
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions docs/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from subprocess import check_call
from sys import executable, path, version_info

from pytest import importorskip
from pytest import importorskip, mark

from importnb import Notebook
from importnb import __version__ as importnb_version
Expand Down Expand Up @@ -92,7 +92,8 @@ def test_module():
"""


@cli_test("-m importnb -c '{}'")
@mark.xfail
@cli_test("""-m importnb -c '{"cells": []}'""")
def test_empty_code():
""""""

Expand Down
3 changes: 2 additions & 1 deletion docs/test_importnb.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def test_load_file(clean, ref):


def test_load_code(clean):
assert Notebook.load_code(""), "can't load an empty notebook"
with raises(BaseException):
assert Notebook.load_code(""), "can't load an empty notebook"
body = Path("docs/Untitled42.ipynb").read_text()
m = Notebook.load_code(body)
cant_reload(m)
Expand Down
43 changes: 43 additions & 0 deletions docs/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from json import dumps
from pathlib import Path
from hypothesis_jsonschema import from_schema
from hypothesis import given
from pytest import mark, raises
from importnb.decoder import InvalidNotebook, parse_nbformat

HERE = Path(__file__).parent


@mark.parametrize("file", [HERE / "test_in_notebook.ipynb"])
def test_parse_notebook(file):
parse_nbformat(file.read_text())


@mark.parametrize("file", [HERE / "ascript.ipy"])
def test_parse_notebook_fail(file):
with raises(InvalidNotebook):
parse_nbformat(file.read_text())


@given(from_schema(dict(dict(type=["object", "string"]))))
def test_random_objects_fail(object):
with raises(InvalidNotebook):
return parse_nbformat(dumps(object))


@given(
from_schema(
dict(
properties=dict(
cells=dict(
type="array",
items=dict(dict(type=["object", "string"])),
),
additionalProperties=False,
)
)
)
)
def test_random_cells_fail(object):
with raises(InvalidNotebook):
return parse_nbformat(dumps(object))
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies = [
"tomli",
"ruamel.yaml",
"tomli_w",
"hypothesis-jsonschema"
]

[[tool.hatch.envs.test.matrix]]
Expand Down
5 changes: 1 addition & 4 deletions src/importnb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def get_ipython(force=True):
return None


import builtins

from ._version import __version__
from .entry_points import imports
from .loader import Notebook, reload

builtins.true, builtins.false, builtins.null = True, False, None
from .entry_points import imports
Loading