Skip to content

Commit

Permalink
refactor(type-hints): fixing type hint issues from pylance
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Oct 18, 2020
1 parent f6f49d9 commit 09ec3bf
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 50 deletions.
8 changes: 4 additions & 4 deletions apps/controllerx/cx_core/feature_support/__init__.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions apps/controllerx/cx_core/feature_support/cover.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions apps/controllerx/cx_core/feature_support/light.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions apps/controllerx/cx_core/feature_support/media_player.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions apps/controllerx/cx_core/integration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 3 additions & 1 deletion tests/integ_tests/integ_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_core/custom_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_core/release_hold_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_core/type/cover_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 15 additions & 10 deletions tests/unit_tests/cx_core/type/light_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -101,15 +101,15 @@ async def fake_super_initialize(self):
)
@pytest.mark.asyncio
async def test_get_attribute(
sut,
sut: LightController,
monkeypatch,
attribute_input,
color_mode,
supported_features,
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}

Expand Down Expand Up @@ -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,
Expand All @@ -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")

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_core/type/switch_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_core/type_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_domain(self):

@pytest.fixture
def sut(hass_mock):
c = FakeTypeController()
c = FakeTypeController() # type: ignore
c.args = {}
return c

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/cx_devices/aqara_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion tests/unit_tests/cx_devices/phillips_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 09ec3bf

Please sign in to comment.