API Reference¶
watch¶
watch(obj, watcher, deepstate=False)
Watches obj for mutation.
Parameters¶
Name |
Type |
Description |
|---|---|---|
|
|
Object to watch. |
|
|
Callback called when a watched mutation occurs. |
|
|
When |
Returns¶
The watched object.
For built-in containers, the return value is a watched container initialized
with the contents of obj. For pydantic models, plain objects, and dataclasses,
the return value is the original object when observation succeeds. Objects
without a __dict__ are returned unchanged.
Callback¶
def watcher(obj, method, ref, call_args, call_kwargs):
...
Name |
Type |
Description |
|---|---|---|
|
|
Watched object receiving the mutation. For pydantic field assignment, this is the model field dictionary. |
|
|
Mutating method name, without leading or trailing double underscores. |
|
|
Watched entry point. For nested mutations with |
|
|
Positional arguments passed to the mutating method. |
|
|
Keyword arguments passed to the mutating method. |
The callback is called before the underlying mutation completes. Its return value is ignored. Exceptions raised by the callback propagate to the caller.
Supported Mutations¶
list¶
Mutation |
Reported |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dict¶
Mutation |
Reported |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
set¶
Mutation |
Reported |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pydantic.BaseModel¶
Field assignment is reported as setitem because pydantic stores field values
in the model field dictionary.
With deepstate=True, nested pydantic models and nested mutable containers are
watched when they are reachable from the watched model.
Plain Objects And Dataclasses¶
Attribute assignment is reported as setattr.
With deepstate=True, nested values present during installation are watched.
Objects without a __dict__ are returned unchanged.