mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Add python_script to strict typing (#134822)
This commit is contained in:
parent
bf0cf1c30f
commit
bc22e34fc3
@ -383,6 +383,7 @@ homeassistant.components.pure_energie.*
|
|||||||
homeassistant.components.purpleair.*
|
homeassistant.components.purpleair.*
|
||||||
homeassistant.components.pushbullet.*
|
homeassistant.components.pushbullet.*
|
||||||
homeassistant.components.pvoutput.*
|
homeassistant.components.pvoutput.*
|
||||||
|
homeassistant.components.python_script.*
|
||||||
homeassistant.components.qnap_qsw.*
|
homeassistant.components.qnap_qsw.*
|
||||||
homeassistant.components.rabbitair.*
|
homeassistant.components.rabbitair.*
|
||||||
homeassistant.components.radarr.*
|
homeassistant.components.radarr.*
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"""Component to allow running Python scripts."""
|
"""Component to allow running Python scripts."""
|
||||||
|
|
||||||
from collections.abc import Mapping, Sequence
|
from collections.abc import Callable, Mapping, Sequence
|
||||||
import datetime
|
import datetime
|
||||||
import glob
|
import glob
|
||||||
import logging
|
import logging
|
||||||
@ -197,7 +197,12 @@ def guarded_inplacevar(op: str, target: Any, operand: Any) -> Any:
|
|||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def execute_script(hass, name, data=None, return_response=False):
|
def execute_script(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
name: str,
|
||||||
|
data: dict[str, Any] | None = None,
|
||||||
|
return_response: bool = False,
|
||||||
|
) -> dict | None:
|
||||||
"""Execute a script."""
|
"""Execute a script."""
|
||||||
filename = f"{name}.py"
|
filename = f"{name}.py"
|
||||||
raise_if_invalid_filename(filename)
|
raise_if_invalid_filename(filename)
|
||||||
@ -207,7 +212,13 @@ def execute_script(hass, name, data=None, return_response=False):
|
|||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
def execute(hass, filename, source, data=None, return_response=False):
|
def execute(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
filename: str,
|
||||||
|
source: Any,
|
||||||
|
data: dict[str, Any] | None = None,
|
||||||
|
return_response: bool = False,
|
||||||
|
) -> dict | None:
|
||||||
"""Execute Python source."""
|
"""Execute Python source."""
|
||||||
|
|
||||||
compiled = compile_restricted_exec(source, filename=filename)
|
compiled = compile_restricted_exec(source, filename=filename)
|
||||||
@ -223,7 +234,7 @@ def execute(hass, filename, source, data=None, return_response=False):
|
|||||||
"Warning loading script %s: %s", filename, ", ".join(compiled.warnings)
|
"Warning loading script %s: %s", filename, ", ".join(compiled.warnings)
|
||||||
)
|
)
|
||||||
|
|
||||||
def protected_getattr(obj, name, default=None):
|
def protected_getattr(obj: object, name: str, default: Any = None) -> Any:
|
||||||
"""Restricted method to get attributes."""
|
"""Restricted method to get attributes."""
|
||||||
if name.startswith("async_"):
|
if name.startswith("async_"):
|
||||||
raise ScriptError("Not allowed to access async methods")
|
raise ScriptError("Not allowed to access async methods")
|
||||||
@ -316,10 +327,10 @@ def execute(hass, filename, source, data=None, return_response=False):
|
|||||||
class StubPrinter:
|
class StubPrinter:
|
||||||
"""Class to handle printing inside scripts."""
|
"""Class to handle printing inside scripts."""
|
||||||
|
|
||||||
def __init__(self, _getattr_):
|
def __init__(self, _getattr_: Callable) -> None:
|
||||||
"""Initialize our printer."""
|
"""Initialize our printer."""
|
||||||
|
|
||||||
def _call_print(self, *objects, **kwargs):
|
def _call_print(self, *objects: object, **kwargs: Any) -> None:
|
||||||
"""Print text."""
|
"""Print text."""
|
||||||
_LOGGER.warning("Don't use print() inside scripts. Use logger.info() instead")
|
_LOGGER.warning("Don't use print() inside scripts. Use logger.info() instead")
|
||||||
|
|
||||||
@ -330,7 +341,7 @@ class TimeWrapper:
|
|||||||
# Class variable, only going to warn once per Home Assistant run
|
# Class variable, only going to warn once per Home Assistant run
|
||||||
warned = False
|
warned = False
|
||||||
|
|
||||||
def sleep(self, *args, **kwargs):
|
def sleep(self, *args: Any, **kwargs: Any) -> None:
|
||||||
"""Sleep method that warns once."""
|
"""Sleep method that warns once."""
|
||||||
if not TimeWrapper.warned:
|
if not TimeWrapper.warned:
|
||||||
TimeWrapper.warned = True
|
TimeWrapper.warned = True
|
||||||
@ -340,12 +351,12 @@ class TimeWrapper:
|
|||||||
|
|
||||||
time.sleep(*args, **kwargs)
|
time.sleep(*args, **kwargs)
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr: str) -> Any:
|
||||||
"""Fetch an attribute from Time module."""
|
"""Fetch an attribute from Time module."""
|
||||||
attribute = getattr(time, attr)
|
attribute = getattr(time, attr)
|
||||||
if callable(attribute):
|
if callable(attribute):
|
||||||
|
|
||||||
def wrapper(*args, **kw):
|
def wrapper(*args: Any, **kw: Any) -> Any:
|
||||||
"""Wrap to return callable method if callable."""
|
"""Wrap to return callable method if callable."""
|
||||||
return attribute(*args, **kw)
|
return attribute(*args, **kw)
|
||||||
|
|
||||||
|
10
mypy.ini
10
mypy.ini
@ -3586,6 +3586,16 @@ disallow_untyped_defs = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = true
|
warn_unreachable = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.components.python_script.*]
|
||||||
|
check_untyped_defs = true
|
||||||
|
disallow_incomplete_defs = true
|
||||||
|
disallow_subclassing_any = true
|
||||||
|
disallow_untyped_calls = true
|
||||||
|
disallow_untyped_decorators = true
|
||||||
|
disallow_untyped_defs = true
|
||||||
|
warn_return_any = true
|
||||||
|
warn_unreachable = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.qnap_qsw.*]
|
[mypy-homeassistant.components.qnap_qsw.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user