From 398dbed72e51f1ecbb4ca501304d3d14045ccb92 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 27 Jun 2023 01:36:01 +0200 Subject: [PATCH] Improve type annotations of cached_property backport (#95309) --- homeassistant/backports/functools.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/homeassistant/backports/functools.py b/homeassistant/backports/functools.py index b05277173c4..c7ab0d08693 100644 --- a/homeassistant/backports/functools.py +++ b/homeassistant/backports/functools.py @@ -3,7 +3,9 @@ from __future__ import annotations from collections.abc import Callable from types import GenericAlias -from typing import Any, Generic, TypeVar, cast +from typing import Any, Generic, TypeVar, overload + +from typing_extensions import Self _T = TypeVar("_T") _R = TypeVar("_R") @@ -31,10 +33,18 @@ class cached_property(Generic[_T, _R]): # pylint: disable=invalid-name f"({self.attrname!r} and {name!r})." ) - def __get__(self, instance: _T | None, owner: type[_T] | None = None) -> _R: + @overload + def __get__(self, instance: None, owner: type[_T]) -> Self: + ... + + @overload + def __get__(self, instance: _T, owner: type[_T]) -> _R: + ... + + def __get__(self, instance: _T | None, owner: type[_T] | None = None) -> _R | Self: """Get.""" if instance is None: - return cast(_R, self) + return self if self.attrname is None: raise TypeError( "Cannot use cached_property instance without calling __set_name__ on it."