1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-05 12:52:19 +01:00

Merge branch 'ruff_ret' into integration

This commit is contained in:
J. Nick Koston
2025-07-29 22:55:33 -10:00
45 changed files with 292 additions and 180 deletions

View File

@@ -538,8 +538,7 @@ class BoolType(TypeInfo):
wire_type = WireType.VARINT # Uses wire type 0
def dump(self, name: str) -> str:
o = f"out.append(YESNO({name}));"
return o
return f"out.append(YESNO({name}));"
def get_size_calculation(self, name: str, force: bool = False) -> str:
return self._get_simple_size_calculation(name, force, "add_bool")
@@ -591,9 +590,8 @@ class StringType(TypeInfo):
if no_zero_copy:
# Use the std::string directly
return f"buffer.encode_string({self.number}, this->{self.field_name});"
else:
# Use the StringRef
return f"buffer.encode_string({self.number}, this->{self.field_name}_ref_);"
# Use the StringRef
return f"buffer.encode_string({self.number}, this->{self.field_name}_ref_);"
def dump(self, name):
# Check if no_zero_copy option is set
@@ -715,8 +713,7 @@ class MessageType(TypeInfo):
return f"case {self.number}: value.decode_to_message(this->{self.field_name}); break;"
def dump(self, name: str) -> str:
o = f"{name}.dump_to(out);"
return o
return f"{name}.dump_to(out);"
@property
def dump_content(self) -> str:
@@ -864,8 +861,7 @@ class FixedArrayBytesType(TypeInfo):
return f"buffer.encode_bytes({self.number}, this->{self.field_name}, this->{self.field_name}_len);"
def dump(self, name: str) -> str:
o = f"out.append(format_hex_pretty({name}, {name}_len));"
return o
return f"out.append(format_hex_pretty({name}, {name}_len));"
@property
def dump_content(self) -> str:
@@ -882,9 +878,8 @@ class FixedArrayBytesType(TypeInfo):
if force:
# For repeated fields, always calculate size (no zero check)
return f"size.add_length_force({field_id_size}, {length_field});"
else:
# For non-repeated fields, add_length already checks for zero
return f"size.add_length({field_id_size}, {length_field});"
# For non-repeated fields, add_length already checks for zero
return f"size.add_length({field_id_size}, {length_field});"
def get_estimated_size(self) -> int:
# Estimate based on typical BLE advertisement size
@@ -939,8 +934,7 @@ class EnumType(TypeInfo):
return f"buffer.{self.encode_func}({self.number}, static_cast<uint32_t>(this->{self.field_name}));"
def dump(self, name: str) -> str:
o = f"out.append(proto_enum_to_string<{self.cpp_type}>({name}));"
return o
return f"out.append(proto_enum_to_string<{self.cpp_type}>({name}));"
def dump_field_value(self, value: str) -> str:
# Enums need explicit cast for the template
@@ -1109,13 +1103,12 @@ class FixedArrayRepeatedType(TypeInfo):
def encode_element(element: str) -> str:
if isinstance(self._ti, EnumType):
return f"buffer.{self._ti.encode_func}({self.number}, static_cast<uint32_t>({element}), true);"
else:
return f"buffer.{self._ti.encode_func}({self.number}, {element}, true);"
return f"buffer.{self._ti.encode_func}({self.number}, {element}, true);"
# Unroll small arrays for efficiency
if self.array_size == 1:
return encode_element(f"this->{self.field_name}[0]")
elif self.array_size == 2:
if self.array_size == 2:
return (
encode_element(f"this->{self.field_name}[0]")
+ "\n "
@@ -1194,8 +1187,7 @@ class RepeatedTypeInfo(TypeInfo):
# use it as-is, otherwise append the element type
if "<" in self._container_type and ">" in self._container_type:
return f"const {self._container_type}*"
else:
return f"const {self._container_type}<{self._ti.cpp_type}>*"
return f"const {self._container_type}<{self._ti.cpp_type}>*"
return f"std::vector<{self._ti.cpp_type}>"
@property
@@ -1281,14 +1273,13 @@ class RepeatedTypeInfo(TypeInfo):
o += f" buffer.{self._ti.encode_func}({self.number}, it, true);\n"
o += "}"
return o
o = f"for (auto {'' if self._ti_is_bool else '&'}it : this->{self.field_name}) {{\n"
if isinstance(self._ti, EnumType):
o += f" buffer.{self._ti.encode_func}({self.number}, static_cast<uint32_t>(it), true);\n"
else:
o = f"for (auto {'' if self._ti_is_bool else '&'}it : this->{self.field_name}) {{\n"
if isinstance(self._ti, EnumType):
o += f" buffer.{self._ti.encode_func}({self.number}, static_cast<uint32_t>(it), true);\n"
else:
o += f" buffer.{self._ti.encode_func}({self.number}, it, true);\n"
o += "}"
return o
o += f" buffer.{self._ti.encode_func}({self.number}, it, true);\n"
o += "}"
return o
@property
def dump_content(self) -> str:

View File

@@ -444,8 +444,7 @@ def get_str_path_schema(strPath):
if len(parts) > 2:
parts[0] += "." + parts[1]
parts[1] = parts[2]
s1 = output.get(parts[0], {}).get(S_SCHEMAS, {}).get(parts[1], {})
return s1
return output.get(parts[0], {}).get(S_SCHEMAS, {}).get(parts[1], {})
def pop_str_path_schema(strPath):

View File

@@ -42,12 +42,11 @@ CONFIG_NEWLIB_LIBC=y
def extract_defines(command):
define_pattern = re.compile(r"-D\s*([^\s]+)")
defines = [
return [
match
for match in define_pattern.findall(command)
if match not in ("_ASMLANGUAGE")
]
return defines
def find_cxx_path(commands):
for entry in commands:
@@ -56,6 +55,7 @@ CONFIG_NEWLIB_LIBC=y
if not cxx_path.endswith("++"):
continue
return cxx_path
return None
def get_builtin_include_paths(compiler):
result = subprocess.run(
@@ -83,11 +83,10 @@ CONFIG_NEWLIB_LIBC=y
flag_pattern = re.compile(
r"(-O[0-3s]|-g|-std=[^\s]+|-Wall|-Wextra|-Werror|--[^\s]+|-f[^\s]+|-m[^\s]+|-imacros\s*[^\s]+)"
)
flags = [
return [
match.replace("-imacros ", "-imacros")
for match in flag_pattern.findall(command)
]
return flags
def transform_to_idedata_format(compile_commands):
cxx_path = find_cxx_path(compile_commands)