Allow importing typing helper in core files (#119377)

* Allow importing typing helper in core files

* Really fix the circular import

* Update test
This commit is contained in:
Erik Montnemery
2024-06-11 13:48:12 +02:00
committed by GitHub
parent 572700a326
commit 904b89df80
5 changed files with 69 additions and 44 deletions

View File

@ -5,10 +5,8 @@ from enum import Enum
from functools import partial
from typing import Any, Never
import homeassistant.core
from .deprecation import (
DeprecatedAlias,
DeferredDeprecatedAlias,
all_with_deprecated_constants,
check_if_deprecated_constant,
dir_with_deprecated_constants,
@ -35,23 +33,27 @@ class UndefinedType(Enum):
UNDEFINED = UndefinedType._singleton # noqa: SLF001
def _deprecated_typing_helper(attr: str) -> DeferredDeprecatedAlias:
"""Help to make a DeferredDeprecatedAlias."""
def value_fn() -> Any:
# pylint: disable-next=import-outside-toplevel
import homeassistant.core
return getattr(homeassistant.core, attr)
return DeferredDeprecatedAlias(value_fn, f"homeassistant.core.{attr}", "2025.5")
# The following types should not used and
# are not present in the core code base.
# They are kept in order not to break custom integrations
# that may rely on them.
# Deprecated as of 2024.5 use types from homeassistant.core instead.
_DEPRECATED_ContextType = DeprecatedAlias(
homeassistant.core.Context, "homeassistant.core.Context", "2025.5"
)
_DEPRECATED_EventType = DeprecatedAlias(
homeassistant.core.Event, "homeassistant.core.Event", "2025.5"
)
_DEPRECATED_HomeAssistantType = DeprecatedAlias(
homeassistant.core.HomeAssistant, "homeassistant.core.HomeAssistant", "2025.5"
)
_DEPRECATED_ServiceCallType = DeprecatedAlias(
homeassistant.core.ServiceCall, "homeassistant.core.ServiceCall", "2025.5"
)
_DEPRECATED_ContextType = _deprecated_typing_helper("Context")
_DEPRECATED_EventType = _deprecated_typing_helper("Event")
_DEPRECATED_HomeAssistantType = _deprecated_typing_helper("HomeAssistant")
_DEPRECATED_ServiceCallType = _deprecated_typing_helper("ServiceCall")
# These can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())