Use loader.async_suggest_report_issue in deprecation helper (#101393)

This commit is contained in:
Erik Montnemery 2023-10-06 09:51:08 +02:00 committed by GitHub
parent 60fa63a1f0
commit 3478666973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -2,12 +2,17 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from contextlib import suppress
import functools import functools
import inspect import inspect
import logging import logging
from typing import Any, ParamSpec, TypeVar from typing import Any, ParamSpec, TypeVar
from ..helpers.frame import MissingIntegrationFrame, get_integration_frame from homeassistant.core import HomeAssistant, async_get_hass
from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import async_suggest_report_issue
from .frame import MissingIntegrationFrame, get_integration_frame
_ObjectT = TypeVar("_ObjectT", bound=object) _ObjectT = TypeVar("_ObjectT", bound=object)
_R = TypeVar("_R") _R = TypeVar("_R")
@ -134,16 +139,24 @@ def _print_deprecation_warning(obj: Any, replacement: str, description: str) ->
try: try:
integration_frame = get_integration_frame() integration_frame = get_integration_frame()
if integration_frame.custom_integration: if integration_frame.custom_integration:
hass: HomeAssistant | None = None
with suppress(HomeAssistantError):
hass = async_get_hass()
report_issue = async_suggest_report_issue(
hass,
integration_domain=integration_frame.integration,
module=integration_frame.module,
)
logger.warning( logger.warning(
( (
"%s was called from %s, this is a deprecated %s. Use %s instead," "%s was called from %s, this is a deprecated %s. Use %s instead,"
" please report this to the maintainer of %s" " please %s"
), ),
obj.__name__, obj.__name__,
integration_frame.integration, integration_frame.integration,
description, description,
replacement, replacement,
integration_frame.integration, report_issue,
) )
else: else:
logger.warning( logger.warning(

View File

@ -3,6 +3,7 @@ from unittest.mock import MagicMock, Mock, patch
import pytest import pytest
from homeassistant.core import HomeAssistant
from homeassistant.helpers.deprecation import ( from homeassistant.helpers.deprecation import (
deprecated_class, deprecated_class,
deprecated_function, deprecated_function,
@ -10,6 +11,8 @@ from homeassistant.helpers.deprecation import (
get_deprecated, get_deprecated,
) )
from tests.common import MockModule, mock_integration
class MockBaseClassDeprecatedProperty: class MockBaseClassDeprecatedProperty:
"""Mock base class for deprecated testing.""" """Mock base class for deprecated testing."""
@ -173,6 +176,7 @@ def test_deprecated_function_called_from_built_in_integration(
def test_deprecated_function_called_from_custom_integration( def test_deprecated_function_called_from_custom_integration(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test deprecated_function decorator. """Test deprecated_function decorator.
@ -180,6 +184,8 @@ def test_deprecated_function_called_from_custom_integration(
This tests the behavior when the calling integration is custom. This tests the behavior when the calling integration is custom.
""" """
mock_integration(hass, MockModule("hue"), built_in=False)
@deprecated_function("new_function") @deprecated_function("new_function")
def mock_deprecated_function(): def mock_deprecated_function():
pass pass
@ -207,6 +213,6 @@ def test_deprecated_function_called_from_custom_integration(
mock_deprecated_function() mock_deprecated_function()
assert ( assert (
"mock_deprecated_function was called from hue, this is a deprecated function. " "mock_deprecated_function was called from hue, this is a deprecated function. "
"Use new_function instead, please report this to the maintainer of hue" "Use new_function instead, please report it to the author of the 'hue' custom "
in caplog.text "integration" in caplog.text
) )