Skip to content

Commit

Permalink
style: edtlib: Use a better type Annotations
Browse files Browse the repository at this point in the history
Use built-in types for annotations instead
of types from the typing module.

Signed-off-by: James Roy <[email protected]>
  • Loading branch information
rruuaanng authored and kartben committed Jan 8, 2025
1 parent 307eca4 commit c99a61a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 111 deletions.
2 changes: 0 additions & 2 deletions .ruff-excludes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@
"E701", # https://docs.astral.sh/ruff/rules/multiple-statements-on-one-line-colon
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM201", # https://docs.astral.sh/ruff/rules/negate-equal-op
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
"UP032", # https://docs.astral.sh/ruff/rules/f-string
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
Expand All @@ -452,7 +451,6 @@
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
"SIM118", # https://docs.astral.sh/ruff/rules/in-dict-keys
"UP006", # https://docs.astral.sh/ruff/rules/non-pep585-annotation
"UP007", # https://docs.astral.sh/ruff/rules/non-pep604-annotation
"UP015", # https://docs.astral.sh/ruff/rules/redundant-open-modes
"UP032", # https://docs.astral.sh/ruff/rules/f-string
Expand Down
42 changes: 21 additions & 21 deletions scripts/dts/python-devicetree/src/devicetree/dtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import string
import sys
import textwrap
from typing import (Any, Dict, Iterable, List,
from typing import (Any, Iterable,
NamedTuple, NoReturn, Optional,
Set, Tuple, TYPE_CHECKING, Union)
TYPE_CHECKING, Union)

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

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

self._name = name
self.props: Dict[str, 'Property'] = {}
self.nodes: Dict[str, 'Node'] = {}
self.labels: List[str] = []
self.props: dict[str, Property] = {}
self.nodes: dict[str, Node] = {}
self.labels: list[str] = []
self.parent = parent
self.dt = dt

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

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

self._label_offset_lst: List[Tuple[str, int]] = []
self._label_offset_lst: list[tuple[str, int]] = []

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

@property
def type(self) -> Type:
Expand Down Expand Up @@ -388,7 +388,7 @@ def to_num(self, signed=False) -> int:

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

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

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

def to_strings(self) -> List[str]:
def to_strings(self) -> list[str]:
"""
Returns the value of the property as a list of strings.
Expand Down Expand Up @@ -498,7 +498,7 @@ def to_node(self) -> Node:

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

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

self._root: Optional[Node] = None
self.alias2node: Dict[str, Node] = {}
self.label2node: Dict[str, Node] = {}
self.label2prop: Dict[str, Property] = {}
self.label2prop_offset: Dict[str, Tuple[Property, int]] = {}
self.phandle2node: Dict[int, Node] = {}
self.memreserves: List[Tuple[Set[str], int, int]] = []
self.alias2node: dict[str, Node] = {}
self.label2node: dict[str, Node] = {}
self.label2prop: dict[str, Property] = {}
self.label2prop_offset: dict[str, tuple[Property, int]] = {}
self.phandle2node: dict[int, Node] = {}
self.memreserves: list[tuple[set[str], int, int]] = []
self.filename = filename

self._force = force

if filename is not None:
self._parse_file(filename, include_path)
else:
self._include_path: List[str] = []
self._include_path: list[str] = []

@property
def root(self) -> Node:
Expand Down Expand Up @@ -1027,7 +1027,7 @@ def _parse_file(self, filename: str, include_path: Iterable[str]):
self._file_contents = f.read()

self._tok_i = self._tok_end_i = 0
self._filestack: List[_FileStackElt] = []
self._filestack: list[_FileStackElt] = []

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

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

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

0 comments on commit c99a61a

Please sign in to comment.