mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Reduce false-positives in test-before-setup IQS check (#133349)
This commit is contained in:
parent
4b3893eadf
commit
cd2cc1d99f
@ -15,13 +15,31 @@ _VALID_EXCEPTIONS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _raises_exception(async_setup_entry_function: ast.AsyncFunctionDef) -> bool:
|
def _get_exception_name(expression: ast.expr) -> str:
|
||||||
"""Check that a valid exception is raised within `async_setup_entry`."""
|
"""Get the name of the exception being raised."""
|
||||||
for node in ast.walk(async_setup_entry_function):
|
if isinstance(expression, ast.Name):
|
||||||
if isinstance(node, ast.Raise):
|
return expression.id
|
||||||
if isinstance(node.exc, ast.Name) and node.exc.id in _VALID_EXCEPTIONS:
|
|
||||||
return True
|
if isinstance(expression, ast.Call):
|
||||||
if isinstance(node.exc, ast.Call) and node.exc.func.id in _VALID_EXCEPTIONS:
|
return _get_exception_name(expression.func)
|
||||||
|
|
||||||
|
if isinstance(expression, ast.Attribute):
|
||||||
|
return _get_exception_name(expression.value)
|
||||||
|
|
||||||
|
raise AssertionError(
|
||||||
|
f"Raise is neither Attribute nor Call nor Name: {type(expression)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _raises_exception(integration: Integration) -> bool:
|
||||||
|
"""Check that a valid exception is raised."""
|
||||||
|
for module_file in integration.path.rglob("*.py"):
|
||||||
|
module = ast_parse_module(module_file)
|
||||||
|
for node in ast.walk(module):
|
||||||
|
if (
|
||||||
|
isinstance(node, ast.Raise)
|
||||||
|
and _get_exception_name(node.exc) in _VALID_EXCEPTIONS
|
||||||
|
):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -59,11 +77,6 @@ def validate(
|
|||||||
if not (async_setup_entry := _get_setup_entry_function(init)):
|
if not (async_setup_entry := _get_setup_entry_function(init)):
|
||||||
return [f"Could not find `async_setup_entry` in {init_file}"]
|
return [f"Could not find `async_setup_entry` in {init_file}"]
|
||||||
|
|
||||||
if not (
|
if not (_calls_first_refresh(async_setup_entry) or _raises_exception(integration)):
|
||||||
_raises_exception(async_setup_entry) or _calls_first_refresh(async_setup_entry)
|
return [f"Integration does not raise one of {_VALID_EXCEPTIONS}"]
|
||||||
):
|
|
||||||
return [
|
|
||||||
f"Integration does not raise one of {_VALID_EXCEPTIONS} "
|
|
||||||
f"in async_setup_entry ({init_file})"
|
|
||||||
]
|
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user