Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade mypy to 0.720, turn on unreachability warnings #25157

Merged
merged 2 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _recursive_merge(


async def merge_packages_config(hass: HomeAssistant, config: Dict,
packages: Dict,
packages: Dict[str, Any],
_log_pkg_error: Callable = _log_pkg_error) \
-> Dict:
"""Merge packages into the top-level configuration. Mutate config."""
Expand Down Expand Up @@ -642,11 +642,6 @@ async def merge_packages_config(hass: HomeAssistant, config: Dict,
pack_name, comp_name, config,
"cannot be merged. Dict expected in main config.")
continue
if not isinstance(comp_conf, dict):
_log_pkg_error(
pack_name, comp_name, config,
"cannot be merged. Dict expected in package.")
continue

error = _recursive_merge(conf=config[comp_name],
package=comp_conf)
Expand Down
7 changes: 5 additions & 2 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import logging
import functools
import uuid
from typing import Callable, List, Optional, Set # noqa pylint: disable=unused-import
from typing import (
Any, Callable, List, Optional, Set # noqa pylint: disable=unused-import
)
import weakref

from homeassistant import data_entry_flow, loader
Expand Down Expand Up @@ -121,7 +123,8 @@ def __init__(self, version: int, domain: str, title: str, data: dict,
self.update_listeners = [] # type: list

# Function to cancel a scheduled retry
self._async_cancel_retry_setup = None
self._async_cancel_retry_setup = \
None # type: Optional[Callable[[], Any]]

async def async_setup(
self, hass: HomeAssistant, *,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/data_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class FlowHandler:
hass = None
handler = None
cur_step = None
context = None
context = None # type: Optional[Dict]

# Set by _async_create_flow callback
init_step = 'init'
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/helpers/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def has_location(state: State) -> bool:

Async friendly.
"""
return (isinstance(state, State) and
# type ignore: https://github.com/python/mypy/issues/7207
return (isinstance(state, State) and # type: ignore
isinstance(state.attributes.get(ATTR_LATITUDE), float) and
isinstance(state.attributes.get(ATTR_LONGITUDE), float))

Expand Down
8 changes: 5 additions & 3 deletions homeassistant/helpers/temperature.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Temperature helpers for Home Assistant."""
from numbers import Number
from typing import Optional

from homeassistant.core import HomeAssistant
from homeassistant.util.temperature import convert as convert_temperature
from homeassistant.const import PRECISION_HALVES, PRECISION_TENTHS


def display_temp(hass: HomeAssistant, temperature: float, unit: str,
precision: float) -> float:
def display_temp(hass: HomeAssistant, temperature: Optional[float], unit: str,
precision: float) -> Optional[float]:
"""Convert temperature into preferred units/precision for display."""
temperature_unit = unit
ha_unit = hass.config.units.temperature_unit
Expand All @@ -21,7 +22,8 @@ def display_temp(hass: HomeAssistant, temperature: float, unit: str,
raise TypeError(
"Temperature is not a number: {}".format(temperature))

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

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def repr_helper(inp: Any) -> str:
return str(inp)


def convert(value: T, to_type: Callable[[T], U],
def convert(value: Optional[T], to_type: Callable[[T], U],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that Optional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is really optional -- the code has specific checks and treatment of None values.

default: Optional[U] = None) -> Optional[U]:
"""Convert value to to_type, returns default if fails."""
try:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/util/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

try:
# pylint: disable=invalid-name
asyncio_run = asyncio.run # type: ignore
asyncio_run = asyncio.run
except AttributeError:
_T = TypeVar('_T')

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/util/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def convert(value: float, unit_1: str, unit_2: str) -> float:
if not isinstance(value, Number):
raise TypeError('{} is not of numeric type'.format(value))

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

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

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

pascals = value / UNIT_CONVERSION[unit_1]
Expand Down
13 changes: 9 additions & 4 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,36 @@ def temperature(self, temperature: float, from_unit: str) -> float:
raise TypeError(
'{} is not a numeric value.'.format(str(temperature)))

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

def length(self, length: Optional[float], from_unit: str) -> float:
"""Convert the given length to this unit system."""
if not isinstance(length, Number):
raise TypeError('{} is not a numeric value.'.format(str(length)))

return distance_util.convert(length, from_unit,
# type ignore: https://github.com/python/mypy/issues/7207
return distance_util.convert(length, from_unit, # type: ignore
self.length_unit)

def pressure(self, pressure: Optional[float], from_unit: str) -> float:
"""Convert the given pressure to this unit system."""
if not isinstance(pressure, Number):
raise TypeError('{} is not a numeric value.'.format(str(pressure)))

return pressure_util.convert(pressure, from_unit,
# type ignore: https://github.com/python/mypy/issues/7207
return pressure_util.convert(pressure, from_unit, # type: ignore
self.pressure_unit)

def volume(self, volume: Optional[float], from_unit: str) -> float:
"""Convert the given volume to this unit system."""
if not isinstance(volume, Number):
raise TypeError('{} is not a numeric value.'.format(str(volume)))

return volume_util.convert(volume, from_unit, self.volume_unit)
# type ignore: https://github.com/python/mypy/issues/7207
return volume_util.convert(volume, from_unit, # type: ignore
self.volume_unit)

def as_dict(self) -> dict:
"""Convert the unit system to a dictionary."""
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/util/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def convert(volume: float, from_unit: str, to_unit: str) -> float:
if not isinstance(volume, Number):
raise TypeError('{} is not of numeric type'.format(volume))

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

result = volume
Expand Down
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ strict_equality = true
warn_incomplete_stub = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

Expand Down
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ coveralls==1.2.0
flake8-docstrings==1.3.0
flake8==3.7.8
mock-open==1.3.1
mypy==0.711
mypy==0.720
pydocstyle==3.0.0
pylint==2.3.1
pytest-aiohttp==0.3.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ coveralls==1.2.0
flake8-docstrings==1.3.0
flake8==3.7.8
mock-open==1.3.1
mypy==0.711
mypy==0.720
pydocstyle==3.0.0
pylint==2.3.1
pytest-aiohttp==0.3.0
Expand Down