8000 typing: Strengthen Type Signature of Decorators with ParamSpec. by PIG208 · Pull Request #21782 · zulip/zulip · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 5 commits into from
Apr 14, 2022

Conversation

PIG208
Copy link
Member
@PIG208 PIG208 commented Apr 13, 2022

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 (like has_request_variables).

Currently, we import ParamSpec from typing_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.

@PIG208 PIG208 force-pushed the no-cast branch 2 times, most recently from 1dea81e to 8b56bc5 Compare April 13, 2022 14:58
@PIG208 PIG208 changed the title typing: Strengthen Type Signature of Decorators with ParamSpec. [WIP] typing: Strengthen Type Signature of Decorators with ParamSpec. Apr 13, 2022
@PIG208
Copy link
Member Author
PIG208 commented Apr 13, 2022

A follow-up to this PR will be fixing the view function decorators when the mypy fix is released.

@PIG208 PIG208 marked this pull request as ready for review April 13, 2022 15:10
@timabbott timabbott requested a review from andersk April 13, 2022 18:45
@PIG208 PIG208 force-pushed the no-cast branch 2 times, most recently from e20ca14 to 9f81fa7 Compare April 13, 2022 19:27
@PIG208 PIG208 force-pushed the no-cast branch 3 times, most recently from 4ca2f2a to c9b36b4 Compare April 13, 2022 20:11
PIG208 added 5 commits April 13, 2022 16:14
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>
@PIG208 PIG208 requested a review from timabbott April 14, 2022 15:14
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"]
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is technically a positional-or-keyword argument. Actually, I don't think PEP 612 mentions that there should be an error in this case. It is likely a false positive of mypy. But the workaround here isn't that harmful. I opened an issue to see if we can get a fix.

@PIG208
Copy link
Member Author
PIG208 commented Apr 14, 2022 via email

@timabbott timabbott merged commit 532f827 into zulip:main Apr 14, 2022
@timabbott
Copy link
Member

Merged, thanks @PIG208!

@PIG208 PIG208 deleted the no-cast branch May 30, 2022 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0