Skip to content

Commit 98a5397

Browse files
author
James Souter
committed
validate hinted attributes on subcontrollers
1 parent 48c688d commit 98a5397

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/fastcs/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def validate_hinted_attributes(controller: BaseController):
3737
For each type-hinted attribute, validate that a corresponding instance exists in the
3838
controller with the correct access mode and datatype.
3939
"""
40-
40+
for subcontroller in controller.sub_controllers.values():
41+
validate_hinted_attributes(subcontroller)
4142
hints = get_type_hints(type(controller))
4243
alias_hints = {k: v for k, v in hints.items() if isinstance(v, _GenericAlias)}
4344
for name, hint in alias_hints.items():

tests/test_util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic import ValidationError
77

88
from fastcs.attributes import AttrR, AttrRW
9+
from fastcs.backend import Backend
910
from fastcs.controller import Controller
1011
from fastcs.datatypes import Bool, Enum, Float, Int, String
1112
from fastcs.util import (
@@ -136,3 +137,26 @@ class ControllerWrongEnumClass(Controller):
136137
"'hinted_enum' does not match defined datatype. "
137138
"Expected 'MyEnum', got 'MyEnum2'."
138139
)
140+
141+
142+
def test_hinted_attributes_verified_on_subcontrollers():
143+
loop = asyncio.get_event_loop()
144+
145+
class ControllerWithWrongType(SubController):
146+
hinted_missing: AttrR[int]
147+
148+
async def connect(self):
149+
return
150+
151+
class TopController(Controller):
152+
async def initialise(self):
153+
subcontroller = ControllerWithWrongType()
154+
self.register_sub_controller("MySubController", subcontroller)
155+
156+
with pytest.raises(RuntimeError) as excinfo:
157+
Backend(TopController(), loop)
158+
159+
assert str(excinfo.value) == (
160+
"Controller `ControllerWithWrongType` failed to introspect hinted attribute "
161+
"`hinted_missing` during initialisation"
162+
)

0 commit comments

Comments
 (0)