Validate that an object can have children

Fixes #32.
This commit is contained in:
James Westman 2022-07-09 16:40:02 -05:00
parent 0a0389b1f8
commit 664fa2250b
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
4 changed files with 27 additions and 0 deletions

View file

@ -24,6 +24,10 @@ from .gobject_object import Object
from .response_id import ResponseId
from .common import *
ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [
("Gtk", "Buildable"),
("Gio", "ListStore")
]
class Child(AstNode):
grammar = [
@ -37,6 +41,22 @@ class Child(AstNode):
Object,
]
@validate()
def parent_can_have_child(self):
if gir_class := self.parent.gir_class:
for namespace, name in ALLOWED_PARENTS:
parent_type = self.root.gir.get_type(name, namespace)
if gir_class.assignable_to(parent_type):
break
else:
hints=["only Gio.ListStore or Gtk.Buildable implementors can have children"]
if "child" in gir_class.properties:
hints.append("did you mean to assign this object to the 'child' property?")
raise CompileError(
f"{gir_class.full_name} doesn't have children",
hints=hints,
)
@cached_property
def response_id(self) -> T.Optional[ResponseId]:
"""Get action widget's response ID.