Relax typing on cached_property to accept subclasses (#95407)
This commit is contained in:
parent
235b98da8a
commit
19b0a6e7f6
1 changed files with 10 additions and 9 deletions
|
@ -6,22 +6,21 @@ from types import GenericAlias
|
|||
from typing import Any, Generic, Self, TypeVar, overload
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_R = TypeVar("_R")
|
||||
|
||||
|
||||
class cached_property(Generic[_T, _R]): # pylint: disable=invalid-name
|
||||
class cached_property(Generic[_T]): # 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], _T]) -> None:
|
||||
"""Initialize."""
|
||||
self.func = func
|
||||
self.attrname: Any = None
|
||||
self.func: Callable[[Any], _T] = func
|
||||
self.attrname: str | None = 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
|
||||
|
@ -32,14 +31,16 @@ class cached_property(Generic[_T, _R]): # pylint: disable=invalid-name
|
|||
)
|
||||
|
||||
@overload
|
||||
def __get__(self, instance: None, owner: type[_T]) -> Self:
|
||||
def __get__(self, instance: None, owner: type[Any] | None = None) -> Self:
|
||||
...
|
||||
|
||||
@overload
|
||||
def __get__(self, instance: _T, owner: type[_T]) -> _R:
|
||||
def __get__(self, instance: Any, owner: type[Any] | None = None) -> _T:
|
||||
...
|
||||
|
||||
def __get__(self, instance: _T | None, owner: type[_T] | None = None) -> _R | Self:
|
||||
def __get__(
|
||||
self, instance: Any | None, owner: type[Any] | None = None
|
||||
) -> _T | Self:
|
||||
"""Get."""
|
||||
if instance is None:
|
||||
return self
|
||||
|
|
Loading…
Add table
Reference in a new issue