-
-
Notifications
You must be signed in to change notification settings - Fork 465
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 property
instead of NewType(...)
to fix type annotations for ApplicationContext attributes
#2636
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 |
This is why |
You're right, I remember looking at it a couple of weeks ago, then it went out of my mind. In any case having the properties cached in almost all places makes almost no sense (see discussion here https://discord.com/channels/881207955029110855/881735314987708456/1304609838101172286), so it would be preferred to replace them with property directly. I think for now @probablyjassin you can try as said by @DA-344 , |
Actually, @DA-344, I remembered why I didn't mention that slots thing, it is because pycord has But I think for now we can stop at just fixing the type checker issue and I'll move discussion to another issue. |
I've tested this with a project of mine and it fixes the types on attributes of ApplicationContext without breaking anything else Signed-off-by: Jässin Aouani <[email protected]>
Alright, I replaced it with |
I think this can be merged if the changed is approved as is. |
@probablyjassin Can you change the pull request title to reflect the change ? Thx |
functools.cached_property
to fix propert type annotations for context attributesproperty
instead of NewType(...)
to fix type annotations for ApplicationContext attributes
Done ✅ |
Signed-off-by: Dorukyum <[email protected]>
The |
… for ApplicationContext attributes (Pycord-Development#2636) Signed-off-by: Jässin Aouani <[email protected]> Signed-off-by: Dorukyum <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <[email protected]> Co-authored-by: Dorukyum <[email protected]> Co-authored-by: Lala Sabathil <[email protected]>
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.