From 63706d2f67f47de2fa5fe62fc2be0555f2d4658b Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:46:32 +0200 Subject: [PATCH] Remove blueprint from mypy ignore list (#74990) --- homeassistant/components/blueprint/errors.py | 10 ++++++---- homeassistant/components/blueprint/importer.py | 11 +++++++---- homeassistant/components/blueprint/models.py | 13 +++++++------ homeassistant/components/blueprint/websocket_api.py | 8 ++++---- mypy.ini | 9 --------- script/hassfest/mypy_config.py | 3 --- 6 files changed, 24 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/blueprint/errors.py b/homeassistant/components/blueprint/errors.py index 4b14201652f..aceca533d23 100644 --- a/homeassistant/components/blueprint/errors.py +++ b/homeassistant/components/blueprint/errors.py @@ -13,7 +13,7 @@ from homeassistant.exceptions import HomeAssistantError class BlueprintException(HomeAssistantError): """Base exception for blueprint errors.""" - def __init__(self, domain: str, msg: str) -> None: + def __init__(self, domain: str | None, msg: str) -> None: """Initialize a blueprint exception.""" super().__init__(msg) self.domain = domain @@ -22,7 +22,9 @@ class BlueprintException(HomeAssistantError): class BlueprintWithNameException(BlueprintException): """Base exception for blueprint errors.""" - def __init__(self, domain: str, blueprint_name: str, msg: str) -> None: + def __init__( + self, domain: str | None, blueprint_name: str | None, msg: str + ) -> None: """Initialize blueprint exception.""" super().__init__(domain, msg) self.blueprint_name = blueprint_name @@ -41,8 +43,8 @@ class InvalidBlueprint(BlueprintWithNameException): def __init__( self, - domain: str, - blueprint_name: str, + domain: str | None, + blueprint_name: str | None, blueprint_data: Any, msg_or_exc: vol.Invalid, ) -> None: diff --git a/homeassistant/components/blueprint/importer.py b/homeassistant/components/blueprint/importer.py index de39741d8ed..f8b37a97c31 100644 --- a/homeassistant/components/blueprint/importer.py +++ b/homeassistant/components/blueprint/importer.py @@ -91,12 +91,12 @@ def _get_community_post_import_url(url: str) -> str: def _extract_blueprint_from_community_topic( url: str, topic: dict, -) -> ImportedBlueprint | None: +) -> ImportedBlueprint: """Extract a blueprint from a community post JSON. Async friendly. """ - block_content = None + block_content: str blueprint = None post = topic["post_stream"]["posts"][0] @@ -118,6 +118,7 @@ def _extract_blueprint_from_community_topic( if not is_blueprint_config(data): continue + assert isinstance(data, dict) blueprint = Blueprint(data) break @@ -134,7 +135,7 @@ def _extract_blueprint_from_community_topic( async def fetch_blueprint_from_community_post( hass: HomeAssistant, url: str -) -> ImportedBlueprint | None: +) -> ImportedBlueprint: """Get blueprints from a community post url. Method can raise aiohttp client exceptions, vol.Invalid. @@ -160,6 +161,7 @@ async def fetch_blueprint_from_github_url( resp = await session.get(import_url, raise_for_status=True) raw_yaml = await resp.text() data = yaml.parse_yaml(raw_yaml) + assert isinstance(data, dict) blueprint = Blueprint(data) parsed_import_url = yarl.URL(import_url) @@ -189,7 +191,7 @@ async def fetch_blueprint_from_github_gist_url( blueprint = None filename = None - content = None + content: str for filename, info in gist["files"].items(): if not filename.endswith(".yaml"): @@ -200,6 +202,7 @@ async def fetch_blueprint_from_github_gist_url( if not is_blueprint_config(data): continue + assert isinstance(data, dict) blueprint = Blueprint(data) break diff --git a/homeassistant/components/blueprint/models.py b/homeassistant/components/blueprint/models.py index a8146764710..0d90c663b4f 100644 --- a/homeassistant/components/blueprint/models.py +++ b/homeassistant/components/blueprint/models.py @@ -188,7 +188,7 @@ class DomainBlueprints: self.hass = hass self.domain = domain self.logger = logger - self._blueprints = {} + self._blueprints: dict[str, Blueprint | None] = {} self._load_lock = asyncio.Lock() hass.data.setdefault(DOMAIN, {})[domain] = self @@ -216,19 +216,20 @@ class DomainBlueprints: except HomeAssistantError as err: raise FailedToLoad(self.domain, blueprint_path, err) from err + assert isinstance(blueprint_data, dict) return Blueprint( blueprint_data, expected_domain=self.domain, path=blueprint_path ) - def _load_blueprints(self) -> dict[str, Blueprint | BlueprintException]: + def _load_blueprints(self) -> dict[str, Blueprint | BlueprintException | None]: """Load all the blueprints.""" blueprint_folder = pathlib.Path( self.hass.config.path(BLUEPRINT_FOLDER, self.domain) ) - results = {} + results: dict[str, Blueprint | BlueprintException | None] = {} - for blueprint_path in blueprint_folder.glob("**/*.yaml"): - blueprint_path = str(blueprint_path.relative_to(blueprint_folder)) + for path in blueprint_folder.glob("**/*.yaml"): + blueprint_path = str(path.relative_to(blueprint_folder)) if self._blueprints.get(blueprint_path) is None: try: self._blueprints[blueprint_path] = self._load_blueprint( @@ -245,7 +246,7 @@ class DomainBlueprints: async def async_get_blueprints( self, - ) -> dict[str, Blueprint | BlueprintException]: + ) -> dict[str, Blueprint | BlueprintException | None]: """Get all the blueprints.""" async with self._load_lock: return await self.hass.async_add_executor_job(self._load_blueprints) diff --git a/homeassistant/components/blueprint/websocket_api.py b/homeassistant/components/blueprint/websocket_api.py index b8a4c214a2e..0b84d1d08c2 100644 --- a/homeassistant/components/blueprint/websocket_api.py +++ b/homeassistant/components/blueprint/websocket_api.py @@ -24,13 +24,13 @@ def async_setup(hass: HomeAssistant): websocket_api.async_register_command(hass, ws_delete_blueprint) -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "blueprint/list", vol.Required("domain"): cv.string, } ) +@websocket_api.async_response async def ws_list_blueprints(hass, connection, msg): """List available blueprints.""" domain_blueprints: dict[str, models.DomainBlueprints] | None = hass.data.get( @@ -55,13 +55,13 @@ async def ws_list_blueprints(hass, connection, msg): connection.send_result(msg["id"], results) -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "blueprint/import", vol.Required("url"): cv.url, } ) +@websocket_api.async_response async def ws_import_blueprint(hass, connection, msg): """Import a blueprint.""" async with async_timeout.timeout(10): @@ -86,7 +86,6 @@ async def ws_import_blueprint(hass, connection, msg): ) -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "blueprint/save", @@ -96,6 +95,7 @@ async def ws_import_blueprint(hass, connection, msg): vol.Optional("source_url"): cv.url, } ) +@websocket_api.async_response async def ws_save_blueprint(hass, connection, msg): """Save a blueprint.""" @@ -135,7 +135,6 @@ async def ws_save_blueprint(hass, connection, msg): ) -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "blueprint/delete", @@ -143,6 +142,7 @@ async def ws_save_blueprint(hass, connection, msg): vol.Required("path"): cv.path, } ) +@websocket_api.async_response async def ws_delete_blueprint(hass, connection, msg): """Delete a blueprint.""" diff --git a/mypy.ini b/mypy.ini index 1fb12c3a47e..4b74049f084 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2656,15 +2656,6 @@ no_implicit_optional = false warn_return_any = false warn_unreachable = false -[mypy-homeassistant.components.blueprint.importer] -ignore_errors = true - -[mypy-homeassistant.components.blueprint.models] -ignore_errors = true - -[mypy-homeassistant.components.blueprint.websocket_api] -ignore_errors = true - [mypy-homeassistant.components.cloud.client] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 17ef8edc529..a2c0fb37629 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -16,9 +16,6 @@ from .model import Config, Integration # remove your component from this list to enable type checks. # Do your best to not add anything new here. IGNORED_MODULES: Final[list[str]] = [ - "homeassistant.components.blueprint.importer", - "homeassistant.components.blueprint.models", - "homeassistant.components.blueprint.websocket_api", "homeassistant.components.cloud.client", "homeassistant.components.cloud.http_api", "homeassistant.components.conversation",