Skip to content

Conversation

@Drino
Copy link
Contributor

@Drino Drino commented Aug 22, 2022

I believe I'm not the only one who will struggle with behavior of Callable type aliases.
I hope if this is added to the documentation - some people will save an hour on figuring how TypeAlias work.

I'm not sure if it is worth introducing "decorator factory" term here, but it is used in mypy documentation.

Related discussion:
#1236

I believe I'm not the only one who will struggle with behavior of `Callable` type aliases.
I hope if this is added to the documentation - some people will save an hour on figuring how `TypeAlias` work.

I'm not sure if it is worth introducing "decorator factory" term here, but it is used in [mypy documentation](https://mypy.readthedocs.io/en/stable/generics.html#decorator-factories).

Related discussion:
python#1236
@ghost
Copy link

ghost commented Aug 22, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

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.

Thanks, this is a useful addition.

I'm not sure it should be in the "libraries" document, but there's already a TODO on moving it out.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks, this makes sense. But I think we should rather recommend to use the _typeshed.IdentityFunction protocol, instead of recommending a custom protocol.

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from _typeshed import IdentityFunction

def decorator_factory(*, mode: str) -> "IdentityFunction":
    """
    Decorator factory is invoked with arguments like this:
        @decorator_factory(mode="easy")
        def my_function(): ...
    """
    ...

@AlexWaygood
Copy link
Member

But I think we should rather recommend to use the _typeshed.IdentityFunction protocol, instead of recommending a custom protocol.

I think it's important to note that this only works in stub files, if you have from __future__ annotations enabled, or if you use -> "IdentityFunction" instead of -> IdentityFunction.

@srittau
Copy link
Collaborator

srittau commented Aug 22, 2022

I think it's important to note that this only works in stub files, if you have from __future__ annotations enabled, or if you use -> "IdentityFunction" instead of -> IdentityFunction.

Thanks, easy to forget when always using the future import. :)

@Drino
Copy link
Contributor Author

Drino commented Aug 22, 2022

Thanks for the fast review and cool suggestion!

I've rewritten the example using IdentityFunction, though I'm a little bit concerned to show a "proper" example of decorator protocol definition, so I've added another one with slightly less trivial decorator.

Co-authored-by: Alex Waygood <[email protected]>
@Drino Drino requested a review from srittau August 23, 2022 13:19
@Drino
Copy link
Contributor Author

Drino commented Sep 23, 2022

@srittau Hello, I've went through your comments and it seems that I've resolved the issues. Would you look at this PR one more time please?

srittau added 2 commits March 17, 2025 10:45
Also, replace class names with links to the Python docs
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

I'm sorry. This completely to fell through the cracks. LGTM now, although I've changed a few links to refer to the – now existing – typing spec and to link to the Python docs.

@srittau srittau merged commit 736f9ec into python:main Mar 17, 2025
4 checks passed
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.

4 participants