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 .response_id import ResponseId
from .common import * from .common import *
ALLOWED_PARENTS: T.List[T.Tuple[str, str]] = [
("Gtk", "Buildable"),
("Gio", "ListStore")
]
class Child(AstNode): class Child(AstNode):
grammar = [ grammar = [
@ -37,6 +41,22 @@ class Child(AstNode):
Object, 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 @cached_property
def response_id(self) -> T.Optional[ResponseId]: def response_id(self) -> T.Optional[ResponseId]:
"""Get action widget's response ID. """Get action widget's response ID.

View file

@ -0,0 +1,5 @@
using Gtk 4.0;
ListItem {
Label {}
}

View file

@ -0,0 +1 @@
4,3,8,Gtk.ListItem doesn't have children

View file

@ -175,6 +175,7 @@ class TestSamples(unittest.TestCase):
self.assert_sample_error("action_widget_response_dne") self.assert_sample_error("action_widget_response_dne")
self.assert_sample_error("action_widget_negative_response") self.assert_sample_error("action_widget_negative_response")
self.assert_sample_error("bitfield_member_dne") self.assert_sample_error("bitfield_member_dne")
self.assert_sample_error("children")
self.assert_sample_error("class_assign") self.assert_sample_error("class_assign")
self.assert_sample_error("class_dne") self.assert_sample_error("class_dne")
self.assert_sample_error("consecutive_unexpected_tokens") self.assert_sample_error("consecutive_unexpected_tokens")