KEMBAR78
docs: use ParamSpec in "Declaring decorators" by ikonst · Pull Request #13237 · python/mypy · GitHub
Skip to content

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".

.. 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