Skip to content

Commit

Permalink
Upgrade mypy to 0.770, tighten config a bit (home-assistant#32715)
Browse files Browse the repository at this point in the history
* Upgrade mypy to 0.770, related cleanups

https://mypy-lang.blogspot.com/2020/03/mypy-0770-released.html

* Clean up config and make it a notch stricter, address findings
  • Loading branch information
scop authored Mar 12, 2020
1 parent 77ebda0 commit 221d520
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 37 deletions.
10 changes: 6 additions & 4 deletions homeassistant/helpers/config_entry_oauth2_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ def extra_authorize_data(self) -> dict:
"""Extra data that needs to be appended to the authorize url."""
return {}

async def async_step_pick_implementation(self, user_input: dict = None) -> dict:
async def async_step_pick_implementation(
self, user_input: Optional[dict] = None
) -> dict:
"""Handle a flow start."""
assert self.hass
implementations = await async_get_implementations(self.hass, self.DOMAIN)
Expand Down Expand Up @@ -224,7 +226,7 @@ async def async_step_pick_implementation(self, user_input: dict = None) -> dict:
),
)

async def async_step_auth(self, user_input: dict = None) -> dict:
async def async_step_auth(self, user_input: Optional[dict] = None) -> dict:
"""Create an entry for auth."""
# Flow has been triggered by external data
if user_input:
Expand All @@ -241,7 +243,7 @@ async def async_step_auth(self, user_input: dict = None) -> dict:

return self.async_external_step(step_id="auth", url=url)

async def async_step_creation(self, user_input: dict = None) -> dict:
async def async_step_creation(self, user_input: Optional[dict] = None) -> dict:
"""Create config entry from external data."""
token = await self.flow_impl.async_resolve_external_data(self.external_data)
token["expires_at"] = time.time() + token["expires_in"]
Expand All @@ -259,7 +261,7 @@ async def async_oauth_create_entry(self, data: dict) -> dict:
"""
return self.async_create_entry(title=self.flow_impl.name, data=data)

async def async_step_discovery(self, user_input: dict = None) -> dict:
async def async_step_discovery(self, user_input: Optional[dict] = None) -> dict:
"""Handle a flow initialized by discovery."""
await self.async_set_unique_id(self.DOMAIN)

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def date(value: Any) -> date_sys:

def time_period_str(value: str) -> timedelta:
"""Validate and transform time offset."""
if isinstance(value, int):
if isinstance(value, int): # type: ignore
raise vol.Invalid("Make sure you wrap time values in quotes")
if not isinstance(value, str):
raise vol.Invalid(TIME_PERIOD_ERROR.format(value))
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/helpers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def log(self, level: int, msg: Any, *args: Any, **kwargs: Any) -> None:
"""Log the message provided at the appropriate level."""
if self.isEnabledFor(level):
msg, log_kwargs = self.process(msg, kwargs)
self.logger._log( # type: ignore # pylint: disable=protected-access
self.logger._log( # pylint: disable=protected-access
level, KeywordMessage(msg, args, kwargs), (), **log_kwargs
)

Expand All @@ -48,7 +48,7 @@ def process(
{
k: kwargs[k]
for k in inspect.getfullargspec(
self.logger._log # type: ignore # pylint: disable=protected-access
self.logger._log # pylint: disable=protected-access
).args[1:]
if k in kwargs
},
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/helpers/temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def display_temp(
if not isinstance(temperature, Number):
raise TypeError(f"Temperature is not a number: {temperature}")

# type ignore: https://github.com/python/mypy/issues/7207
if temperature_unit != ha_unit: # type: ignore
if temperature_unit != ha_unit:
temperature = convert_temperature(temperature, temperature_unit, ha_unit)

# Round in the units appropriate
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def ensure_valid(self):
raise TemplateError(err)

def extract_entities(
self, variables: Dict[str, Any] = None
self, variables: Optional[Dict[str, Any]] = None
) -> Union[str, List[str]]:
"""Extract all entities for state_changed listener."""
return extract_entities(self.template, variables)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, domain: str, requirements: List) -> None:


async def async_get_integration_with_requirements(
hass: HomeAssistant, domain: str, done: Set[str] = None
hass: HomeAssistant, domain: str, done: Optional[Set[str]] = None
) -> Integration:
"""Get an integration with all requirements installed, including the dependencies.
Expand Down
6 changes: 4 additions & 2 deletions homeassistant/scripts/benchmark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime
import logging
from timeit import default_timer as timer
from typing import Callable, Dict
from typing import Callable, Dict, TypeVar

from homeassistant import core
from homeassistant.components.websocket_api.const import JSON_DUMP
Expand All @@ -15,6 +15,8 @@
# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
# mypy: no-warn-return-any

CALLABLE_T = TypeVar("CALLABLE_T", bound=Callable) # pylint: disable=invalid-name

BENCHMARKS: Dict[str, Callable] = {}


Expand Down Expand Up @@ -44,7 +46,7 @@ def run(args):
loop.close()


