Skip to content

Commit

Permalink
Avoid mutable objects as function defaults; use Root as root ns
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Jun 24, 2022
1 parent aee9d36 commit 5fb0722
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions shiny/session/_session.py
Expand Up @@ -162,8 +162,8 @@ def __init__(
# query information about the request, like headers, cookies, etc.
self.http_conn: HTTPConnection = conn.get_http_conn()

self.input: Inputs = Inputs()
self.output: Outputs = Outputs(self)
self.input: Inputs = Inputs(dict())
self.output: Outputs = Outputs(self, self.ns, dict(), dict())

self.user: Union[str, None] = None
self.groups: Union[List[str], None] = None
Expand Down Expand Up @@ -301,14 +301,20 @@ def _manage_inputs(self, data: Dict[str, object]) -> None:
if len(keys) == 2:
val = input_handlers._process_value(keys[1], val, keys[0], self)

self.input[keys[0]]._set(val)
# The keys[0] value is already a fully namespaced id; make that explicit by
# wrapping it in ResolvedId, otherwise self.input will throw an id
# validation error.
self.input[ResolvedId(keys[0])]._set(val)

self.output._manage_hidden()

def _is_hidden(self, name: str) -> bool:
with isolate():
# The .clientdata_output_{name}_hidden string is already a fully namespaced
# id; make that explicit by wrapping it in ResolvedId, otherwise self.input
# will throw an id validation error.
hidden_value_obj = cast(
Value[bool], self.input[f".clientdata_output_{name}_hidden"]
Value[bool], self.input[ResolvedId(f".clientdata_output_{name}_hidden")]
)
if not hidden_value_obj.is_set():
return True
Expand Down Expand Up @@ -365,7 +371,10 @@ async def uploadEnd(job_id: str, input_id: str) -> None:
)
return None
file_data = upload_op.finish()
self.input[input_id]._set(file_data)
# The input_id string is already a fully namespaced id; make that explicit
# by wrapping it in ResolvedId, otherwise self.input will throw an id
# validation error.
self.input[ResolvedId(input_id)]._set(file_data)
# Explicitly return None to signal that the message was handled.
return None

Expand Down Expand Up @@ -822,7 +831,7 @@ class Inputs:
"""

def __init__(
self, values: Dict[str, Value[Any]] = {}, ns: Callable[[str], str] = lambda x: x
self, values: Dict[str, Value[Any]], ns: Callable[[str], str] = Root
) -> None:
self._map = values
self._ns = ns
Expand Down Expand Up @@ -881,9 +890,9 @@ class Outputs:
def __init__(
self,
session: Session,
ns: Callable[[str], str] = lambda x: x,
effects: Dict[str, Effect_] = {},
suspend_when_hidden: Dict[str, bool] = {},
ns: Callable[[str], str],
effects: Dict[str, Effect_],
suspend_when_hidden: Dict[str, bool],
) -> None:
self._session = session
self._ns = ns
Expand Down

0 comments on commit 5fb0722

Please sign in to comment.