Allow float for int argument type [pylint plugin] (#114105)

This commit is contained in:
Marc Mueller 2024-03-27 11:51:27 +01:00 committed by GitHub
parent 5c97049f2e
commit 3929273b41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 5 deletions

View File

@ -159,7 +159,7 @@ class XiaomiGenericHumidifier(XiaomiCoordinatedMiioEntity, HumidifierEntity):
self._state = False self._state = False
self.async_write_ha_state() self.async_write_ha_state()
def translate_humidity(self, humidity): def translate_humidity(self, humidity: float) -> float | None:
"""Translate the target humidity to the first valid step.""" """Translate the target humidity to the first valid step."""
return ( return (
math.ceil(percentage_to_ranged_value((1, self._humidity_steps), humidity)) math.ceil(percentage_to_ranged_value((1, self._humidity_steps), humidity))
@ -240,7 +240,7 @@ class XiaomiAirHumidifier(XiaomiGenericHumidifier, HumidifierEntity):
else None else None
) )
async def async_set_humidity(self, humidity: int) -> None: async def async_set_humidity(self, humidity: float) -> None:
"""Set the target humidity of the humidifier and set the mode to auto.""" """Set the target humidity of the humidifier and set the mode to auto."""
target_humidity = self.translate_humidity(humidity) target_humidity = self.translate_humidity(humidity)
if not target_humidity: if not target_humidity:
@ -318,7 +318,7 @@ class XiaomiAirHumidifierMiot(XiaomiAirHumidifier):
) )
return None return None
async def async_set_humidity(self, humidity: int) -> None: async def async_set_humidity(self, humidity: float) -> None:
"""Set the target humidity of the humidifier and set the mode to auto.""" """Set the target humidity of the humidifier and set the mode to auto."""
target_humidity = self.translate_humidity(humidity) target_humidity = self.translate_humidity(humidity)
if not target_humidity: if not target_humidity:
@ -393,7 +393,7 @@ class XiaomiAirHumidifierMjjsq(XiaomiAirHumidifier):
return self._target_humidity return self._target_humidity
return None return None
async def async_set_humidity(self, humidity: int) -> None: async def async_set_humidity(self, humidity: float) -> None:
"""Set the target humidity of the humidifier and set the mode to Humidity.""" """Set the target humidity of the humidifier and set the mode to Humidity."""
target_humidity = self.translate_humidity(humidity) target_humidity = self.translate_humidity(humidity)
if not target_humidity: if not target_humidity:

View File

@ -81,7 +81,7 @@ def ranged_value_to_percentage(
def percentage_to_ranged_value( def percentage_to_ranged_value(
low_high_range: tuple[float, float], percentage: int low_high_range: tuple[float, float], percentage: float
) -> float: ) -> float:
"""Given a range of low and high values convert a percentage to a single value. """Given a range of low and high values convert a percentage to a single value.

View File

@ -2968,6 +2968,15 @@ def _is_valid_type(
): ):
return True return True
# Special case for int in argument type
if (
expected_type == "int"
and not in_return
and isinstance(node, nodes.Name)
and node.name in ("float", "int")
):
return True
# Name occurs when a namespace is not used, eg. "HomeAssistant" # Name occurs when a namespace is not used, eg. "HomeAssistant"
if isinstance(node, nodes.Name) and node.name == expected_type: if isinstance(node, nodes.Name) and node.name == expected_type:
return True return True

View File

@ -989,6 +989,39 @@ def test_media_player_entity(
type_hint_checker.visit_classdef(class_node) type_hint_checker.visit_classdef(class_node)
def test_humidifier_entity(
linter: UnittestLinter, type_hint_checker: BaseChecker
) -> None:
"""Ensure valid hints are accepted for humidifier entity."""
# Set bypass option
type_hint_checker.linter.config.ignore_missing_annotations = False
# Ensure that `float` and `int` are valid for `int` argument type
class_node = astroid.extract_node(
"""
class Entity():
pass
class HumidifierEntity(Entity):
pass
class MyHumidifier( #@
HumidifierEntity
):
def set_humidity(self, humidity: int) -> None:
pass
def async_set_humidity(self, humidity: float) -> None:
pass
""",
"homeassistant.components.pylint_test.humidifier",
)
type_hint_checker.visit_module(class_node.parent)
with assert_no_messages(linter):
type_hint_checker.visit_classdef(class_node)
def test_number_entity(linter: UnittestLinter, type_hint_checker: BaseChecker) -> None: def test_number_entity(linter: UnittestLinter, type_hint_checker: BaseChecker) -> None:
"""Ensure valid hints are accepted for number entity.""" """Ensure valid hints are accepted for number entity."""
# Set bypass option # Set bypass option