Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

test(py): demonstrate structural `instance of` over Python objects

public

test(py): demonstrate structural `instance of` over Python objects

Add feel-py smoke tests for `value instance of context<…>`: the structural shape
check over a dict / dataclass (width subtyping — extra fields are fine), the two
documented caveats (the check materialises the instance `__dict__`, so a
`@property` is invisible to it even though the FEEL dot fires it; a `__slots__`
object has no `__dict__` and is not introspectable → false), the Null-field
nuance (`Null <: T`, so a present-but-None field conforms), nested/list generics,
and the built-in types. Bump the README suite count (now 41).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changesetc41106ff62d132b2d2c1c3b530e91ec91cf4aec3
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents4d82c9ea5a59
Children
Pushlog
by timo via ssh

modified README.md +1 −1

@@ -277,7 +277,7 @@
277277
 ```sh
278278
 pip install maturin
279279
 cd crates/feel-py && maturin develop      # builds + installs into the active venv
280
-pytest tests                              # 21 smoke tests
280
+pytest tests                              # 41 smoke tests
281281
 ```
282282
 
283283
 The context is a native Python value and the result is a native Python value.

modified crates/feel-py/tests/test_feel.py +116 −0

@@ -284,3 +284,119 @@
284284
     # A function-valued context binding takes precedence over a built-in of the
285285
     # same name (host override; ADR-23).
286286
     assert feel.evaluate("count(1, 2)", {"count": lambda a, b: a * b}) == 2
287
+
288
+
289
+# --- `instance of`: structural shape checks over Python objects (ADR-26) -- #
290
+#
291
+# FEEL is *structurally* typed (like TypeScript): `value instance of context<…>`
292
+# asserts the value has *at least* the named fields with conforming types — it does
293
+# NOT consult the Python class name (that would be `isinstance`, which is nominal and
294
+# non-portable). So `instance of` is a host-agnostic "duck-typed shape" assertion.
295
+
296
+def test_instance_of_builtin_types():
297
+    # Over native values, against the built-in types.
298
+    assert feel.evaluate('"foo" instance of string') is True
299
+    assert feel.evaluate("42 instance of number") is True
300
+    assert feel.evaluate("42 instance of string") is False
301
+    # A null is an instance only of `Null`; a type that can't be mapped -> null.
302
+    assert feel.evaluate("null instance of Null") is True
303
+    assert feel.evaluate("null instance of string") is False
304
+    assert feel.evaluate("42 instance of list") is None  # bare `list` needs a parameter
305
+
306
+
307
+def test_instance_of_checks_dict_shape():
308
+    # The common case: a JSON-like dict. Width subtyping — extra keys are fine.
309
+    order = {"id": 7, "customer": "Ada"}
310
+    assert feel.evaluate("o instance of context<customer: string>", {"o": order}) is True
311
+    assert feel.evaluate(
312
+        "o instance of context<id: number, customer: string>", {"o": order}
313
+    ) is True
314
+    assert feel.evaluate("o instance of context<customer: number>", {"o": order}) is False
315
+    assert feel.evaluate("o instance of context<missing: string>", {"o": order}) is False
316
+
317
+
318
+def test_instance_of_checks_object_shape():
319
+    # A class instance is materialised from its instance ``__dict__`` for the check,
320
+    # so a plain dataclass works exactly like the dict above.
321
+    from dataclasses import dataclass
322
+
323
+    @dataclass
324
+    class Person:
325
+        name: str
326
+        age: int
327
+
328
+    p = Person("Ada", 36)
329
+    assert feel.evaluate("p instance of context<name: string>", {"p": p}) is True
330
+    assert feel.evaluate(
331
+        "p instance of context<name: string, age: number>", {"p": p}
332
+    ) is True
333
+    assert feel.evaluate("p instance of context<age: number>", {"p": p}) is True
334
+    assert feel.evaluate("p instance of context<name: number>", {"p": p}) is False
335
+
336
+
337
+def test_instance_of_sees_instance_data_not_properties():
338
+    # CAVEAT: the shape check materialises the instance ``__dict__`` (real data
339
+    # attributes). A computed ``@property`` is *not* part of that view — even though
340
+    # the FEEL dot (``r.doubled``) *does* fire it. The two paths differ on purpose:
341
+    # bulk materialisation for `instance of` vs. lazy attribute access for the dot.
342
+    class Row:
343
+        def __init__(self):
344
+            self.total = 10
345
+
346
+        @property
347
+        def doubled(self):
348
+            return self.total * 2
349
+
350
+    r = Row()
351
+    assert feel.evaluate("r.doubled", {"r": r}) == 20  # the dot fires the property
352
+    assert feel.evaluate("r instance of context<total: number>", {"r": r}) is True
353
+    # The property is invisible to the structural check:
354
+    assert feel.evaluate("r instance of context<doubled: number>", {"r": r}) is False
355
+
356
+
357
+def test_instance_of_slots_object_is_not_introspectable():
358
+    # CAVEAT: a ``__slots__`` object has no ``__dict__``, so it cannot be materialised
359
+    # into a context — the predicate is False (not an error). Lazy dot access still
360
+    # works.
361
+    class Point:
362
+        __slots__ = ("x", "y")
363
+
364
+        def __init__(self, x, y):
365
+            self.x = x
366
+            self.y = y
367
+
368
+    pt = Point(1, 2)
369
+    assert feel.evaluate("pt.x", {"pt": pt}) == 1  # the dot still works
370
+    assert feel.evaluate("pt instance of context<x: number>", {"pt": pt}) is False
371
+
372
+
373
+def test_instance_of_field_may_be_null():
374
+    # A *missing* field fails the check, but a field that is present and ``None``
375
+    # conforms to any type — in the FEEL lattice ``Null`` is a subtype of every type.
376
+    # So this asserts "has a ``name`` slot (possibly null) that, when non-null, is a
377
+    # string", not "has a non-null string name".
378
+    class Person:
379
+        def __init__(self, name):
380
+            self.name = name
381
+
382
+    assert feel.evaluate("p instance of context<name: string>", {"p": Person(None)}) is True
383
+    assert feel.evaluate("p instance of context<name: string>", {"p": Person("Ada")}) is True
384
+
385
+
386
+def test_instance_of_nested_and_list_shapes():
387
+    # Generics compose: ``list<…>`` over elements and nested ``context<…>``.
388
+    people = [{"name": "Ada"}, {"name": "Grace"}]
389
+    assert feel.evaluate(
390
+        "ps instance of list<context<name: string>>", {"ps": people}
391
+    ) is True
392
+    # One element of the wrong shape fails the whole list.
393
+    assert feel.evaluate(
394
+        "ps instance of list<context<name: number>>", {"ps": people}
395
+    ) is False
396
+    nested = {"owner": {"name": "Ada"}}
397
+    assert feel.evaluate(
398
+        "c instance of context<owner: context<name: string>>", {"c": nested}
399
+    ) is True
400
+    assert feel.evaluate(
401
+        "c instance of context<owner: context<name: number>>", {"c": nested}
402
+    ) is False