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

update minimal python version to 3.10 (#8850)

This commit is contained in:
Thomas Rupprecht
2025-05-22 03:21:43 +02:00
committed by GitHub
parent 026f47bfb3
commit aeb4e63950
36 changed files with 148 additions and 166 deletions

View File

@@ -1,9 +1,9 @@
import abc
from collections.abc import Sequence
from collections.abc import Callable, Sequence
import inspect
import math
import re
from typing import Any, Callable, Optional, Union
from typing import Any
from esphome.core import (
CORE,
@@ -35,19 +35,19 @@ class Expression(abc.ABC):
"""
SafeExpType = Union[
Expression,
bool,
str,
str,
int,
float,
TimePeriod,
type[bool],
type[int],
type[float],
Sequence[Any],
]
SafeExpType = (
Expression
| bool
| str
| str
| int
| float
| TimePeriod
| type[bool]
| type[int]
| type[float]
| Sequence[Any]
)
class RawExpression(Expression):
@@ -90,7 +90,7 @@ class VariableDeclarationExpression(Expression):
class ExpressionList(Expression):
__slots__ = ("args",)
def __init__(self, *args: Optional[SafeExpType]):
def __init__(self, *args: SafeExpType | None):
# Remove every None on end
args = list(args)
while args and args[-1] is None:
@@ -139,7 +139,7 @@ class CallExpression(Expression):
class StructInitializer(Expression):
__slots__ = ("base", "args")
def __init__(self, base: Expression, *args: tuple[str, Optional[SafeExpType]]):
def __init__(self, base: Expression, *args: tuple[str, SafeExpType | None]):
self.base = base
# TODO: args is always a Tuple, is this check required?
if not isinstance(args, OrderedDict):
@@ -197,9 +197,7 @@ class ParameterExpression(Expression):
class ParameterListExpression(Expression):
__slots__ = ("parameters",)
def __init__(
self, *parameters: Union[ParameterExpression, tuple[SafeExpType, str]]
):
def __init__(self, *parameters: ParameterExpression | tuple[SafeExpType, str]):
self.parameters = []
for parameter in parameters:
if not isinstance(parameter, ParameterExpression):
@@ -362,7 +360,7 @@ def safe_exp(obj: SafeExpType) -> Expression:
return IntLiteral(int(obj.total_seconds))
if isinstance(obj, TimePeriodMinutes):
return IntLiteral(int(obj.total_minutes))
if isinstance(obj, (tuple, list)):
if isinstance(obj, tuple | list):
return ArrayInitializer(*[safe_exp(o) for o in obj])
if obj is bool:
return bool_
@@ -461,7 +459,7 @@ def static_const_array(id_, rhs) -> "MockObj":
return obj
def statement(expression: Union[Expression, Statement]) -> Statement:
def statement(expression: Expression | Statement) -> Statement:
"""Convert expression into a statement unless is already a statement."""
if isinstance(expression, Statement):
return expression
@@ -579,7 +577,7 @@ def new_Pvariable(id_: ID, *args: SafeExpType) -> Pvariable:
return Pvariable(id_, rhs)
def add(expression: Union[Expression, Statement]):
def add(expression: Expression | Statement):
"""Add an expression to the codegen section.
After this is called, the given given expression will
@@ -588,12 +586,12 @@ def add(expression: Union[Expression, Statement]):
CORE.add(expression)
def add_global(expression: Union[SafeExpType, Statement], prepend: bool = False):
def add_global(expression: SafeExpType | Statement, prepend: bool = False):
"""Add an expression to the codegen global storage (above setup())."""
CORE.add_global(expression, prepend)
def add_library(name: str, version: Optional[str], repository: Optional[str] = None):
def add_library(name: str, version: str | None, repository: str | None = None):
"""Add a library to the codegen library storage.
:param name: The name of the library (for example 'AsyncTCP')
@@ -619,7 +617,7 @@ def add_define(name: str, value: SafeExpType = None):
CORE.add_define(Define(name, safe_exp(value)))
def add_platformio_option(key: str, value: Union[str, list[str]]):
def add_platformio_option(key: str, value: str | list[str]):
CORE.add_platformio_option(key, value)
@@ -654,7 +652,7 @@ async def process_lambda(
parameters: list[tuple[SafeExpType, str]],
capture: str = "=",
return_type: SafeExpType = None,
) -> Union[LambdaExpression, None]:
) -> LambdaExpression | None:
"""Process the given lambda value into a LambdaExpression.
This is a coroutine because lambdas can depend on other IDs,
@@ -711,8 +709,8 @@ def is_template(value):
async def templatable(
value: Any,
args: list[tuple[SafeExpType, str]],
output_type: Optional[SafeExpType],
to_exp: Union[Callable, dict] = None,
output_type: SafeExpType | None,
to_exp: Callable | dict = None,
):
"""Generate code for a templatable config option.
@@ -821,7 +819,7 @@ class MockObj(Expression):
assert self.op == "::"
return MockObj(f"using namespace {self.base}")
def __getitem__(self, item: Union[str, Expression]) -> "MockObj":
def __getitem__(self, item: str | Expression) -> "MockObj":
next_op = "."
if isinstance(item, str) and item.startswith("P"):
item = item[1:]