Skip to content

Honor fallback_value on Observable - #4189

Merged
quaquel merged 2 commits into
mesa:mainfrom
quaquel:fix/observable-fallback-value
Nov 25, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
quaquel:fix/observable-fallback-value

Conversation

@quaquel

@quaquel quaquel commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Observable accepted fallback_value but never used it. Reading before the first assignment raised AttributeError, and the first CHANGED signal reported old=None.

Observable now returns its fallback value when read before assignment (also from computed properties) and reports it as the old value of the first assignment, so assigning a value equal to the fallback emits nothing. Without a fallback value, the behavior is unchanged: reading before assignment raises AttributeError and the first old value is None. A sentinel distinguishes "no fallback" from a fallback of None.

BaseObservable.__set__ had old and new swapped and never stored the value. All subclasses override it, so it now raises NotImplementedError.

Adds tests for both cases with and without a fallback value, and for BaseObservable.set.

Observable accepted fallback_value but never used it: reading before the
first assignment raised AttributeError, and the first CHANGED signal
reported old=None.

Observable now returns its fallback value when read before assignment
(also from computed properties) and reports it as the old value of the
first assignment, so assigning a value equal to the fallback emits
nothing. Without a fallback value the behavior is unchanged: reading
before assignment raises AttributeError and the first old value is None.
A sentinel distinguishes "no fallback" from a fallback of None.

BaseObservable.__set__ had old and new swapped and never stored the
value. All subclasses override it, so it now raises NotImplementedError.

Adds tests for both cases with and without a fallback value, and for
BaseObservable.__set__.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
@quaquel quaquel added the bug Release notes label label Nov 23, 2026
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 23, 2026
@mesa-triage-bot

Copy link
Copy Markdown

Thanks for the fallback regression tests, @quaquel. The diff also replaces BaseObservable.__set__ with NotImplementedError; both existing subclasses override that method, so this change isn't needed for the fallback fix.

Please split that setter change and its test into a separate PR, or drop them here, so this PR addresses one problem.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@quaquel

quaquel commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

I indeed collapse 2 fixes into 1 PR, but they touch the same part of the code and are at least somewhat related, so I favor proceeding with this as is. Also, with close to 50 PRs open continuously, I am hesitant to create even more noise.

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix and tests look good, and the tram example (Observable(fallback_value=...)) finally gets what it asks for. Tests pass locally.

One thing to keep in mind: #4190 documents the current behaviour in the Observable docstring ("reading an Observable before it has been assigned raises AttributeError", "for the first assignment, old is None"). Whichever of the two lands second should update those lines to mention fallback_value.

@quaquel
quaquel merged commit debf4dc into mesa:main Nov 25, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants