Skip to content

docs: use ParamSpec in "Declaring decorators" #13237

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 4 commits into from
Aug 12, 2022

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Jul 26, 2022

Resolves #12983.

@ikonst
Copy link
Contributor Author

ikonst commented Jul 26, 2022

While the documented approach is valid for the common case of a decorator which doesn't change the function type, and as the docs say the cast isn't too bad in such a small function, I suppose ParamSpec would be the best practice for decorators going forward, so we should provide it as a "recipe".

@@ -553,16 +552,17 @@ Here's a complete example of a function decorator:

.. code-block:: python

from typing import Any, Callable, TypeVar, cast
from typing import Callable, ParamSpec, TypeVar
Copy link
Member

Choose a reason for hiding this comment

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

I think that we should include both examples. Because both techniques are quite common. This way we can clearly show why ParamSpec is better.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sobolevn Check it out now. I've re-introduced the non-ParamSpec example, and also added an example where we change the return type (to show where ParamSpec is actually useful beyond "avoiding casts" type pure-ism).

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you.

@sobolevn
Copy link
Member

Let's wait for a bit, maybe @srittau or @JelleZijlstra have some feedback?

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Looks good, a few editorial suggestions

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Declaring decorators" should be revised with ParamSpec
3 participants