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

feat: Make MISSING_TYPE a None-alike object #1000

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gadenbuie
Copy link
Collaborator

@gadenbuie gadenbuie commented Jan 12, 2024

This PR updates MISSING_TYPE to make it behave similar to a None object by implementing several dunder methods. It also updates reactive.Value() to return the MISSING value when accessing an uninitialized reactive, rather than raising a SilentException.

This PR is current a proof-of-concept: there are a lot of red squiggles to track down where previously we relied on the SilentException to prevent a non-initialized input value from propagating. I'll continue to investigate and resolve these as I have time.

Behavior

>>> import shiny
>>> missing = shiny.types.MISSING
>>> missing
<MISSING>
>>> str(missing)
''
>>> missing or "default"
'default'
>>> 'yes' if missing else 'no'
'no'
>>> # No two missing values are ever the same
>>> missing == missing
False
>>> isinstance(missing, shiny.types.MISSING_TYPE)

Example App

Before this PR, the following app would never render the slider because calling input.slider() would raise an exception and the ui.input_slider() would never be returned.

from shiny import render
from shiny.express import input, ui

ui.input_switch("show_slider", "Show slider", True)


@render.ui
def uiElement():
    if input.show_slider():
        value = input.slider() or 5
        return ui.input_slider("slider", "Choose a number", min=1, max=10, value=value)

This could be worked around by calling

value = input.slider() if "slider" in input else 5

but that's not as concise or pythonic as value = input.slider() or 5.

@schloerke
Copy link
Collaborator

I'd like to throw an option into the ring that an uninitialized reactive.Value() throws an error and req() can catch the error.

Having to handle MISSING_TYPE for every reactive.Value() would be cumbersome to handle.

@gadenbuie
Copy link
Collaborator Author

@schloerke Agreed, that makes a lot of sense

@DvGils
Copy link

DvGils commented Jul 23, 2024

>>> # No two missing values are ever the same
>>> missing == missing
False

Just want to point out that None == None will return True in Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants