Skip to content

Commit d414db0

Browse files
Fix compatibility with pygfx (#130)
* Fix compatibility with pygfx This change prevents reactivity on custom dict subclasses and instead will check that only real dicts are converted into reactive proxies. The problem is that pygfx's geometry is a dict subclass and turning it into a reactive proxy will stop it from being recognized as a Geometry type, so pygfx will not accept it when you try to set the geometry attribute on a Mesh. I could check for pygfx specifically, but it seems to me that more problems are to be expected in the future if observ tries to proxy custom dict subclasses. I'm thinking to update list and set proxy as well. * Fix linting by formatting * Remove uv.lock
1 parent 746141b commit d414db0

6 files changed

Lines changed: 34 additions & 1163 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist
66
.benchmarks
77
.venv
88
.pytest_cache
9+
uv.lock

observ/dict_proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def readonly_dict_proxy_init(self, target, shallow=False, **kwargs):
7777

7878

7979
def type_test(target):
80-
return isinstance(target, dict)
80+
return type(target) is dict
8181

8282

8383
TYPE_LOOKUP[type_test] = (DictProxy, ReadonlyDictProxy)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ qt = [
3131
"pytest-qt",
3232
"pytest-xvfb",
3333
]
34+
pygfx = ["pygfx"]
3435
numpy = ["numpy"]
3536

3637
[tool.uv]

tests/test_proxy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ def test_tuple_equality():
256256
assert isinstance(raw, tuple)
257257
assert p is not raw
258258
assert p == raw
259+
260+
261+
def test_dict_subclass_not_wrapped():
262+
class Custom(dict):
263+
pass
264+
265+
raw = Custom(foo="bar")
266+
p = proxy(raw)
267+
assert not isinstance(p, Proxy)
268+
assert isinstance(raw, dict)
269+
assert p is raw

tests/test_pygfx.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from observ import reactive
4+
5+
try:
6+
import pygfx as gfx
7+
8+
has_pygfx = True
9+
except ImportError:
10+
has_pygfx = False
11+
pygfx_missing_reason = "Pygfx is not installed"
12+
13+
14+
@pytest.mark.skipif(not has_pygfx, reason=pygfx_missing_reason)
15+
def test_pygfx_geometry_not_wrapped():
16+
geometry = gfx.sphere_geometry()
17+
18+
wrapped = reactive(geometry)
19+
# Assert strict equality to test that it is not wrapped
20+
assert wrapped is geometry

0 commit comments

Comments
 (0)