Skip to content

Commit

Permalink
Add Self typing (3) [mypy 1.0] (home-assistant#87600)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Feb 7, 2023
1 parent 342b406 commit f7b39aa
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 19 deletions.
5 changes: 3 additions & 2 deletions homeassistant/components/counter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging

from typing_extensions import Self
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -171,14 +172,14 @@ def __init__(self, config: ConfigType) -> None:
self._state: int | None = config[CONF_INITIAL]

@classmethod
def from_storage(cls, config: ConfigType) -> Counter:
def from_storage(cls, config: ConfigType) -> Self:
"""Create counter instance from storage."""
counter = cls(config)
counter.editable = True
return counter

@classmethod
def from_yaml(cls, config: ConfigType) -> Counter:
def from_yaml(cls, config: ConfigType) -> Self:
"""Create counter instance from yaml config."""
counter = cls(config)
counter.editable = False
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_boolean/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from typing import Any

from typing_extensions import Self
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -167,14 +168,14 @@ def __init__(self, config: ConfigType) -> None:
self._attr_unique_id = config[CONF_ID]

@classmethod
def from_storage(cls, config: ConfigType) -> InputBoolean:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
input_bool = cls(config)
input_bool.editable = True
return input_bool

@classmethod
def from_yaml(cls, config: ConfigType) -> InputBoolean:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
input_bool = cls(config)
input_bool.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_button/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from typing import cast

from typing_extensions import Self
import voluptuous as vol

from homeassistant.components.button import SERVICE_PRESS, ButtonEntity
Expand Down Expand Up @@ -147,14 +148,14 @@ def __init__(self, config: ConfigType) -> None:
self._attr_unique_id = config[CONF_ID]

@classmethod
def from_storage(cls, config: ConfigType) -> InputButton:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
button = cls(config)
button.editable = True
return button

@classmethod
def from_yaml(cls, config: ConfigType) -> InputButton:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
button = cls(config)
button.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_datetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
from typing import Any

from typing_extensions import Self
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -250,14 +251,14 @@ def __init__(self, config: ConfigType) -> None:
)

@classmethod
def from_storage(cls, config: ConfigType) -> InputDatetime:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
input_dt = cls(config)
input_dt.editable = True
return input_dt

@classmethod
def from_yaml(cls, config: ConfigType) -> InputDatetime:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
input_dt = cls(config)
input_dt.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from contextlib import suppress
import logging

from typing_extensions import Self
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -218,14 +219,14 @@ def __init__(self, config: ConfigType) -> None:
self._current_value: float | None = config.get(CONF_INITIAL)

@classmethod
def from_storage(cls, config: ConfigType) -> InputNumber:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
input_num = cls(config)
input_num.editable = True
return input_num

@classmethod
def from_yaml(cls, config: ConfigType) -> InputNumber:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
input_num = cls(config)
input_num.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from typing import Any, cast

from typing_extensions import Self
import voluptuous as vol

from homeassistant.components.select import (
Expand Down Expand Up @@ -268,14 +269,14 @@ def __init__(self, config: ConfigType) -> None:
self._attr_unique_id = config[CONF_ID]

@classmethod
def from_storage(cls, config: ConfigType) -> InputSelect:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
input_select = cls(config)
input_select.editable = True
return input_select

@classmethod
def from_yaml(cls, config: ConfigType) -> InputSelect:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
input_select = cls(config)
input_select.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/input_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import logging

from typing_extensions import Self
import voluptuous as vol

from homeassistant.const import (
Expand Down Expand Up @@ -196,14 +197,14 @@ def __init__(self, config: ConfigType) -> None:
self._current_value = config.get(CONF_INITIAL)

@classmethod
def from_storage(cls, config: ConfigType) -> InputText:
def from_storage(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from storage."""
input_text = cls(config)
input_text.editable = True
return input_text

@classmethod
def from_yaml(cls, config: ConfigType) -> InputText:
def from_yaml(cls, config: ConfigType) -> Self:
"""Return entity instance initialized from yaml."""
input_text = cls(config)
input_text.entity_id = f"{DOMAIN}.{config[CONF_ID]}"
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
from typing import Any, cast, final

from typing_extensions import Self
import voluptuous as vol

from homeassistant.backports.enum import StrEnum
Expand Down Expand Up @@ -679,7 +680,7 @@ def __post_init__(self) -> None:
)

@classmethod
def from_csv_row(cls, csv_row: list[str]) -> Profile:
def from_csv_row(cls, csv_row: list[str]) -> Self:
"""Create profile from a CSV row tuple."""
return cls(*cls.SCHEMA(csv_row))

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/met/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any

import metno
from typing_extensions import Self

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand Down Expand Up @@ -181,7 +182,7 @@ def set_coordinates(self) -> bool:
)
return True

async def fetch_data(self) -> MetWeatherData:
async def fetch_data(self) -> Self:
"""Fetch data from API - (current weather and forecast)."""
resp = await self._weather_data.fetching_data()
if not resp:
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/minio/minio_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Minio helper methods."""
from __future__ import annotations

from collections.abc import Iterable, Iterator
from collections.abc import Iterable
import json
import logging
from queue import Queue
Expand All @@ -11,6 +11,7 @@
from urllib.parse import unquote

from minio import Minio
from typing_extensions import Self
from urllib3.exceptions import HTTPError

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -53,7 +54,7 @@ def get_minio_notification_response(
class MinioEventStreamIterator(Iterable):
"""Iterator wrapper over notification http response stream."""

def __iter__(self) -> Iterator:
def __iter__(self) -> Self:
"""Return self."""
return self

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from math import ceil, floor
from typing import Any, final

from typing_extensions import Self
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -540,7 +541,7 @@ def as_dict(self) -> dict[str, Any]:
return dataclasses.asdict(self)

@classmethod
def from_dict(cls, restored: dict[str, Any]) -> NumberExtraStoredData | None:
def from_dict(cls, restored: dict[str, Any]) -> Self | None:
"""Initialize a stored number state from a dict."""
try:
return cls(
Expand Down

0 comments on commit f7b39aa

Please sign in to comment.