-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
typing: Strengthen Type Signature of Decorators with ParamSpec. #21782
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
Conversation
1dea81e
to
8b56bc5
Compare
A follow-up to this PR will be fixing the view function decorators when the mypy fix is released. |
e20ca14
to
9f81fa7
Compare
4ca2f2a
to
c9b36b4
Compare
This demonstrates a way to resolve the long-standing issue of typing higher-order identity functions without using `cast` and in a type-safe manner for decorators in `cache.py`. Signed-off-by: Zixuan James Li <359101898@qq.com>
`cachify` is essentially caching the return value of a function using only the non-keyword-only arguments as the key. The use case of the function in the backend can be sufficiently covered by `functools.lru_cache` as an unbound cache. There is no signficant difference apart from `cachify` overlooking keyword-only arguments, and `functools.lru_cache` being conveniently typed. Signed-off-by: Zixuan James Li <359101898@qq.com>
Signed-off-by: Zixuan James Li <359101898@qq.com>
Signed-off-by: Zixuan James Li <359101898@qq.com>
Signed-off-by: Zixuan James Li <359101898@qq.com>
def currently_used_upload_space_bytes(self) -> int: | ||
used_space = Attachment.objects.filter(realm=self).aggregate(Sum("size"))["size__sum"] | ||
def currently_used_upload_space_bytes(realm) -> int: | ||
used_space = Attachment.objects.filter(realm=realm).aggregate(Sum("size"))["size__sum"] |
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 you confirm that the way self
works as a keyword is that the current instance will still be received as the first argument even though it is not called self
?
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.
Replying to #21782 (comment):
self
is a conventional name for the first argument of methods. It does
not affect how the instance will be passed.
I guess the Python 3 tutorial confirms that:
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
I guess the crux of the issue is that a ParamSpec includes the name of positional arguments, since you can pass a positional parameter by name. OK.
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.
`self` is a conventional name for the first argument of methods. It does
not affect how the instance will be passed.
…On Thu, Apr 14, 2022, 12:24 Tim Abbott ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In zerver/models.py
<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_zulip_zulip_pull_21782-23discussion-5Fr850608581&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=b_yQYQzY71v5dLCdkk1wmw&m=66e1plChRoHO3_JL6JBBRFkJKc5JPF4HA4Ie7hiIvbeUETlQryAKM1VKFC3iHh8J&s=OZzi93_lEmwTT_NQ2515N0UI3tHOCAhOhX_tIEzduNA&e=>
:
> @cache_with_key(get_realm_used_upload_space_cache_key, timeout=3600 * 24 * 7)
- def currently_used_upload_space_bytes(self) -> int:
- used_space = Attachment.objects.filter(realm=self).aggregate(Sum("size"))["size__sum"]
+ def currently_used_upload_space_bytes(realm) -> int:
+ used_space = Attachment.objects.filter(realm=realm).aggregate(Sum("size"))["size__sum"]
Can you confirm that the way self works as a keyword is that the current
instance will still be received as the first argument even though it is not
called self?
—
Reply to this email directly, view it on GitHub
<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_zulip_zulip_pull_21782-23pullrequestreview-2D942518224&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=b_yQYQzY71v5dLCdkk1wmw&m=66e1plChRoHO3_JL6JBBRFkJKc5JPF4HA4Ie7hiIvbeUETlQryAKM1VKFC3iHh8J&s=RLNIeQdmQHXyWyM3GMsdVzE-uO7MEncq41ePRYEQyZE&e=>,
or unsubscribe
<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AJQG4XZDLKJVNRUMZ6CSRFDVFBBC7ANCNFSM5TJKQ3LA&d=DwMCaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=b_yQYQzY71v5dLCdkk1wmw&m=66e1plChRoHO3_JL6JBBRFkJKc5JPF4HA4Ie7hiIvbeUETlQryAKM1VKFC3iHh8J&s=vLN49rZDlkXWsAARAqECYu-F7eYQOQQBA_RVIMQknic&e=>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Merged, thanks @PIG208! |
This should resolve the long-standing issue of typing higher-order identity functions without using
cast
. As many decorators will be adapted to this pattern as possible.Note that before mypy releases support to
Cacatenate
, we can't type certain decorators that curry or modify the parameters in any way (likehas_request_variables
).Currently, we import
ParamSpec
fromtyping_extensions
. Hopefully, we will migrate to Python 3.10 in the future and there should be a separate PR dropping the back-port imports added here.