Skip to content

Return descriptor on class-level Observable access - #3733

Merged
quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:fix-observable-class-access
Jun 8, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:fix-observable-class-access

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Closes #3732

What

Accessing an Observable attribute on the class (e.g. MyAgent.value) raised
AttributeError instead of returning the descriptor. BaseObservable.__get__
called getattr(instance, self.private_name) unconditionally, so class-level
access (where Python passes instance=None) became getattr(None, "_value").

This also made hasattr(MyAgent, "value") return False, since hasattr
swallows the AttributeError, breaking normal class-level introspection.

Fix

Added the standard descriptor-protocol guard at the top of
BaseObservable.__get__:

if instance is None:
    return self

This is the same convention property follows. Observable inherits this
__get__, so the single guard covers both classes. Instance access is unchanged.

Test

Added test_observable_class_level_access: class-level access returns the
Observable descriptor, hasattr(cls, name) is True, and instance access
still returns the value.

mesa_signals tests pass under -Werror; ruff clean.

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.8% [-1.2%, -0.5%] 🔵 -0.3% [-0.8%, +0.0%]
BoltzmannWealth large 🔵 -0.1% [-0.5%, +0.4%] 🔵 -1.9% [-2.8%, -1.2%]
Schelling small 🔵 -2.5% [-3.1%, -1.9%] 🔵 -0.6% [-1.4%, +0.2%]
Schelling large 🔵 +0.1% [-0.7%, +0.7%] 🔵 -3.0% [-5.6%, -0.5%]
WolfSheep small 🔵 +0.1% [-0.2%, +0.4%] 🔵 -0.8% [-5.6%, +3.9%]
WolfSheep large 🔵 +0.7% [+0.3%, +1.2%] 🔵 -1.6% [-2.0%, -1.2%]
SugarscapeG1mt small 🔵 +0.9% [+0.8%, +1.0%] 🔵 -0.7% [-2.3%, +0.8%]
SugarscapeG1mt large 🔵 +0.2% [-0.2%, +0.5%] 🔵 +1.4% [-0.9%, +3.5%]
BoidFlockers small 🔵 +0.6% [+0.3%, +1.0%] 🔵 -0.4% [-0.7%, -0.0%]
BoidFlockers large 🔵 +0.2% [-0.1%, +0.5%] 🔵 +0.2% [-0.0%, +0.4%]

@quaquel quaquel added the bug Release notes label label Jun 8, 2026
@quaquel
quaquel merged commit 293fe9c into mesa:main Jun 8, 2026
12 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Observable raises AttributeError on class-level access (descriptor protocol)

2 participants