Skip to content

Commit

Permalink
Relax typing on cached_property to accept subclasses
Browse files Browse the repository at this point in the history
broken out from #95315
  • Loading branch information
bdraco committed Jun 27, 2023
1 parent a4a6972 commit 3759f31
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions homeassistant/backports/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@

from typing_extensions import Self

_T = TypeVar("_T")
_R = TypeVar("_R")
_R_co = TypeVar("_R_co", covariant=True)


class cached_property(Generic[_T, _R]): # pylint: disable=invalid-name
class cached_property(Generic[_R_co]): # pylint: disable=invalid-name
"""Backport of Python 3.12's cached_property.
Includes https://github.com/python/cpython/pull/101890/files
"""

def __init__(self, func: Callable[[_T], _R]) -> None:
def __init__(self, func: Callable[[Any], _R_co]) -> None:
"""Initialize."""
self.func = func
self.attrname: Any = None
self.__doc__ = func.__doc__

def __set_name__(self, owner: type[_T], name: str) -> None:
def __set_name__(self, owner: type[Any], name: str) -> None:
"""Set name."""
if self.attrname is None:
self.attrname = name
Expand All @@ -34,14 +33,16 @@ def __set_name__(self, owner: type[_T], name: str) -> None:
)

@overload
def __get__(self, instance: None, owner: type[_T]) -> Self:
def __get__(self, instance: None, owner: type[Any]) -> Self:
...

@overload
def __get__(self, instance: _T, owner: type[_T]) -> _R:
def __get__(self, instance: Any, owner: type[Any]) -> _R_co:
...

def __get__(self, instance: _T | None, owner: type[_T] | None = None) -> _R | Self:
def __get__(
self, instance: Any | None, owner: type[Any] | None = None
) -> _R_co | Self:
"""Get."""
if instance is None:
return self
Expand Down

0 comments on commit 3759f31

Please sign in to comment.