You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am just getting into app-model and its features. I noticed that context is typed as MutableMapping which makes sense. In my case, I want to use typing. I ended up using TypedDict as it is typed. It seems to work (if you ignore the warning).
Is there a side effect issue of using TypedDict? If not, i suggest the inclusion of TypedDict as a possible type for the context variable.
Is there anythong way to use a class? some of the AIs are suggesting to use ContextNamespace but i am not sure how to use and couldn't get it to work.
there are some subtleties here, and lots of room for improvement, Ultimately though, I would just suggest using type ignore or casting dict(my_dict) when you pass it into the Application, and move on with your life:
conceptually, you really can think of a context as any old mutable mapping. That "mutable" part is important however... And a TypedDict is not, technically speaking, a MutableMapping (since it has a fixed set of keys... yes, you can mutate the value of any of those keys, but because it doesn't accept new keys, it's not actually a MutableMapping).
When you pass a mapping into the Application constructor, it uses it to create a Context object (Context(your_object)). That doesn't copy the dictionary, it stores it directly. Arguably, we could just call dict(your_object), and then we could relax the type hint to context: Mapping (but, we still couldn't use TypedDict as a possible type ... it's not correct here). But you can also just pass context=dict(your_object) yourself and that would also work.
I definitely concede that the ContextNamespace thing isn't very intuitive. I'm working on docs in #284 ... but even as I write them I'm not super happy with the mental mode. However, to get you started, here's a rough example of how you would currently use ContextNamespace to get static typing of context values, and (the more important bit) use the keys directly in expressions (claude made this example, but I read it and agree):
from __future__ importannotationsfromapp_modelimportAction, Applicationfromapp_model.expressionsimportContextKey, ContextNamespace# 1. Declare your typed namespace -----------------------------------------# `ContextNamespace[A]` is generic in the type passed to a (rarely-used)# `getter` callback; if you don't use getters, `object` is fine.# `ContextKey[A, T]` annotations are what give static typing on reads/writes.classAppKeys(ContextNamespace[object]):
selected_count: ContextKey[object, int] =ContextKey(
default_value=0,
description="Number of currently selected items.",
)
is_dirty: ContextKey[object, bool] =ContextKey(
default_value=False,
description="Whether the document has unsaved changes.",
)
document_title: ContextKey[object, str] =ContextKey(
default_value="Untitled",
description="Title of the active document.",
)
# 2. Bind the namespace to the app's context ------------------------------app=Application.get_or_create("my-app")
keys=AppKeys(app.context) # <-- the binding step that's easy to miss# 3. Typed reads and writes -----------------------------------------------assertkeys.selected_count==0# int (typed)keys.selected_count=3# OKkeys.is_dirty=True# OK# keys.selected_count = "three" # mypy/pyright would flag this# The namespace and the underlying context share storage:assertapp.context["selected_count"] ==3# 4. The payoff: build expressions from the *class* attributes ------------# AppKeys.selected_count is a ContextKey, which IS an Expr -- usable# directly in `enablement` / `when` clauses, no string parsing needed.has_selection=AppKeys.selected_count>0needs_save=AppKeys.is_dirty& (AppKeys.document_title!="Untitled")
asserthas_selection.eval({"selected_count": 3}) isTrueasserthas_selection.eval(app.context) isTrue# works on a live Context toodefdelete_selection() ->None:
print(f"deleting {keys.selected_count} items")
app.register_action(
Action(
id="my-app.delete",
title="Delete Selection",
callback=delete_selection,
enablement=has_selection, # ← typed ContextKey, not a string
)
)
if you don't want to use that
you can just stick with your TypedDict. and, if you want to retain static typing whenever you access the app.context property, you should just typed_ctx = cast(YourDict, app.context). (this is also not mutually exclusive with the ContextNamespace approach above).
you could also just pass a TypedDict as the context (with type ignore) and keep the reference
#284 (now merged) adds docs on contexts, expressions and ContextNamespace, including the typed ContextKey[A, T] pattern from the example above. so I'm going to close this one... if there's something those docs don't cover, let me know!
Activity
tlambert03 commentedon May 8, 2026
hey @MosGeo
there are some subtleties here, and lots of room for improvement, Ultimately though, I would just suggest using type ignore or casting
dict(my_dict)when you pass it into the Application, and move on with your life:TypedDictis not, technically speaking, aMutableMapping(since it has a fixed set of keys... yes, you can mutate the value of any of those keys, but because it doesn't accept new keys, it's not actually a MutableMapping).Contextobject (Context(your_object)). That doesn't copy the dictionary, it stores it directly. Arguably, we could just calldict(your_object), and then we could relax the type hint tocontext: Mapping(but, we still couldn't useTypedDictas a possible type ... it's not correct here). But you can also just passcontext=dict(your_object)yourself and that would also work.I definitely concede that the ContextNamespace thing isn't very intuitive. I'm working on docs in #284 ... but even as I write them I'm not super happy with the mental mode. However, to get you started, here's a rough example of how you would currently use
ContextNamespaceto get static typing of context values, and (the more important bit) use the keys directly in expressions (claude made this example, but I read it and agree):if you don't want to use that
TypedDict. and, if you want to retain static typing whenever you access theapp.contextproperty, you should justtyped_ctx = cast(YourDict, app.context). (this is also not mutually exclusive with theContextNamespaceapproach above).This works because Application adopts the dict directly (as we discussed earlier)
anyway, there's a number of options from simple to complex.
tlambert03 commentedon Nov 20, 2026
#284 (now merged) adds docs on contexts, expressions and
ContextNamespace, including the typedContextKey[A, T]pattern from the example above. so I'm going to close this one... if there's something those docs don't cover, let me know!