@@ -33,33 +33,98 @@ def test_controller_nesting():
3333 controller .register_sub_controller ("c" , sub_controller )
3434
3535
36- def test_attribute_parsing ():
37- runtime_attribute = AttrR (Int ())
36+ class SomeSubController (SubController ):
37+ def __init__ (self ):
38+ self ._root_attribute = AttrR (Int ())
39+ super ().__init__ ()
3840
39- class SomeController (Controller ):
40- annotated_attr = AttrR (Int ())
41- annotated_attr_not_defined_in_init : AttrR [int ]
42- equal_attr = AttrR (Int ())
43- annotated_and_equal_attr : AttrR [int ] = AttrR (Int ())
41+ sub_attribute = AttrR (Int ())
4442
45- def get_attributes (self ) -> dict [str , Attribute ]:
46- return {"get_attributes_attr" : runtime_attribute }
43+ @property
44+ def root_attribute (self ):
45+ return self ._root_attribute
4746
48- def __init__ (self ):
49- self .annotated_attr = AttrR (Int ())
50- super ().__init__ ()
5147
52- controller = SomeController ()
53- mapping = next (_walk_mappings (controller ))
54- assert mapping .attributes == {
55- "get_attributes_attr" : runtime_attribute ,
48+ class SomeController (Controller ):
49+ annotated_attr = AttrR (Int ())
50+ annotated_attr_not_defined_in_init : AttrR [int ]
51+ equal_attr = AttrR (Int ())
52+ annotated_and_equal_attr : AttrR [int ] = AttrR (Int ())
53+
54+ def __init__ (self , sub_controller : SubController ):
55+ self .get_attributes_attribute = AttrR (Int ())
56+ self .annotated_attr = AttrR (Int ())
57+ self .attr_not_walked = AttrR (Int ())
58+
59+ super ().__init__ ()
60+
61+ self .register_sub_controller ("sub_controller" , sub_controller )
62+
63+ def get_attributes (self ) -> dict [str , Attribute ]:
64+ return {
65+ "get_attributes_attr" : self .get_attributes_attribute ,
66+ "equal_attr" : self .equal_attr ,
67+ }
68+
69+
70+ def test_attribute_parsing ():
71+ sub_controller = SomeSubController ()
72+ controller = SomeController (sub_controller )
73+
74+ mapping_walk = _walk_mappings (controller )
75+
76+ controller_mapping = next (mapping_walk )
77+ assert controller_mapping .attributes == {
78+ "get_attributes_attr" : controller .get_attributes_attribute ,
5679 "annotated_attr" : controller .annotated_attr ,
5780 "equal_attr" : controller .equal_attr ,
5881 "annotated_and_equal_attr" : controller .annotated_and_equal_attr ,
82+ "sub_controller" : sub_controller .root_attribute ,
5983 }
6084
6185 assert SomeController .equal_attr is not controller .equal_attr
6286 assert (
6387 SomeController .annotated_and_equal_attr
6488 is not controller .annotated_and_equal_attr
6589 )
90+
91+ sub_controller_mapping = next (mapping_walk )
92+ assert sub_controller_mapping .attributes == {
93+ "sub_attribute" : sub_controller .sub_attribute ,
94+ }
95+
96+
97+ def test_root_attribute ():
98+ class FailingController (SomeController ):
99+ def get_attributes (self ) -> dict [str , Attribute ]:
100+ return {"sub_controller" : self .get_attributes_attribute }
101+
102+ with pytest .raises (
103+ ValueError ,
104+ match = (
105+ "sub_controller `sub_controller` has a `root_attribute` already "
106+ "defined defined in parent controller sub_controller"
107+ ),
108+ ):
109+ next (_walk_mappings (FailingController (SomeSubController ())))
110+
111+
112+ def test_attribute_in_both_class_and_get_attributes ():
113+ class FailingController (Controller ):
114+ duplicate_attribute = AttrR (Int ())
115+
116+ def __init__ (self ):
117+ super ().__init__ ()
118+
119+ def get_attributes (self ) -> dict [str , Attribute ]:
120+ return {"duplicate_attribute" : AttrR (Int ())}
121+
122+ with pytest .raises (
123+ ValueError ,
124+ match = (
125+ "`FailingController` has conflicting attributes between those passed "
126+ "in `get_attributes` and those obtained from the class definition: "
127+ "{'duplicate_attribute'}"
128+ ),
129+ ):
130+ next (_walk_mappings (FailingController ()))
0 commit comments