-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[WIP] PEP 484: Describe a way of annotating decorated declarations #242
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,6 +276,34 @@ implemented by deferring to ``isinstance(x, collections.abc.Callable)``. | |
However, ``isinstance(x, typing.Callable[...])`` is not supported. | ||
|
||
|
||
Decorators | ||
---------- | ||
|
||
Decorators can modify the types of the functions or classes they | ||
decorate. Use the ``decorated_type`` decorator to declare the type of | ||
the resulting item after all other decorators have been applied:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might also tweak the words to indicate that there may not be any other decorators. |
||
|
||
from typing import ContextManager, Iterator, decorated_type | ||
from contextlib import contextmanager | ||
|
||
class DatabaseSession: ... | ||
|
||
@decorated_type(Callable[[str], ContextManager[DatabaseSession]]) | ||
@contextmanager | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May need a different example than |
||
def session(url: str) -> Iterator[DatabaseSession]: | ||
s = DatabaseSession(url) | ||
try: | ||
yield s | ||
finally: | ||
s.close() | ||
|
||
The argument of ``decorated_type`` is a type annotation on the name | ||
being declared (``session``, in the example above). If you have | ||
multiple decorators, ``decorated_type`` must be topmost. The | ||
``decorated_type`` decorator is invalid on a function declaration that | ||
is also decorated with ``overload``, but you can annotate the | ||
implementation of the overload series with ``decorated_type``. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we clarify (via example and/or specification language) that the type in I'm not sure if the semantics should exactly match those of assignment, though that would be my first approximation until we find a counterexample. |
||
|
||
Generics | ||
-------- | ||
|
||
|
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.
Oh wait, we're going with
declared_type
instead right?