Skip to content

Commit

Permalink
edtlib: make PinCtrl a type-annotated dataclass
Browse files Browse the repository at this point in the history
Converting this to a dataclass will make it easier to type annotate.
Adding type annotations is incremental progress towards type checking
the entire module.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
  • Loading branch information
mbolivar-nordic committed Apr 17, 2023
1 parent 83b6db2 commit cae8b65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
27 changes: 11 additions & 16 deletions scripts/dts/python-devicetree/src/devicetree/edtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ class ControllerAndData:
basename: Optional[str]


@dataclass
class PinCtrl:
"""
Represents a pin control configuration for a set of pins on a device,
Expand All @@ -834,21 +835,15 @@ class PinCtrl:
pinctrl-0 = <&state_1 &state_2>;
"""

node: 'Node'
name: Optional[str]
conf_nodes: List['Node']

@property
def name_as_token(self):
"See the class docstring"
return str_as_token(self.name) if self.name is not None else None

def __repr__(self):
fields = []

if self.name is not None:
fields.append("name: " + self.name)

fields.append("configuration nodes: " + str(self.conf_nodes))

return "<PinCtrl, {}>".format(", ".join(fields))


class Node:
"""
Expand Down Expand Up @@ -1625,12 +1620,12 @@ def _init_pinctrls(self):

self.pinctrls = []
for prop in pinctrl_props:
pinctrl = PinCtrl()
pinctrl.node = self
pinctrl.conf_nodes = [
self.edt._node2enode[node] for node in prop.to_nodes()
]
self.pinctrls.append(pinctrl)
# We'll fix up the names below.
self.pinctrls.append(PinCtrl(
node=self,
name=None,
conf_nodes=[self.edt._node2enode[node]
for node in prop.to_nodes()]))

_add_names(node, "pinctrl", self.pinctrls)

Expand Down
10 changes: 8 additions & 2 deletions scripts/dts/python-devicetree/tests/test_edtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,14 @@ def test_pinctrl():
with from_here():
edt = edtlib.EDT("test.dts", ["test-bindings"])

assert str(edt.get_node("/pinctrl/dev").pinctrls) == \
"[<PinCtrl, name: zero, configuration nodes: []>, <PinCtrl, name: one, configuration nodes: [<Node /pinctrl/pincontroller/state-1 in 'test.dts', no binding>]>, <PinCtrl, name: two, configuration nodes: [<Node /pinctrl/pincontroller/state-1 in 'test.dts', no binding>, <Node /pinctrl/pincontroller/state-2 in 'test.dts', no binding>]>]"
node = edt.get_node("/pinctrl/dev")
state_1 = edt.get_node('/pinctrl/pincontroller/state-1')
state_2 = edt.get_node('/pinctrl/pincontroller/state-2')
assert node.pinctrls == [
edtlib.PinCtrl(node=node, name='zero', conf_nodes=[]),
edtlib.PinCtrl(node=node, name='one', conf_nodes=[state_1]),
edtlib.PinCtrl(node=node, name='two', conf_nodes=[state_1, state_2])
]

def test_hierarchy():
'''Test Node.parent and Node.children'''
Expand Down

0 comments on commit cae8b65

Please sign in to comment.