From 912ff76fda9ec5f79e96b5d1064ce9be8a8c95bf Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 10 Jan 2022 12:07:22 +0100 Subject: [PATCH] Enable strict typing for `core.py` (#63244) --- .strict-typing | 1 + mypy.ini | 3 +++ script/hassfest/mypy_config.py | 24 ++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.strict-typing b/.strict-typing index c81721d926c..f6a1b48bcca 100644 --- a/.strict-typing +++ b/.strict-typing @@ -2,6 +2,7 @@ # If component is fully covered with type annotations, please add it here # to enable strict mypy checks. +homeassistant.core homeassistant.components homeassistant.components.acer_projector.* homeassistant.components.accuweather.* diff --git a/mypy.ini b/mypy.ini index 1a5fb092299..0fa832130e8 100644 --- a/mypy.ini +++ b/mypy.ini @@ -22,6 +22,9 @@ no_implicit_optional = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.core] +disallow_any_generics = true + [mypy-homeassistant.components.*] check_untyped_defs = false disallow_incomplete_defs = false diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 9a5e0ad948d..f42ee1c2e87 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -175,6 +175,12 @@ STRICT_SETTINGS: Final[list[str]] = [ # "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: """Validate and generate mypy config.""" @@ -185,12 +191,20 @@ def generate_and_validate(config: Config) -> str: lines = fp.readlines() # Filter empty and commented lines. - strict_modules: list[str] = [ + parsed_modules: list[str] = [ line.strip() for line in lines 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) for module in strict_modules: if ( @@ -206,7 +220,7 @@ def generate_and_validate(config: Config) -> str: ) # 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: if module.endswith(".*"): 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: 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. components_section = "mypy-homeassistant.components.*" mypy_config.add_section(components_section)