Skip to content

Improve error messages for context managers called functionally #1008

@gadenbuie

Description

@gadenbuie

Users who see existing documentation or examples using shiny.ui.card() may try the following app structure

from shiny.express import ui

a_card = ui.card("A simple card")

but this will fail with a TypeError

[...snip...]

  File "/Users/garrick/work/posit-dev/py-shiny/shiny/express/app.py", line 14, in __getattr__
    return wrap_express_app(Path(name))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/garrick/work/posit-dev/py-shiny/shiny/express/_run.py", line 42, in wrap_express_app
    app_ui = run_express(file).tagify()
             ^^^^^^^^^^^^^^^^^
  File "/Users/garrick/work/posit-dev/py-shiny/shiny/express/_run.py", line 98, in run_express
    exec(
  File "/Users/garrick/work/posit-dev/py-shiny/shiny/api-examples/layout_column_wrap/app-express.py", line 3, in <module>
    a_card = ui.card("A simple card")
             ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: card() takes 0 positional arguments but 1 was given

This generic error is not very helpful for new users and does not provide actionable advice. Ideally, we'd instruct users to switch to the context manager form, possibly with an error like the following:

`card()` was called with positional arguments, but in Shiny Express `card()`
does not take unnamed arguments. Please use the `with ui.card():` form instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions