Skip to content

Commit

Permalink
Fix pyright, stack overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Nov 29, 2023
1 parent e5579fe commit b635d35
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ celerybeat-schedule
.env

# virtualenv
.venv
.venv/
.venv-*/
venv/
ENV/

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ clean-test: ## remove test and coverage artifacts
rm -fr .pytest_cache
rm -rf typings/

typings/comm:
pyright --createstub comm

typings/uvicorn:
pyright --createstub uvicorn

Expand All @@ -60,7 +63,7 @@ typings/matplotlib/__init__.pyi: ## grab type stubs from GitHub
typings/seaborn:
pyright --createstub seaborn

pyright: typings/uvicorn typings/matplotlib/__init__.pyi typings/seaborn ## type check with pyright
pyright: typings/comm typings/uvicorn typings/matplotlib/__init__.pyi typings/seaborn ## type check with pyright
pyright

lint: ## check style with flake8
Expand Down
4 changes: 2 additions & 2 deletions jupyterlab_extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ async function injectShinyDependencies() {
// Dynamically inserted <script> tags aren't guaranteed to load in order, so we need
// to serialize them using each one's load event.
const scripts = [
'/shiny/shared/jquery/jquery-3.6.0.js',
'/shiny/shared/shiny.js',
'/shiny/shared/jquery/jquery-3.6.0.min.js',
'/shiny/shared/shiny.min.js',
'/shiny/shared/bootstrap/bootstrap.bundle.min.js',
'/shiny/shared/ionrangeslider/js/ion.rangeSlider.min.js'
];
Expand Down
9 changes: 7 additions & 2 deletions shiny/notebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import asyncio
import uuid
from typing import Any, cast
from typing import Any

# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false
from IPython.core.interactiveshell import InteractiveShell
from IPython.display import display

import shiny._namespaces
import shiny.reactive
from shiny.session import _utils as session_utils
from shiny.types import SilentCancelOutputException, SilentException

from ._mimerender import initialize as initialize_mime_render
from .shiny_shim import JupyterKernelConnection, create_kernel_session
Expand All @@ -26,6 +26,11 @@ def load_ipython_extension(ipython: InteractiveShell):
# instance, which can be used in any way. This allows you to register
# new magics or aliases, for example.

# Squelch SilentException and SilentCancelOutputException (TODO: bad idea??)
ipython.set_custom_exc(
(SilentException, SilentCancelOutputException), lambda *a, **kw: None
)

initialize_mime_render(ipython)

sess, set_comm = create_kernel_session(uuid.uuid4().hex)
Expand Down
55 changes: 7 additions & 48 deletions shiny/notebook/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

# pyright: reportUnknownVariableType=false, reportUnknownMemberType=false, reportUntypedFunctionDecorator=false
import asyncio
import uuid
from contextlib import contextmanager
from typing import Any, cast
from typing import cast

from IPython.core.display_functions import display, update_display
from IPython.core.getipython import get_ipython
from IPython.core.interactiveshell import InteractiveShell
from IPython.core.magic import register_cell_magic
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
from IPython.display import HTML, clear_output
from IPython.display import clear_output

from shiny import reactive as shiny_reactive
from shiny import ui
Expand Down Expand Up @@ -69,7 +68,10 @@ def reactive(line: str, cell: str):

# TODO: If line/cell magics are still in the cell, error.

ipy = get_ipython()
if get_ipython() is None:
raise RuntimeError("ipython not found")

ipy = cast(InteractiveShell, get_ipython())

# This basically captures a reference to "the cell that's executing us" while we're
# still in the main IPython event loop. We need to re-install this whenever we
Expand Down Expand Up @@ -115,49 +117,6 @@ async def flush():
asyncio.create_task(flush())


# code = """
# import ipywidgets as widgets
# from shiny import reactive
# from IPython.core.getipython import get_ipython

# if "__{reactive_name}_output_effect__" in globals():
# __{reactive_name}_output_effect__.destroy()
# if "__{reactive_name}_output_sink__" in globals():
# # Output.clear_output() does not work reliably from "threaded" contexts
# # https://github.com/jupyter-widgets/ipywidgets/issues/3260#issuecomment-907715980
# __{reactive_name}_output_sink__.outputs = ()

# @reactive.Calc
# def {reactive_name}():
# res = get_ipython().run_cell('''{cell}''')
# if res.success:
# return res.result
# else:
# if execution_result.error_before_exec:
# raise execution_result.error_before_exec
# if execution_result.error_in_exec:
# raise execution_result.error_in_exec
# """
# code += (
# """
# __{reactive_name}_output_sink__ = widgets.Output()
# @reactive.Effect
# def __{reactive_name}_output_effect__():
# # Output.clear_output() does not work reliably from "threaded" contexts
# # https://github.com/jupyter-widgets/ipywidgets/issues/3260#issuecomment-907715980
# __{reactive_name}_output_sink__.outputs = ()
# __{reactive_name}_output_sink__.append_display_data({reactive_name}())
# display(__{reactive_name}_output_sink__)
# """
# if not args.no_echo
# else ""
# )

# code = code.replace("{reactive_name}", reactive_name).replace("{cell}", cell)

# ipy.run_cell(code)


@contextmanager
def set_parent(shell, parent_msg):
old_parent = shell.get_parent()
Expand Down
6 changes: 0 additions & 6 deletions shiny/render/transformer/_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,6 @@ def on_register(self) -> None:
self._session.output.remove(self.__name__)
self._auto_registered = False

from ...session import get_current_session

s = get_current_session()
if s is not None:
s.output(self)

def _set_metadata(self, session: Session, name: str) -> None:
"""
When `Renderer`s are assigned to Output object slots, this method is used to
Expand Down

0 comments on commit b635d35

Please sign in to comment.