-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unnecessary functools.cached_property
backport
#114239
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,16 @@ | ||
"""Functools backports from standard lib.""" | ||
"""Functools backports from standard lib. | ||
|
||
# This file contains parts of Python's module wrapper | ||
# for the _functools C module | ||
# to allow utilities written in Python to be added | ||
# to the functools module. | ||
# Written by Nick Coghlan <ncoghlan at gmail.com>, | ||
# Raymond Hettinger <python at rcn.com>, | ||
# and Łukasz Langa <lukasz at langa.pl>. | ||
# Copyright © 2001-2023 Python Software Foundation; All Rights Reserved | ||
This file contained the backport of the cached_property implementation of Python 3.12. | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from types import GenericAlias | ||
from typing import Any, Generic, Self, TypeVar, overload | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
class cached_property(Generic[_T]): | ||
"""Backport of Python 3.12's cached_property. | ||
Since we have dropped support for Python 3.11, we can remove this backport. | ||
This file is kept for now to avoid breaking custom components that might | ||
import it. | ||
""" | ||
|
||
Includes https://github.com/python/cpython/pull/101890/files | ||
""" | ||
|
||
def __init__(self, func: Callable[[Any], _T]) -> None: | ||
"""Initialize.""" | ||
self.func: Callable[[Any], _T] = func | ||
self.attrname: str | None = None | ||
self.__doc__ = func.__doc__ | ||
|
||
def __set_name__(self, owner: type[Any], name: str) -> None: | ||
"""Set name.""" | ||
if self.attrname is None: | ||
self.attrname = name | ||
elif name != self.attrname: | ||
raise TypeError( | ||
"Cannot assign the same cached_property to two different names " | ||
f"({self.attrname!r} and {name!r})." | ||
) | ||
|
||
@overload | ||
def __get__(self, instance: None, owner: type[Any] | None = None) -> Self: ... | ||
|
||
@overload | ||
def __get__(self, instance: Any, owner: type[Any] | None = None) -> _T: ... | ||
from __future__ import annotations | ||
|
||
def __get__( | ||
self, instance: Any | None, owner: type[Any] | None = None | ||
) -> _T | Self: | ||
"""Get.""" | ||
if instance is None: | ||
return self | ||
if self.attrname is None: | ||
raise TypeError( | ||
"Cannot use cached_property instance without calling __set_name__ on it." | ||
) | ||
try: | ||
cache = instance.__dict__ | ||
# not all objects have __dict__ (e.g. class defines slots) | ||
except AttributeError: | ||
msg = ( | ||
f"No '__dict__' attribute on {type(instance).__name__!r} " | ||
f"instance to cache {self.attrname!r} property." | ||
) | ||
raise TypeError(msg) from None | ||
val = self.func(instance) | ||
try: | ||
cache[self.attrname] = val | ||
except TypeError: | ||
msg = ( | ||
f"The '__dict__' attribute on {type(instance).__name__!r} instance " | ||
f"does not support item assignment for caching {self.attrname!r} property." | ||
) | ||
raise TypeError(msg) from None | ||
return val | ||
from functools import cached_property | ||
|
||
__class_getitem__ = classmethod(GenericAlias) # type: ignore[var-annotated] | ||
__all__ = [ | ||
"cached_property", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something similar to the deprecated const?
core/homeassistant/components/alarm_control_panel/const.py
Line 61 in ff3a801
core/homeassistant/components/alarm_control_panel/const.py
Lines 74 to 78 in ff3a801
Importing and using a deprecated const will create a log entry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be possible. For now I've copied the way from
backports.enum
and added a new entry for thehass_imports
pylint plugin.core/homeassistant/backports/enum.py
Lines 10 to 16 in 53944b2
Would it be fine if we explore your idea after this is merged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's fine to add it after this PR is merged. Maybe we can also add it for the enum one.
So we can remove the files et all after the deprecation period