Skip to content

Commit 05aa6c0

Browse files
committed
Fix node internal properties with extension methods
1 parent 57e1d02 commit 05aa6c0

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

pylasu/support.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
from pylasu.model.model import Concept
2+
3+
4+
def register_internal_property(cls, name):
5+
cls.__internal_properties__.append(name)
6+
for s in cls.__subclasses__():
7+
register_internal_property(s, name)
8+
9+
110
def extension_method(cls):
211
"""Installs the decorated function as an extension method on cls.
312
See https://mail.python.org/pipermail/python-dev/2008-January/076194.html"""
@@ -6,5 +15,7 @@ def decorator(func):
615
if name in cls.__dict__:
716
raise Exception(f"{cls} already has a member called {name}")
817
setattr(cls, name, func)
18+
if isinstance(cls, Concept):
19+
register_internal_property(cls, name)
920
return func
1021
return decorator

tests/model/test_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pylasu.model import Node, Position, Point
66
from pylasu.model.reflection import Multiplicity
77
from pylasu.model.naming import ReferenceByName, Named, Scope, Symbol
8+
from pylasu.support import extension_method
89

910

1011
@dataclasses.dataclass
@@ -144,6 +145,10 @@ def test_scope_case_insensitive_lookup(self):
144145
self.assertIsInstance(result, Symbol)
145146

146147
def test_node_properties_meta(self):
148+
@extension_method(Node)
149+
def frob_node(_: Node):
150+
pass
151+
147152
pds = [pd for pd in sorted(SomeNode.node_properties, key=lambda x: x.name)]
148153
self.assertEqual(5, len(pds), f"{pds} should be 5")
149154
self.assertEqual("bar", pds[0].name)

0 commit comments

Comments
 (0)