Skip to content
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

Update cheat_sheet_py3.rst 2 properly xplain Callable #15613

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ Functions
x.anything() + 1 + "string" # no errors

# This is how you annotate a callable (function) value
Copy link
Author

Choose a reason for hiding this comment

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

Explanation on which values are the inputs of a function and which one is the return type of the function is a little ambiguous for newbies who are skimming through the cheatsheet.

Callable[[A1, A2, A3], Rt] represents a function with three arguments with types A1, A2, and A3, respectively. The return type of the function is Rt.
x: Callable[[int, float], float] = f
def register(callback: Callable[[str], int]) -> None: ...
Callable[..., ReturnType] (literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType
ReturnType can only be of a single type.

# A generator function that yields ints is secretly just a function that
# returns an iterator of ints, so that's how we annotate it
Expand Down