fix: use property instead of NewType(...) to fix type annotations for ApplicationContext attributes#2636
fix: use property instead of NewType(...) to fix type annotations for ApplicationContext attributes#2636Lulalaby merged 14 commits intoPycord-Development:masterfrom probablyjassin:master
property instead of NewType(...) to fix type annotations for ApplicationContext attributes#2636Conversation
…ix type annotations for context atrributes
Co-authored-by: Paillat <jeremiecotti@ik.me> Signed-off-by: Jässin Aouani <123012687+probablyjassin@users.noreply.github.com>
Head branch was pushed to by a user without write access
probablyjassin
left a comment
There was a problem hiding this comment.
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 = AnyTry 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 = usercauses 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 NoneI 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 <123012687+probablyjassin@users.noreply.github.com>
|
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 <53639936+Dorukyum@users.noreply.github.com>
The |
… for ApplicationContext attributes (Pycord-Development#2636) Signed-off-by: Jässin Aouani <123012687+probablyjassin@users.noreply.github.com> Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <jeremiecotti@ik.me> Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Co-authored-by: Lala Sabathil <lala@pycord.dev>
fixes #2635 : use
functools.cached_propertyto fix propert type annotations for context attributesSummary
context attributes like
guild,message,userand such have wrong type annotations. They are typed as methods instead of properties.This is because a NewType based on
propertywas used instead offunctools.cached_property, like in other spots in the codebase already.This PR implements
functools.cached_propertyfor context, which fixes these type annotations.Information
examples, ...).
Checklist
type: ignorecomments were used, a comment is also left explaining why.