From 09ec3bf24fbcda4e344508094d158ac82e0a18e0 Mon Sep 17 00:00:00 2001 From: Xavier Moreno Date: Sun, 18 Oct 2020 13:05:49 +0200 Subject: [PATCH] refactor(type-hints): fixing type hint issues from pylance --- .../cx_core/feature_support/__init__.py | 8 +++--- .../cx_core/feature_support/cover.py | 8 ++---- .../cx_core/feature_support/light.py | 8 ++---- .../cx_core/feature_support/media_player.py | 8 ++---- .../cx_core/integration/__init__.py | 9 ++++--- tests/integ_tests/integ_test.py | 4 ++- .../cx_core/custom_controller_test.py | 2 +- .../feature_support/cover_support_test.py | 2 +- .../feature_support/feature_support_test.py | 4 +-- .../feature_support/light_support_test.py | 2 +- .../media_player_support_test.py | 2 +- .../cx_core/release_hold_controller_test.py | 2 +- .../cx_core/type/cover_controller_test.py | 2 +- .../cx_core/type/light_controller_test.py | 25 +++++++++++-------- .../type/media_player_controller_test.py | 2 +- .../cx_core/type/switch_controller_test.py | 2 +- .../cx_core/type_controller_test.py | 2 +- tests/unit_tests/cx_devices/aqara_test.py | 4 +-- tests/unit_tests/cx_devices/phillips_test.py | 2 +- 19 files changed, 48 insertions(+), 50 deletions(-) diff --git a/apps/controllerx/cx_core/feature_support/__init__.py b/apps/controllerx/cx_core/feature_support/__init__.py index f5295f4e..ce62f567 100644 --- a/apps/controllerx/cx_core/feature_support/__init__.py +++ b/apps/controllerx/cx_core/feature_support/__init__.py @@ -1,6 +1,6 @@ -from typing import List, Optional, Set, Union +from typing import List, Set, Union -from cx_core.controller import Controller +from cx_core.controller import TypeController SupportedFeatureNumber = Union[int, str] Features = List[int] @@ -21,8 +21,8 @@ def decode(number: int, features: Features) -> SupportedFeatures: def __init__( self, - entity: Optional[str], - controller: Optional[Controller], + entity: str, + controller: TypeController, features: Features, update_supported_features: bool, ) -> None: diff --git a/apps/controllerx/cx_core/feature_support/cover.py b/apps/controllerx/cx_core/feature_support/cover.py index 1b695109..1fc514a6 100644 --- a/apps/controllerx/cx_core/feature_support/cover.py +++ b/apps/controllerx/cx_core/feature_support/cover.py @@ -1,5 +1,4 @@ -from typing import Optional -from cx_core.controller import Controller +from cx_core.controller import TypeController from cx_core.feature_support import FeatureSupport SUPPORT_OPEN = 1 @@ -24,10 +23,7 @@ class CoverSupport(FeatureSupport): SET_TILT_POSITION = 128 def __init__( - self, - entity: Optional[str], - controller: Optional[Controller], - update_supported_features: bool, + self, entity: str, controller: TypeController, update_supported_features: bool, ) -> None: super().__init__( entity, diff --git a/apps/controllerx/cx_core/feature_support/light.py b/apps/controllerx/cx_core/feature_support/light.py index ebdbeafd..719b7f20 100644 --- a/apps/controllerx/cx_core/feature_support/light.py +++ b/apps/controllerx/cx_core/feature_support/light.py @@ -1,5 +1,4 @@ -from typing import Optional -from cx_core.controller import Controller +from cx_core.controller import TypeController from cx_core.feature_support import FeatureSupport @@ -13,10 +12,7 @@ class LightSupport(FeatureSupport): WHITE_VALUE = 128 def __init__( - self, - entity: Optional[str], - controller: Optional[Controller], - update_supported_features: bool, + self, entity: str, controller: TypeController, update_supported_features: bool, ) -> None: super().__init__( entity, diff --git a/apps/controllerx/cx_core/feature_support/media_player.py b/apps/controllerx/cx_core/feature_support/media_player.py index 77aed630..49e6531e 100644 --- a/apps/controllerx/cx_core/feature_support/media_player.py +++ b/apps/controllerx/cx_core/feature_support/media_player.py @@ -1,5 +1,4 @@ -from typing import Optional -from cx_core.controller import Controller +from cx_core.controller import TypeController from cx_core.feature_support import FeatureSupport @@ -22,10 +21,7 @@ class MediaPlayerSupport(FeatureSupport): SELECT_SOUND_MODE = 65536 def __init__( - self, - entity: Optional[str], - controller: Optional[Controller], - update_supported_features: bool, + self, entity: str, controller: TypeController, update_supported_features: bool, ) -> None: super().__init__( entity, diff --git a/apps/controllerx/cx_core/integration/__init__.py b/apps/controllerx/cx_core/integration/__init__.py index cf1998f3..6fdab355 100644 --- a/apps/controllerx/cx_core/integration/__init__.py +++ b/apps/controllerx/cx_core/integration/__init__.py @@ -2,13 +2,16 @@ import importlib import os import pkgutil -from typing import Any, Dict, List, NewType, Optional, Type, Union +from typing import TYPE_CHECKING, Any, Dict, List, NewType, Optional, Type, Union from cx_const import TypeActionsMapping +if TYPE_CHECKING: + from cx_core.controller import Controller + class Integration(abc.ABC): - def __init__(self, controller, kwargs: Dict[str, Any]): + def __init__(self, controller: "Controller", kwargs: Dict[str, Any]): self.name = self.get_name() self.controller = controller self.kwargs = kwargs @@ -41,7 +44,7 @@ def _all_integration_subclasses( subclasses = set(cls_.__subclasses__()).union( [s for c in cls_.__subclasses__() for s in _all_integration_subclasses(c)] ) - return list(subclasses) + return list(subclasses) # type: ignore def get_integrations(controller, kwargs) -> List[Integration]: diff --git a/tests/integ_tests/integ_test.py b/tests/integ_tests/integ_test.py index 1ddc095a..ffa9a6e0 100644 --- a/tests/integ_tests/integ_test.py +++ b/tests/integ_tests/integ_test.py @@ -65,7 +65,9 @@ async def test_integ_configs(hass_mock, mocker, config_file, data): pending = asyncio.Task.all_tasks() # We exclude the current function we are executing - pending = {task for task in pending if task._coro.__name__ != "test_integ_configs"} + pending = { + task for task in pending if task._coro.__name__ != "test_integ_configs" # type: ignore + } if pending: # Finish pending tasks if any await asyncio.wait(pending) assert call_service_stub.call_count == expected_calls_count diff --git a/tests/unit_tests/cx_core/custom_controller_test.py b/tests/unit_tests/cx_core/custom_controller_test.py index bfe14a9f..b25eaabb 100644 --- a/tests/unit_tests/cx_core/custom_controller_test.py +++ b/tests/unit_tests/cx_core/custom_controller_test.py @@ -134,7 +134,7 @@ async def test_custom_controllers( async def test_call_service_controller( hass_mock, monkeypatch, mocker, integration, services, expected_calls, ): - sut = CallServiceController() + sut = CallServiceController() # type: ignore sut.args = { "controller": "test_controller", "integration": integration, diff --git a/tests/unit_tests/cx_core/feature_support/cover_support_test.py b/tests/unit_tests/cx_core/feature_support/cover_support_test.py index d606b918..7a030ec1 100644 --- a/tests/unit_tests/cx_core/feature_support/cover_support_test.py +++ b/tests/unit_tests/cx_core/feature_support/cover_support_test.py @@ -29,7 +29,7 @@ ], ) def test_init(number, expected_supported_features): - cover_support = CoverSupport(None, None, False) + cover_support = CoverSupport("fake_entity", None, False) # type: ignore cover_support._supported_features = FeatureSupport.decode( number, cover_support.features ) diff --git a/tests/unit_tests/cx_core/feature_support/feature_support_test.py b/tests/unit_tests/cx_core/feature_support/feature_support_test.py index b9ec1ed5..6419e6ed 100644 --- a/tests/unit_tests/cx_core/feature_support/feature_support_test.py +++ b/tests/unit_tests/cx_core/feature_support/feature_support_test.py @@ -43,7 +43,7 @@ def test_encode(supported_features, expected_number): ) @pytest.mark.asyncio async def test_is_supported(number, features, feature, is_supported): - feature_support = FeatureSupport(None, None, features, False) + feature_support = FeatureSupport("fake_entity", None, features, False) # type: ignore feature_support._supported_features = FeatureSupport.decode(number, features) is_supported = await feature_support.is_supported(feature) assert is_supported == is_supported @@ -61,7 +61,7 @@ async def test_is_supported(number, features, feature, is_supported): ) @pytest.mark.asyncio async def test_not_supported(number, features, feature, is_supported): - feature_support = FeatureSupport(None, None, features, False) + feature_support = FeatureSupport("fake_entity", None, features, False) # type: ignore feature_support._supported_features = FeatureSupport.decode(number, features) is_supported = await feature_support.not_supported(feature) assert is_supported == is_supported diff --git a/tests/unit_tests/cx_core/feature_support/light_support_test.py b/tests/unit_tests/cx_core/feature_support/light_support_test.py index 19d73397..81737e9e 100644 --- a/tests/unit_tests/cx_core/feature_support/light_support_test.py +++ b/tests/unit_tests/cx_core/feature_support/light_support_test.py @@ -30,7 +30,7 @@ ], ) def test_init(number, expected_supported_features): - light_support = LightSupport(None, None, False) + light_support = LightSupport("fake_entity", None, False) # type: ignore light_support._supported_features = FeatureSupport.decode( number, light_support.features ) diff --git a/tests/unit_tests/cx_core/feature_support/media_player_support_test.py b/tests/unit_tests/cx_core/feature_support/media_player_support_test.py index 2f10ca4a..29f29d3e 100644 --- a/tests/unit_tests/cx_core/feature_support/media_player_support_test.py +++ b/tests/unit_tests/cx_core/feature_support/media_player_support_test.py @@ -31,7 +31,7 @@ ], ) def test_init(number, expected_supported_features): - media_player_support = MediaPlayerSupport(None, None, False) + media_player_support = MediaPlayerSupport("fake_entity", None, False) # type: ignore media_player_support._supported_features = FeatureSupport.decode( number, media_player_support.features ) diff --git a/tests/unit_tests/cx_core/release_hold_controller_test.py b/tests/unit_tests/cx_core/release_hold_controller_test.py index e111f865..4c464558 100644 --- a/tests/unit_tests/cx_core/release_hold_controller_test.py +++ b/tests/unit_tests/cx_core/release_hold_controller_test.py @@ -11,7 +11,7 @@ def hold_loop(self): @pytest.fixture def sut(hass_mock): - c = FakeReleaseHoldController() + c = FakeReleaseHoldController() # type: ignore c.args = {} c.delay = 0 c.hold_release_toggle = False diff --git a/tests/unit_tests/cx_core/type/cover_controller_test.py b/tests/unit_tests/cx_core/type/cover_controller_test.py index d30421bb..4693197f 100644 --- a/tests/unit_tests/cx_core/type/cover_controller_test.py +++ b/tests/unit_tests/cx_core/type/cover_controller_test.py @@ -9,7 +9,7 @@ @pytest.fixture @pytest.mark.asyncio async def sut(hass_mock, mocker): - c = CoverController() + c = CoverController() # type: ignore mocker.patch.object(TypeController, "initialize") c.cover = "cover.test" c.open_position = 100 diff --git a/tests/unit_tests/cx_core/type/light_controller_test.py b/tests/unit_tests/cx_core/type/light_controller_test.py index 1053fabf..0e49b34e 100644 --- a/tests/unit_tests/cx_core/type/light_controller_test.py +++ b/tests/unit_tests/cx_core/type/light_controller_test.py @@ -11,7 +11,7 @@ @pytest.fixture def sut(hass_mock, monkeypatch): - c = LightController() + c = LightController() # type: ignore c.args = {} c.delay = 0 c.light = {"name": "light"} @@ -101,7 +101,7 @@ async def fake_super_initialize(self): ) @pytest.mark.asyncio async def test_get_attribute( - sut, + sut: LightController, monkeypatch, attribute_input, color_mode, @@ -109,7 +109,7 @@ async def test_get_attribute( attribute_expected, throws_error, ): - sut.supported_features = LightSupport(None, None, False) + sut.supported_features = LightSupport("fake_entity", sut, False) sut.supported_features._supported_features = supported_features sut.light = {"name": "light", "color_mode": color_mode} @@ -208,7 +208,7 @@ async def fake_get_entity_state(entity, attribute=None): ) @pytest.mark.asyncio async def test_change_light_state( - sut, + sut: LightController, mocker, monkeypatch, old, @@ -231,7 +231,7 @@ async def fake_get_entity_state(*args, **kwargs): sut.transition = 300 sut.add_transition = True sut.add_transition_turn_toggle = False - sut.supported_features = LightSupport(None, None, False) + sut.supported_features = LightSupport("fake_entity", sut, False) sut.supported_features._supported_features = set() sut.color_wheel = get_color_wheel("default_color_wheel") @@ -287,7 +287,7 @@ async def fake_get_entity_state(*args, **kwargs): ) @pytest.mark.asyncio async def test_call_light_service( - sut, + sut: LightController, mocker, attributes_input, transition_support, @@ -301,7 +301,7 @@ async def test_call_light_service( sut.add_transition = add_transition sut.add_transition_turn_toggle = add_transition_turn_toggle supported_features = {LightSupport.TRANSITION} if transition_support else set() - sut.supported_features = LightSupport(None, None, False) + sut.supported_features = LightSupport("fake_entity", sut, False) sut.supported_features._supported_features = supported_features await sut.call_light_service( "test_service", turned_toggle=turned_toggle, **attributes_input @@ -472,15 +472,20 @@ async def test_on_min(sut, mocker): ) @pytest.mark.asyncio async def test_sync( - sut, monkeypatch, mocker, max_brightness, color_attribute, expected_attributes + sut: LightController, + monkeypatch, + mocker, + max_brightness, + color_attribute, + expected_attributes, ): sut.max_brightness = max_brightness sut.light = {"name": "test_light"} sut.transition = 300 sut.add_transition = True sut.add_transition_turn_toggle = True - sut.supported_features = LightSupport(None, None, False) - sut.supported_features._supported_features = [LightSupport.TRANSITION] + sut.supported_features = LightSupport("fake_entity", sut, False) + sut.supported_features._supported_features = {LightSupport.TRANSITION} async def fake_get_attribute(*args, **kwargs): if color_attribute == "error": diff --git a/tests/unit_tests/cx_core/type/media_player_controller_test.py b/tests/unit_tests/cx_core/type/media_player_controller_test.py index 3a2ffc4f..a7a37d2a 100644 --- a/tests/unit_tests/cx_core/type/media_player_controller_test.py +++ b/tests/unit_tests/cx_core/type/media_player_controller_test.py @@ -9,7 +9,7 @@ @pytest.fixture @pytest.mark.asyncio async def sut(monkeypatch, hass_mock, mocker): - c = MediaPlayerController() + c = MediaPlayerController() # type: ignore c.args = {} c.delay = 0 c.media_player = "test" diff --git a/tests/unit_tests/cx_core/type/switch_controller_test.py b/tests/unit_tests/cx_core/type/switch_controller_test.py index a6dd07f6..f0fa5a99 100644 --- a/tests/unit_tests/cx_core/type/switch_controller_test.py +++ b/tests/unit_tests/cx_core/type/switch_controller_test.py @@ -7,7 +7,7 @@ @pytest.fixture @pytest.mark.asyncio async def sut(hass_mock, mocker): - c = SwitchController() + c = SwitchController() # type: ignore mocker.patch.object(TypeController, "initialize") c.args = {"switch": "switch.test"} await c.initialize() diff --git a/tests/unit_tests/cx_core/type_controller_test.py b/tests/unit_tests/cx_core/type_controller_test.py index e53d807b..7542eb82 100644 --- a/tests/unit_tests/cx_core/type_controller_test.py +++ b/tests/unit_tests/cx_core/type_controller_test.py @@ -10,7 +10,7 @@ def get_domain(self): @pytest.fixture def sut(hass_mock): - c = FakeTypeController() + c = FakeTypeController() # type: ignore c.args = {} return c diff --git a/tests/unit_tests/cx_devices/aqara_test.py b/tests/unit_tests/cx_devices/aqara_test.py index 087f7423..fe7a05af 100644 --- a/tests/unit_tests/cx_devices/aqara_test.py +++ b/tests/unit_tests/cx_devices/aqara_test.py @@ -15,7 +15,7 @@ ], ) def test_zha_action_MFKZQ01LMLightController(data, expected_action): - sut = MFKZQ01LMLightController() + sut = MFKZQ01LMLightController() # type: ignore action = sut.get_zha_action(data) assert action == expected_action @@ -31,6 +31,6 @@ def test_zha_action_MFKZQ01LMLightController(data, expected_action): ], ) def test_zha_action_WXKG01LMLightController(data, expected_action): - sut = WXKG01LMLightController() + sut = WXKG01LMLightController() # type: ignore action = sut.get_zha_action(data) assert action == expected_action diff --git a/tests/unit_tests/cx_devices/phillips_test.py b/tests/unit_tests/cx_devices/phillips_test.py index 35a0c1c9..de8b84a6 100644 --- a/tests/unit_tests/cx_devices/phillips_test.py +++ b/tests/unit_tests/cx_devices/phillips_test.py @@ -11,6 +11,6 @@ ], ) def test_zha_action_HueDimmerController(data, expected_action): - sut = HueDimmerController() + sut = HueDimmerController() # type: ignore action = sut.get_zha_action(data) assert action == expected_action