I ended up creating a Model metaclass called ModelMeta that registers the typed attributes.
See http://github.com/espeed/bulbs/blob/master/bulbs/model.py
In this case, the typed attributes are graph-database "properties", which are all subclasses of the Property class.
See https://github.com/espeed/bulbs/blob/master/bulbs/property.py
Here's an example Model declaration:
# people.py
from bulbs.model import Node, Relationship
from bulbs.property import String, Integer, DateTime
from bulbs.utils import current_datetime
class Person(Node):
element_type = "person"
name = String(nullable=False)
age = Integer()
class Knows(Relationship):
label = "knows"
created = DateTime(default=current_datetime, nullable=False)
Example usage:
>>> from people import Person
>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
# Add a "people" proxy to the Graph object for the Person model:
>>> g.add_proxy("people", Person)
# Use it to create a Person node, which also saves it in the database:
>>> james = g.people.create(name="James")
>>> james.eid
3
>>> james.name
'James'
# Get the node (again) from the database by its element ID:
>>> james = g.people.get(james.eid)
# Update the node and save it in the database:
>>> james.age = 34
>>> james.save()
# Lookup people using the Person model's primary index:
>>> nodes = g.people.index.lookup(name="James")
See...