From cab6c694c54435c6f4f6734d6eb53997a1d4d238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 31 Jan 2018 00:44:05 +0200 Subject: [PATCH] Flake8 bugbear fixes (#12072) * Don't use mutable argument defaults (bugbear B006) * Use callable(x) instead of hasattr(x, '__call__') (bugbear B004) * Remove/mark unused loop control variables (bugbear B007) * Fix stripping protocol from kodi host name (bugbear B005) * Fix plant daily history add default date (bugbear B008) --- homeassistant/components/climate/heatmiser.py | 2 +- homeassistant/components/ios.py | 2 +- homeassistant/components/media_player/plex.py | 2 +- homeassistant/components/notify/kodi.py | 2 +- homeassistant/components/plant.py | 4 ++-- homeassistant/components/rfxtrx.py | 2 +- homeassistant/components/zwave/__init__.py | 2 +- homeassistant/scripts/check_config.py | 2 +- homeassistant/util/location.py | 2 +- tests/components/alexa/test_intent.py | 5 +++-- tests/components/light/test_hue.py | 2 +- tests/components/test_canary.py | 4 ++-- tests/components/test_influxdb.py | 2 +- tests/test_core.py | 8 ++++---- tests/test_util/aiohttp.py | 4 ++-- 15 files changed, 23 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/climate/heatmiser.py b/homeassistant/components/climate/heatmiser.py index b05c880cc37..19c033a319f 100644 --- a/homeassistant/components/climate/heatmiser.py +++ b/homeassistant/components/climate/heatmiser.py @@ -46,7 +46,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): serport = connection.connection(ipaddress, port) serport.open() - for thermostat, tstat in tstats.items(): + for tstat in tstats.values(): add_devices([ HeatmiserV3Thermostat( heatmiser, tstat.get(CONF_ID), tstat.get(CONF_NAME), serport) diff --git a/homeassistant/components/ios.py b/homeassistant/components/ios.py index 5e2528d2f0d..fe3c934659b 100644 --- a/homeassistant/components/ios.py +++ b/homeassistant/components/ios.py @@ -182,7 +182,7 @@ def enabled_push_ids(): """Return a list of push enabled target push IDs.""" push_ids = list() # pylint: disable=unused-variable - for device_name, device in CONFIG_FILE[ATTR_DEVICES].items(): + for device in CONFIG_FILE[ATTR_DEVICES].values(): if device.get(ATTR_PUSH_ID) is not None: push_ids.append(device.get(ATTR_PUSH_ID)) return push_ids diff --git a/homeassistant/components/media_player/plex.py b/homeassistant/components/media_player/plex.py index 38a84053263..b2a89341cf0 100644 --- a/homeassistant/components/media_player/plex.py +++ b/homeassistant/components/media_player/plex.py @@ -175,7 +175,7 @@ def setup_plexserver( else: plex_clients[machine_identifier].refresh(None, session) - for machine_identifier, client in plex_clients.items(): + for client in plex_clients.values(): # force devices to idle that do not have a valid session if client.session is None: client.force_idle() diff --git a/homeassistant/components/notify/kodi.py b/homeassistant/components/notify/kodi.py index 05f4c5d17f3..3eb492f7fa6 100644 --- a/homeassistant/components/notify/kodi.py +++ b/homeassistant/components/notify/kodi.py @@ -51,7 +51,7 @@ def async_get_service(hass, config, discovery_info=None): encryption = config.get(CONF_PROXY_SSL) if host.startswith('http://') or host.startswith('https://'): - host = host.lstrip('http://').lstrip('https://') + host = host[host.index('://') + 3:] _LOGGER.warning( "Kodi host name should no longer contain http:// See updated " "definitions here: " diff --git a/homeassistant/components/plant.py b/homeassistant/components/plant.py index 7df990fa0e5..50bb7c43c72 100644 --- a/homeassistant/components/plant.py +++ b/homeassistant/components/plant.py @@ -336,9 +336,9 @@ class DailyHistory(object): self._max_dict = dict() self.max = None - def add_measurement(self, value, timestamp=datetime.now()): + def add_measurement(self, value, timestamp=None): """Add a new measurement for a certain day.""" - day = timestamp.date() + day = (timestamp or datetime.now()).date() if value is None: return if self._days is None: diff --git a/homeassistant/components/rfxtrx.py b/homeassistant/components/rfxtrx.py index 27bfd1abfbe..de8a0c00d80 100644 --- a/homeassistant/components/rfxtrx.py +++ b/homeassistant/components/rfxtrx.py @@ -171,7 +171,7 @@ def get_pt2262_cmd(device_id, data_bits): # pylint: disable=unused-variable def get_pt2262_device(device_id): """Look for the device which id matches the given device_id parameter.""" - for dev_id, device in RFX_DEVICES.items(): + for device in RFX_DEVICES.values(): if (hasattr(device, 'is_lighting4') and device.masked_id == get_pt2262_deviceid(device_id, device.data_bits)): diff --git a/homeassistant/components/zwave/__init__.py b/homeassistant/components/zwave/__init__.py index 10942de8097..7b8f471850b 100644 --- a/homeassistant/components/zwave/__init__.py +++ b/homeassistant/components/zwave/__init__.py @@ -170,7 +170,7 @@ def _obj_to_dict(obj): """Convert an object into a hash for debug.""" return {key: getattr(obj, key) for key in dir(obj) - if key[0] != '_' and not hasattr(getattr(obj, key), '__call__')} + if key[0] != '_' and not callable(getattr(obj, key))} def _value_name(value): diff --git a/homeassistant/scripts/check_config.py b/homeassistant/scripts/check_config.py index f80eafd72cc..5cfcf628ec5 100644 --- a/homeassistant/scripts/check_config.py +++ b/homeassistant/scripts/check_config.py @@ -134,7 +134,7 @@ def run(script_args: List) -> int: for sfn, sdict in res['secret_cache'].items(): sss = [] - for skey, sval in sdict.items(): + for skey in sdict: if skey in flatsecret: _LOGGER.error('Duplicated secrets in files %s and %s', flatsecret[skey], sfn) diff --git a/homeassistant/util/location.py b/homeassistant/util/location.py index 8b07a344148..35b266cb104 100644 --- a/homeassistant/util/location.py +++ b/homeassistant/util/location.py @@ -107,7 +107,7 @@ def vincenty(point1: Tuple[float, float], point2: Tuple[float, float], sinU2 = math.sin(U2) cosU2 = math.cos(U2) - for iteration in range(MAX_ITERATIONS): + for _ in range(MAX_ITERATIONS): sinLambda = math.sin(Lambda) cosLambda = math.cos(Lambda) sinSigma = math.sqrt((cosU2 * sinLambda) ** 2 + diff --git a/tests/components/alexa/test_intent.py b/tests/components/alexa/test_intent.py index a3587622b3d..2c8fafde155 100644 --- a/tests/components/alexa/test_intent.py +++ b/tests/components/alexa/test_intent.py @@ -98,8 +98,9 @@ def alexa_client(loop, hass, test_client): return loop.run_until_complete(test_client(hass.http.app)) -def _intent_req(client, data={}): - return client.post(intent.INTENTS_API_ENDPOINT, data=json.dumps(data), +def _intent_req(client, data=None): + return client.post(intent.INTENTS_API_ENDPOINT, + data=json.dumps(data or {}), headers={'content-type': 'application/json'}) diff --git a/tests/components/light/test_hue.py b/tests/components/light/test_hue.py index e1d1cdaadec..5c28ea9988f 100644 --- a/tests/components/light/test_hue.py +++ b/tests/components/light/test_hue.py @@ -68,7 +68,7 @@ class TestSetup(unittest.TestCase): """Return a dict suitable for mocking api.get('lights').""" mock_bridge_lights = lights - for light_id, info in mock_bridge_lights.items(): + for info in mock_bridge_lights.values(): if 'state' not in info: info['state'] = {'on': False} diff --git a/tests/components/test_canary.py b/tests/components/test_canary.py index 67122813fb7..2c496c26e11 100644 --- a/tests/components/test_canary.py +++ b/tests/components/test_canary.py @@ -17,12 +17,12 @@ def mock_device(device_id, name, is_online=True): return device -def mock_location(name, is_celsius=True, devices=[]): +def mock_location(name, is_celsius=True, devices=None): """Mock Canary Location class.""" location = MagicMock() type(location).name = PropertyMock(return_value=name) type(location).is_celsius = PropertyMock(return_value=is_celsius) - type(location).devices = PropertyMock(return_value=devices) + type(location).devices = PropertyMock(return_value=devices or []) return location diff --git a/tests/components/test_influxdb.py b/tests/components/test_influxdb.py index d768136592e..0ec5f973ee4 100644 --- a/tests/components/test_influxdb.py +++ b/tests/components/test_influxdb.py @@ -733,7 +733,7 @@ class TestRetryOnErrorDecorator(unittest.TestCase): self.assertEqual(mock_method.call_count, 2) mock_method.assert_called_with(1, 2, test=3) - for cnt in range(3): + for _ in range(3): start = dt_util.utcnow() shifted_time = start + (timedelta(seconds=20 + 1)) self.hass.bus.fire(ha.EVENT_TIME_CHANGED, diff --git a/tests/test_core.py b/tests/test_core.py index 90a72a48a10..77a7872526f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -135,7 +135,7 @@ class TestHomeAssistant(unittest.TestCase): """Test Coro.""" call_count.append('call') - for i in range(3): + for _ in range(3): self.hass.add_job(test_coro()) run_coroutine_threadsafe( @@ -155,7 +155,7 @@ class TestHomeAssistant(unittest.TestCase): """Test Coro.""" call_count.append('call') - for i in range(2): + for _ in range(2): self.hass.add_job(test_coro()) @asyncio.coroutine @@ -185,7 +185,7 @@ class TestHomeAssistant(unittest.TestCase): yield from asyncio.sleep(0, loop=self.hass.loop) yield from asyncio.sleep(0, loop=self.hass.loop) - for i in range(2): + for _ in range(2): self.hass.add_job(test_executor) run_coroutine_threadsafe( @@ -210,7 +210,7 @@ class TestHomeAssistant(unittest.TestCase): yield from asyncio.sleep(0, loop=self.hass.loop) yield from asyncio.sleep(0, loop=self.hass.loop) - for i in range(2): + for _ in range(2): self.hass.add_job(test_callback) run_coroutine_threadsafe( diff --git a/tests/test_util/aiohttp.py b/tests/test_util/aiohttp.py index d11a71d541f..d7033775a14 100644 --- a/tests/test_util/aiohttp.py +++ b/tests/test_util/aiohttp.py @@ -97,7 +97,7 @@ class AiohttpClientMockResponse: """Mock Aiohttp client response.""" def __init__(self, method, url, status, response, cookies=None, exc=None, - headers={}): + headers=None): """Initialize a fake response.""" self.method = method self._url = url @@ -107,7 +107,7 @@ class AiohttpClientMockResponse: self.response = response self.exc = exc - self._headers = headers + self._headers = headers or {} self._cookies = {} if cookies: