-
-
Notifications
You must be signed in to change notification settings - Fork 464
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
fix: use functools.cached_property
to fix propert type annotations for context attributes
#2636
base: master
Are you sure you want to change the base?
Conversation
…ix type annotations for context atrributes
Co-authored-by: Paillat <[email protected]> Signed-off-by: Jässin Aouani <[email protected]>
Head branch was pushed to by a user without write access
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.
Ah yeah that makes sense since it's not used anywhere else in the File, I forgot about that.
@probablyjassin Could you test your code by doing the following? -class _cached_property:
- def __init__(self, function):
- self.function = function
- self.__doc__ = getattr(function, "__doc__")
-
- def __get__(self, instance, owner):
- if instance is None:
- return self
-
- value = self.function(instance)
- setattr(instance, self.function.__name__, value)
-
- return value
+cached_property = functools.cached_property
if TYPE_CHECKING:
from typing_extensions import ParamSpec
from .abc import Snowflake
from .commands.context import AutocompleteContext
from .commands.options import OptionChoice
from .invite import Invite
from .permissions import Permissions
from .template import Template
class _RequestLike(Protocol):
headers: Mapping[str, Any]
- cached_property = NewType("cached_property", property)
P = ParamSpec("P")
else:
- cached_property = _cached_property
AutocompleteContext = Any
OptionChoice = Any Try running a bot and doing a couple of things. If it works, push it to this pr, I will test it. It would allow to remove entirely pycord's |
that would be very nice! i'll test it right now! |
@Paillat-dev here is what I found: Your idea almost works perfectly, except that with these changes, the following line in author: Member | User = user causes this error:
Full Stack Trace
Traceback (most recent call last):
File "C:\Users\Jassin\AppData\Local\Programs\Python\Python311\Lib\functools.py", line 976, in __set_name__
raise TypeError(
TypeError: Cannot assign the same cached_property to two different names ('user' and 'author').
Meaning, if a way can be found to assign ctx.author without just directly assigning user, this problem would be solved entirely. So in addition to your idea, I tried this and it actually worked out I believe: # context.py
- author: Member | User = user
+ @cached_property
+ def author(self) -> Member | User:
+ """Returns the user that sent this context's command.
+ Shorthand for :attr:`.Interaction.user`.
+ """
+ return self.interaction.user # type: ignore # command user will never be None I tested it on my end and it works well. So if you think this is a good solution, I'll add it to this PR. |
I really start to wonder what the point is of |
fixes #2635 : use
functools.cached_property
to fix propert type annotations for context attributesSummary
context attributes like
guild
,message
,user
and such have wrong type annotations. They are typed as methods instead of properties.This is because a NewType based on
property
was used instead offunctools.cached_property
, like in other spots in the codebase already.This PR implements
functools.cached_property
for context, which fixes these type annotations.Information
examples, ...).
Checklist
type: ignore
comments were used, a comment is also left explaining why.