Skip to content

Commit c99a61a

Browse files
rruuaanngkartben
authored andcommitted
style: edtlib: Use a better type Annotations
Use built-in types for annotations instead of types from the typing module. Signed-off-by: James Roy <rruuaanng@outlook.com>
1 parent 307eca4 commit c99a61a

File tree

3 files changed

+109
-111
lines changed

3 files changed

+109
-111
lines changed

.ruff-excludes.toml

-2
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@
439439
"E701", # https://docs.astral.sh/ruff/rules/multiple-statements-on-one-line-colon
440440
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
441441
"SIM201", # https://docs.astral.sh/ruff/rules/negate-equal-op
442-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
443442
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
444443
"UP032", # https://docs.astral.sh/ruff/rules/f-string
445444
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
@@ -452,7 +451,6 @@
452451
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
453452
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
454453
"SIM118", # https://docs.astral.sh/ruff/rules/in-dict-keys
455-
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
456454
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
457455
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
458456
"UP032", # https://docs.astral.sh/ruff/rules/f-string

scripts/dts/python-devicetree/src/devicetree/dtlib.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import string
2121
import sys
2222
import textwrap
23-
from typing import (Any, Dict, Iterable, List,
23+
from typing import (Any, Iterable,
2424
NamedTuple, NoReturn, Optional,
25-
Set, Tuple, TYPE_CHECKING, Union)
25+
TYPE_CHECKING, Union)
2626

2727
# NOTE: tests/test_dtlib.py is the test suite for this library.
2828

@@ -92,9 +92,9 @@ def __init__(self, name: str, parent: Optional['Node'], dt: 'DT'):
9292
# Remember to update DT.__deepcopy__() if you change this.
9393

9494
self._name = name
95-
self.props: Dict[str, 'Property'] = {}
96-
self.nodes: Dict[str, 'Node'] = {}
97-
self.labels: List[str] = []
95+
self.props: dict[str, Property] = {}
96+
self.nodes: dict[str, Node] = {}
97+
self.labels: list[str] = []
9898
self.parent = parent
9999
self.dt = dt
100100

@@ -309,21 +309,21 @@ def __init__(self, node: Node, name: str):
309309

310310
self.name = name
311311
self.value = b""
312-
self.labels: List[str] = []
312+
self.labels: list[str] = []
313313
# We have to wait to set this until later, when we've got
314314
# the entire tree.
315-
self.offset_labels: Dict[str, int] = {}
315+
self.offset_labels: dict[str, int] = {}
316316
self.node: Node = node
317317

318-
self._label_offset_lst: List[Tuple[str, int]] = []
318+
self._label_offset_lst: list[tuple[str, int]] = []
319319

320320
# A list of [offset, label, type] lists (sorted by offset),
321321
# giving the locations of references within the value. 'type'
322322
# is either _MarkerType.PATH, for a node path reference,
323323
# _MarkerType.PHANDLE, for a phandle reference, or
324324
# _MarkerType.LABEL, for a label on/within data. Node paths
325325
# and phandles need to be patched in after parsing.
326-
self._markers: List[List] = []
326+
self._markers: list[list] = []
327327

328328
@property
329329
def type(self) -> Type:
@@ -388,7 +388,7 @@ def to_num(self, signed=False) -> int:
388388

389389
return int.from_bytes(self.value, "big", signed=signed)
390390

391-
def to_nums(self, signed=False) -> List[int]:
391+
def to_nums(self, signed=False) -> list[int]:
392392
"""
393393
Returns the value of the property as a list of numbers.
394394
@@ -455,7 +455,7 @@ def to_string(self) -> str:
455455

456456
return ret # The separate 'return' appeases the type checker.
457457

458-
def to_strings(self) -> List[str]:
458+
def to_strings(self) -> list[str]:
459459
"""
460460
Returns the value of the property as a list of strings.
461461
@@ -498,7 +498,7 @@ def to_node(self) -> Node:
498498

499499
return self.node.dt.phandle2node[int.from_bytes(self.value, "big")]
500500

501-
def to_nodes(self) -> List[Node]:
501+
def to_nodes(self) -> list[Node]:
502502
"""
503503
Returns a list with the Nodes the phandles in the property point to.
504504
@@ -761,20 +761,20 @@ def __init__(self, filename: Optional[str], include_path: Iterable[str] = (),
761761
# Remember to update __deepcopy__() if you change this.
762762

763763
self._root: Optional[Node] = None
764-
self.alias2node: Dict[str, Node] = {}
765-
self.label2node: Dict[str, Node] = {}
766-
self.label2prop: Dict[str, Property] = {}
767-
self.label2prop_offset: Dict[str, Tuple[Property, int]] = {}
768-
self.phandle2node: Dict[int, Node] = {}
769-
self.memreserves: List[Tuple[Set[str], int, int]] = []
764+
self.alias2node: dict[str, Node] = {}
765+
self.label2node: dict[str, Node] = {}
766+
self.label2prop: dict[str, Property] = {}
767+
self.label2prop_offset: dict[str, tuple[Property, int]] = {}
768+
self.phandle2node: dict[int, Node] = {}
769+
self.memreserves: list[tuple[set[str], int, int]] = []
770770
self.filename = filename
771771

772772
self._force = force
773773

774774
if filename is not None:
775775
self._parse_file(filename, include_path)
776776
else:
777-
self._include_path: List[str] = []
777+
self._include_path: list[str] = []
778778

779779
@property
780780
def root(self) -> Node:
@@ -1027,7 +1027,7 @@ def _parse_file(self, filename: str, include_path: Iterable[str]):
10271027
self._file_contents = f.read()
10281028

10291029
self._tok_i = self._tok_end_i = 0
1030-
self._filestack: List[_FileStackElt] = []
1030+
self._filestack: list[_FileStackElt] = []
10311031

10321032
self._lexer_state: int = _DEFAULT
10331033
self._saved_token: Optional[_Token] = None
@@ -2027,7 +2027,7 @@ def to_num(data: bytes, length: Optional[int] = None,
20272027

20282028
return int.from_bytes(data, "big", signed=signed)
20292029

2030-
def to_nums(data: bytes, length: int = 4, signed: bool = False) -> List[int]:
2030+
def to_nums(data: bytes, length: int = 4, signed: bool = False) -> list[int]:
20312031
"""
20322032
Like Property.to_nums(), but takes an arbitrary 'bytes' array. The values
20332033
are assumed to be in big-endian format, which is standard in devicetree.

0 commit comments

Comments
 (0)