1
0
mirror of https://github.com/home-assistant/core synced 2024-07-08 20:17:01 +00:00

Enable strict typing for core.py (#63244)

This commit is contained in:
Marc Mueller 2022-01-10 12:07:22 +01:00 committed by GitHub
parent 51292cc135
commit 912ff76fda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -2,6 +2,7 @@
# If component is fully covered with type annotations, please add it here # If component is fully covered with type annotations, please add it here
# to enable strict mypy checks. # to enable strict mypy checks.
homeassistant.core
homeassistant.components homeassistant.components
homeassistant.components.acer_projector.* homeassistant.components.acer_projector.*
homeassistant.components.accuweather.* homeassistant.components.accuweather.*

View File

@ -22,6 +22,9 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.core]
disallow_any_generics = true
[mypy-homeassistant.components.*] [mypy-homeassistant.components.*]
check_untyped_defs = false check_untyped_defs = false
disallow_incomplete_defs = false disallow_incomplete_defs = false

View File

@ -175,6 +175,12 @@ STRICT_SETTINGS: Final[list[str]] = [
# "no_implicit_reexport", # "no_implicit_reexport",
] ]
# Strict settings are already applied for core files.
# To enable granular typing, add additional settings if core files are given.
STRICT_SETTINGS_CORE: Final[list[str]] = [
"disallow_any_generics",
]
def generate_and_validate(config: Config) -> str: def generate_and_validate(config: Config) -> str:
"""Validate and generate mypy config.""" """Validate and generate mypy config."""
@ -185,12 +191,20 @@ def generate_and_validate(config: Config) -> str:
lines = fp.readlines() lines = fp.readlines()
# Filter empty and commented lines. # Filter empty and commented lines.
strict_modules: list[str] = [ parsed_modules: list[str] = [
line.strip() line.strip()
for line in lines for line in lines
if line.strip() != "" and not line.startswith("#") if line.strip() != "" and not line.startswith("#")
] ]
strict_modules: list[str] = []
strict_core_modules: list[str] = []
for module in parsed_modules:
if module.startswith("homeassistant.components"):
strict_modules.append(module)
else:
strict_core_modules.append(module)
ignored_modules_set: set[str] = set(IGNORED_MODULES) ignored_modules_set: set[str] = set(IGNORED_MODULES)
for module in strict_modules: for module in strict_modules:
if ( if (
@ -206,7 +220,7 @@ def generate_and_validate(config: Config) -> str:
) )
# Validate that all modules exist. # Validate that all modules exist.
all_modules = strict_modules + IGNORED_MODULES all_modules = strict_modules + strict_core_modules + IGNORED_MODULES
for module in all_modules: for module in all_modules:
if module.endswith(".*"): if module.endswith(".*"):
module_path = Path(module[:-2].replace(".", os.path.sep)) module_path = Path(module[:-2].replace(".", os.path.sep))
@ -234,6 +248,12 @@ def generate_and_validate(config: Config) -> str:
for key in STRICT_SETTINGS: for key in STRICT_SETTINGS:
mypy_config.set(general_section, key, "true") mypy_config.set(general_section, key, "true")
for core_module in strict_core_modules:
core_section = f"mypy-{core_module}"
mypy_config.add_section(core_section)
for key in STRICT_SETTINGS_CORE:
mypy_config.set(core_section, key, "true")
# By default strict checks are disabled for components. # By default strict checks are disabled for components.
components_section = "mypy-homeassistant.components.*" components_section = "mypy-homeassistant.components.*"
mypy_config.add_section(components_section) mypy_config.add_section(components_section)