def benchmark(func):
def benchmark(func: CALLABLE_T) -> CALLABLE_T:
"""Decorate to mark a benchmark."""
BENCHMARKS[func.__name__] = func
return func
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/util/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ def convert(value: float, unit_1: str, unit_2: str) -> float:
if not isinstance(value, Number):
raise TypeError(f"{value} is not of numeric type")

# type ignore: https://github.com/python/mypy/issues/7207
if unit_1 == unit_2 or unit_1 not in VALID_UNITS: # type: ignore
if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
return value

meters = value
meters: float = value

if unit_1 == LENGTH_MILES:
meters = __miles_to_meters(value)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/util/pressure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def convert(value: float, unit_1: str, unit_2: str) -> float:
if not isinstance(value, Number):
raise TypeError(f"{value} is not of numeric type")

# type ignore: https://github.com/python/mypy/issues/7207
if unit_1 == unit_2 or unit_1 not in VALID_UNITS: # type: ignore
if unit_1 == unit_2 or unit_1 not in VALID_UNITS:
return value

pascals = value / UNIT_CONVERSION[unit_1]
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/util/ruamel_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict, List, Optional, Union

import ruamel.yaml
from ruamel.yaml import YAML
from ruamel.yaml import YAML # type: ignore
from ruamel.yaml.compat import StringIO
from ruamel.yaml.constructor import SafeConstructor
from ruamel.yaml.error import YAMLError
Expand Down Expand Up @@ -89,8 +89,7 @@ def load_yaml(fname: str, round_trip: bool = False) -> JSON_TYPE:
"""Load a YAML file."""
if round_trip:
yaml = YAML(typ="rt")
# type ignore: https://bitbucket.org/ruamel/yaml/pull-requests/42
yaml.preserve_quotes = True # type: ignore
yaml.preserve_quotes = True
else:
if ExtSafeConstructor.name is None:
ExtSafeConstructor.name = fname
Expand Down
5 changes: 1 addition & 4 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ def temperature(self, temperature: float, from_unit: str) -> float:
if not isinstance(temperature, Number):
raise TypeError(f"{temperature!s} is not a numeric value.")

# type ignore: https://github.com/python/mypy/issues/7207
return temperature_util.convert( # type: ignore
temperature, from_unit, self.temperature_unit
)
return temperature_util.convert(temperature, from_unit, self.temperature_unit)

def length(self, length: Optional[float], from_unit: str) -> float:
"""Convert the given length to this unit system."""
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/util/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ def convert(volume: float, from_unit: str, to_unit: str) -> float:
if not isinstance(volume, Number):
raise TypeError(f"{volume} is not of numeric type")

# type ignore: https://github.com/python/mypy/issues/7207
if from_unit == to_unit: # type: ignore
if from_unit == to_unit:
return volume

result = volume
result: float = volume
if from_unit == VOLUME_LITERS and to_unit == VOLUME_GALLONS:
result = __liter_to_gallon(volume)
elif from_unit == VOLUME_GALLONS and to_unit == VOLUME_LITERS:
Expand Down
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
asynctest==0.13.0
codecov==2.0.15
mock-open==1.3.1
mypy==0.761
mypy==0.770
pre-commit==2.1.1
pylint==2.4.4
astroid==2.3.3
Expand Down
12 changes: 4 additions & 8 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ warn_redundant_casts = true
warn_unused_configs = true

[mypy-homeassistant.bootstrap,homeassistant.components,homeassistant.config_entries,homeassistant.config,homeassistant.const,homeassistant.core,homeassistant.data_entry_flow,homeassistant.exceptions,homeassistant.loader,homeassistant.__main__,homeassistant.requirements,homeassistant.setup,homeassistant.util,homeassistant.auth.*,homeassistant.components.automation.*,homeassistant.components.binary_sensor.*,homeassistant.components.calendar.*,homeassistant.components.cover.*,homeassistant.components.device_automation.*,homeassistant.components.frontend.*,homeassistant.components.geo_location.*,homeassistant.components.group.*,homeassistant.components.history.*,homeassistant.components.http.*,homeassistant.components.image_processing.*,homeassistant.components.integration.*,homeassistant.components.light.*,homeassistant.components.lock.*,homeassistant.components.mailbox.*,homeassistant.components.media_player.*,homeassistant.components.notify.*,homeassistant.components.persistent_notification.*,homeassistant.components.proximity.*,homeassistant.components.remote.*,homeassistant.components.scene.*,homeassistant.components.sensor.*,homeassistant.components.sun.*,homeassistant.components.switch.*,homeassistant.components.systemmonitor.*,homeassistant.components.tts.*,homeassistant.components.vacuum.*,homeassistant.components.water_heater.*,homeassistant.components.weather.*,homeassistant.components.websocket_api.*,homeassistant.components.zone.*,homeassistant.helpers.*,homeassistant.scripts.*,homeassistant.util.*]
strict = true
ignore_errors = false
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_calls = true
disallow_untyped_defs = true
no_implicit_optional = true
strict_equality = true
warn_return_any = true
warn_unreachable = true
warn_unused_ignores = true
# TODO: turn these off, address issues
allow_any_generics = true
implicit_reexport = true

0 comments on commit 221d520

Please sign in to comment.