Skip to content

Commit

Permalink
style: edtlib: Use a better format string
Browse files Browse the repository at this point in the history
Use f-strings as recommended by PEP-8
instead of the .format() method.

Signed-off-by: James Roy <[email protected]>
  • Loading branch information
rruuaanng committed Jan 8, 2025
1 parent 52fd016 commit 25187c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
2 changes: 0 additions & 2 deletions .ruff-excludes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
"SIM201", # https://docs.astral.sh/ruff/rules/negate-equal-op
"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
"UP037", # https://docs.astral.sh/ruff/rules/quoted-annotation
]
Expand All @@ -453,7 +452,6 @@
"SIM118", # https://docs.astral.sh/ruff/rules/in-dict-keys
"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
"UP035", # https://docs.astral.sh/ruff/rules/deprecated-import
"UP037", # https://docs.astral.sh/ruff/rules/quoted-annotation
]
Expand Down
57 changes: 25 additions & 32 deletions scripts/dts/python-devicetree/src/devicetree/dtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,9 @@ def to_num(self, signed=False) -> int:
unsigned.
"""
if self.type is not Type.NUM:
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = < (number) >;', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with "
f"'{self.name} = < (number) >;', not '{self}'")

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

Expand All @@ -402,10 +401,9 @@ def to_nums(self, signed=False) -> list[int]:
unsigned.
"""
if self.type not in (Type.NUM, Type.NUMS):
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = < (number) (number) ... >;', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with "
f"'{self.name} = < (number) (number) ... >;', not '{self}'")

return [int.from_bytes(self.value[i:i + 4], "big", signed=signed)
for i in range(0, len(self.value), 4)]
Expand All @@ -421,10 +419,9 @@ def to_bytes(self) -> bytes:
foo = [ 01 ... ];
"""
if self.type is not Type.BYTES:
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = [ (byte) (byte) ... ];', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} "
f"in {self.node.dt.filename} to be assigned with "
f"'{self.name} = [ (byte) (byte) ... ];', not '{self}'")

return self.value

Expand All @@ -441,10 +438,9 @@ def to_string(self) -> str:
not valid UTF-8.
"""
if self.type is not Type.STRING:
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = \"string\";', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} "
f"in {self.node.dt.filename} to be assigned with "
f"'{self.name} = \"string\";', not '{self}'")

try:
ret = self.value.decode("utf-8")[:-1] # Strip null
Expand All @@ -467,10 +463,9 @@ def to_strings(self) -> list[str]:
Also raises DTError if any of the strings are not valid UTF-8.
"""
if self.type not in (Type.STRING, Type.STRINGS):
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = \"string\", \"string\", ... ;', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with "
f"'{self.name} = \"string\", \"string\", ... ;', not '{self}'")

try:
ret = self.value.decode("utf-8").split("\0")[:-1]
Expand All @@ -491,10 +486,9 @@ def to_node(self) -> Node:
foo = < &bar >;
"""
if self.type is not Type.PHANDLE:
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = < &foo >;', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with "
f"'{self.name} = < &foo >;', not '{self}'")

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

Expand All @@ -517,10 +511,9 @@ def type_ok():
return self.type is Type.NUMS and not self.value

if not type_ok():
_err("expected property '{0}' on {1} in {2} to be assigned with "
"'{0} = < &foo &bar ... >;', not '{3}'"
.format(self.name, self.node.path,
self.node.dt.filename, self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with "
f"'{self.name} = < &foo &bar ... >;', not '{self}'")

return [self.node.dt.phandle2node[int.from_bytes(self.value[i:i + 4],
"big")]
Expand All @@ -539,10 +532,10 @@ def to_path(self) -> Node:
For the second case, DTError is raised if the path does not exist.
"""
if self.type not in (Type.PATH, Type.STRING):
_err("expected property '{0}' on {1} in {2} to be assigned with "
"either '{0} = &foo' or '{0} = \"/path/to/node\"', not '{3}'"
.format(self.name, self.node.path, self.node.dt.filename,
self))
_err(f"expected property '{self.name}' on {self.node.path} in "
f"{self.node.dt.filename} to be assigned with either "
f"'{self.name} = &foo' or '{self.name} = \"/path/to/node\"', "
f"not '{self}'")

try:
path = self.value.decode("utf-8")[:-1]
Expand Down
8 changes: 3 additions & 5 deletions scripts/dts/python-devicetree/src/devicetree/edtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,9 @@ def _prop_val(

if prop_type == "boolean":
if prop.type != Type.EMPTY:
_err(
"'{0}' in {1!r} is defined with 'type: boolean' in {2}, "
"but is assigned a value ('{3}') instead of being empty "
"('{0};')".format(name, node, binding_path, prop)
)
_err(f"'{name}' in {node!r} is defined with 'type: boolean' "
f"in {binding_path}, but is assigned a value ('{prop}') "
f"instead of being empty ('{name};')")
return True

if prop_type == "int":
Expand Down

0 comments on commit 25187c9

Please sign in to comment.