Improve typing via hassfest serializer (#117382)

pull/117899/head^2
Jakob Schlyter 2024-05-22 08:22:18 +02:00 committed by GitHub
parent 1985a2ad8b
commit 2e68363755
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 18 additions and 7 deletions

View File

@ -5,7 +5,9 @@ To update, run python3 -m script.hassfest
from __future__ import annotations
BLUETOOTH: list[dict[str, bool | str | int | list[int]]] = [
from typing import Final
BLUETOOTH: Final[list[dict[str, bool | str | int | list[int]]]] = [
{
"domain": "airthings_ble",
"manufacturer_id": 820,

View File

@ -7,7 +7,11 @@ to the political situation in the world, please contact the ISO 3166 working gro
"""
COUNTRIES = {
from __future__ import annotations
from typing import Final
COUNTRIES: Final[set[str]] = {
"AD",
"AE",
"AF",

View File

@ -5,7 +5,9 @@ To update, run python3 -m script.hassfest
from __future__ import annotations
DHCP: list[dict[str, str | bool]] = [
from typing import Final
DHCP: Final[list[dict[str, str | bool]]] = [
{
"domain": "airzone",
"macaddress": "E84F25*",

View File

@ -24,5 +24,6 @@ Path("homeassistant/generated/countries.py").write_text(
"COUNTRIES": countries,
},
generator=generator_string,
annotations={"COUNTRIES": "Final[set[str]]"},
)
)

View File

@ -20,7 +20,9 @@ def generate_and_validate(integrations: dict[str, Integration]) -> str:
return format_python_namespace(
{"BLUETOOTH": match_list},
annotations={"BLUETOOTH": "list[dict[str, bool | str | int | list[int]]]"},
annotations={
"BLUETOOTH": "Final[list[dict[str, bool | str | int | list[int]]]]"
},
)

View File

@ -20,7 +20,7 @@ def generate_and_validate(integrations: dict[str, Integration]) -> str:
return format_python_namespace(
{"DHCP": match_list},
annotations={"DHCP": "list[dict[str, str | bool]]"},
annotations={"DHCP": "Final[list[dict[str, str | bool]]]"},
)

View File

@ -102,6 +102,6 @@ def format_python_namespace(
for key, value in sorted(content.items())
)
if annotations:
# If we had any annotations, add the __future__ import.
code = f"from __future__ import annotations\n{code}"
# If we had any annotations, add __future__ and typing imports.
code = f"from __future__ import annotations\n\nfrom typing import Final\n{code}"
return format_python(code, generator=generator)