Description
I have some trouble with properly type hinting reactive objects when working with modules servers.
I use pyright as my LSP and I want to properly type annotate the return types of module server objects (that are reactive) such that in other code the static type checking is correct.
Type hinting reactive Calc:
Say I have reactive calc that returns an object of type 'T'. How do I type annotate the output of my module server in the following situation.
from shiny import module, reactive
@module.server
def mod_server(input, output, session) -> ???:
@reactive.calc
def my_calc() -> T:
return object_of_type_T
return my_calc
My pyright LSP is saying that the return type of mod_server is of type Calc_[T]
. But I see that Calc_
is explicitly listed as not exported over here: https://github.com/posit-dev/py-shiny/blob/main/shiny/reactive/__init__.py#L16
I could define type my own generic type that represent a ReactiveCalc function, but then pyright would complain about mismatch between listed return type and the Calc_[T]
return.
Am I missing something?
Type hinting input.xxx
Secondly, how to properly annotate input.xxx
reactives? Such that my Lsp would display the correct type for: input.xxx()
.
Eg. how would I type annotate the return of the following module server? (inspired by the module communcation article )
from shiny import module, render, ui
@module.ui
def city_state_ui():
return ui.input_selectize("state", "State", choices=["NY", "CO", "OR", "MI"], selected="NY")
@module.server
def mod_server(input, output, session) -> ???:
return input.state
Reactive value
Type hinting with reactive value objects seems to be supported. At least the following type hinting results in expected behaviour:
from shiny import reactive
from shiny.reactive import Value
x : Value[int] = reactive.value(1)