Skip to content

Conventions

Three rules hold across the library, so a behavior learned in one place works the same way in the next.

The Three-Tier Precedence Model

Where you can choose a behavior in more than one place, the later place wins:

  1. The class attribute, the default for every view of the class.
  2. A method you override, deciding for one view while it runs.
  3. An argument at the call, which wins over both.
from cascadeui import StatefulLayoutView, button_row, card


class Draft(StatefulLayoutView):
    exit_policy = "delete"  # (1)!

    # ... text, unsaved, and a save() callback, as the panel keeps them

    def build_ui(self):
        self.clear_items()
        self.add_item(card("## Draft", self.text, button_row({"Save": self.save})))

    async def exit(self, delete_message=None):
        if delete_message is None and self.unsaved:  # (2)!
            delete_message = False
        await super().exit(delete_message=delete_message)


# ... later, with draft a Draft that was sent
await draft.exit(delete_message=True)  # (3)!
  1. The class: closing deletes the message.
  2. The override: unsaved work stays on screen with its controls disabled. It changes only what the caller left as None, so the third tier still wins.
  3. The call: deleted, saved or not.

Diagram: the Draft panel closed three ways. With nothing unsaved, draft.exit() follows exit_policy = "delete" and the message is deleted. With unsaved work, the exit() override passes delete_message=False, so the panel stays with Save disabled. With unsaved work, draft.exit(delete_message=True) wins over the override, and the message is deleted.

A *_message attribute and its on_* hook are the first two tiers: the default on_unauthorized() sends unauthorized_message, and an override decides for itself. set_class_attribute(name, value) changes a class attribute for one view.

Sync or Async, Your Choice

Write async def when the body awaits something and a plain def when it does not:

class Standings(StatefulLayoutView):
    async def on_load(self):  # awaits, so async
        self.rows = await db.fetch_top(10)

    def build_ui(self):  # composes, so not
        self.clear_items()
        self.add_item(card(*self.rows))

Every place the library runs your code takes either shape: view hooks, component callbacks, store subscribers and store.on() hooks, reducers, middleware, validators, the pattern builders, from_cursor()'s fetch function, and migrators. The library checks what your function returns, so an object with an async __call__ and a functools.partial of an async function work too.

The places below are the exceptions. They run where nothing can wait, so each needs a plain def. An async one raises TypeError where you hand it to the library, naming the place and the fix:

build_ui takes either shape on every view but three. MenuLayoutView, RolesLayoutView, and DisplayLayoutView build their tree in __init__, which cannot await, so an async def build_ui on one of them raises TypeError when it is built, pointing at on_load() for the work that awaits.

Mappings and Pairs

These take a sequence of (key, value) pairs as well as a dict, and any other mapping (an OrderedDict, a UserDict, a defaultdict), in the order given:

from cascadeui import key_value

key_value({"Wins": 12, "Losses": 3})
key_value([("Wins", 12), ("Losses", 3)])  # the same lines
Takes pairs Two pairs sharing a key
key_value(data=), stats_card(stats=) two lines
button_row(buttons=), tab_nav(tabs=), choice_row(options=) two entries
Theme(styles=), TabView(tabs=), TabLayoutView(tabs=), RoleCategory(roles=) ValueError naming the key

The builders draw one entry per pair, so a label read from data can repeat. A theme's style, a tab's name, or a role button's label must mean one thing, where a dict would keep only the last. Anything else, text included, raises TypeError naming the builder and the argument.

Extension Strategies

How the library adds to discord.py (subclasses, builders, wrappers, patterns, and inputs) has a page of its own: Extension Strategies.