Skip to content

Instantly share code, notes, and snippets.

@stroxler
Last active December 22, 2021 13:53
Show Gist options
  • Save stroxler/0ebf29c3b3e5a52cc1809267c1073dd9 to your computer and use it in GitHub Desktop.
Save stroxler/0ebf29c3b3e5a52cc1809267c1073dd9 to your computer and use it in GitHub Desktop.
Comparing rules for parentheses in arrow callable syntax (PEP 677)
# A quick example to illustrate the two syntaxes
def f(x: str, y: int) -> bool: ...
f: ((str, int) -> bool) # full parentheses syntax
f: (str, int) -> bool # args only parentheses
# Now, some samples from typeshed (I abbreviated a few args lists so that it's easier to find the relevant part)
# full parentheses
def check_call_abbreviated(
args: _CMD,
bufsize: int = ...,
preexec_fn: (() -> Any) = ...,
close_fds: bool = ...,
) -> int: ...
# only args parenthesized
def check_call_abbreviated(
args: _CMD,
bufsize: int = ...,
preexec_fn: (() -> Any) = ...,
close_fds: bool = ...,
) -> int: ...
# full
class UnixDatagramServer(BaseServer):
def __init__(
self,
server_address: str | bytes,
RequestHandlerClass: ((...) -> BaseRequestHandler),
bind_and_activate: bool = ...,
) -> None: ...
# only args
class UnixDatagramServer(BaseServer):
def __init__(
self,
server_address: str | bytes,
RequestHandlerClass: ((...) -> BaseRequestHandler),
bind_and_activate: bool = ...,
) -> None: ...
# full
def configure_abbreviated(
self,
foreground: _Color = ...,
postcommand: (() -> Any) | str = ...,
relief: _Relief = ...,
tearoffcommand: ((str, str) -> Any) | str = ...,
title: str = ...,
) -> Dict[str, Tuple[str, str, str, Any, Any]] | None: ...
# only args, if -> binds tighter (if | binds tighter, same as full!)
def configure_abbreviated(
self,
foreground: _Color = ...,
postcommand: () -> Any | str = ...,
relief: _Relief = ...,
tearoffcommand: (str, str) -> Any | str = ...,
title: str = ...,
) -> Dict[str, Tuple[str, str, str, Any, Any]] | None: ...
# full
def Pool(
processes: Optional[int] = ...,
initializer: ((...) -> Any) | None = ...,
initargs: Iterable[Any] = ...,
) -> pool.Pool: ...
# only args parenthesized, if -> binds tighter (if | binds tighter, same as full!)
def Pool(
processes: Optional[int] = ...,
initializer: (...) -> Any | None = ...,
initargs: Iterable[Any] = ...,
) -> pool.Pool: ...
# full
class ProcessPoolExecutor(Executor):
def __init__(
self,
max_workers: int | None = ...,
initializer: ((...) -> None) | None = ...,
initargs: Tuple[Any, ...] = ...,
) -> None: ...
# only args, if -> binds tighter (otherwise, same as full)
class ProcessPoolExecutor(Executor):
def __init__(
self,
max_workers: int | None = ...,
initializer: (...) -> None | None = ...,
initargs: Tuple[Any, ...] = ...,
) -> None: ...
# full
def tag_bind(
self, tagname: str, sequence: str | None = ..., callback: ((tkinter.Event[Treeview]) -> Any) | None = ...
) -> str: ...
# only args (if -> binds tighter, otherwise same as full)
def tag_bind(
self, tagname: str, sequence: str | None = ..., callback: (tkinter.Event[Treeview] -> Any) | None = ...
) -> str: ...
# full
class Element(MutableSequence[Element]):
def __init__(self, tag: str | ((...) -> Element), attrib: Dict[str, str] = ..., **extra: str) -> None: ...
# only args, if -> binds tighter (otherwise same as full)
class Element(MutableSequence[Element]):
def __init__(self, tag: str | (...) -> Element, attrib: Dict[str, str] = ..., **extra: str) -> None: ...
# full
def start_new_thread(function: ((...) -> Any), args: Any, kwargs: Any = ...) -> int: ...
# args
def start_new_thread(function: (...) -> Any, args: Any, kwargs: Any = ...) -> int: ...
# full
def takewhile(predicate: ((_T) -> Any), iterable: Iterable[_T]) -> Iterator[_T]: ...
# args
def takewhile(predicate: (_T) -> Any, iterable: Iterable[_T]) -> Iterator[_T]: ...
# full
def min(__arg1: _T, __arg2: _T, *_args: _T, key: ((_T) -> SupportsLessThanT)) -> _T: ...
# args
def min(__arg1: _T, __arg2: _T, *_args: _T, key: (_T) -> SupportsLessThanT) -> _T: ...
# full
def contextmanager(func: ((...) -> Iterator[_T])) -> ((...) -> ContextManager[_T]): ...
# args
def contextmanager(func: (...) -> Iterator[_T]) -> (...) -> ContextManager[_T]: ...
# full
class StartResponse(Protocol):
def __call__(
self, status: str, headers: List[Tuple[str, str]], exc_info: _OptExcInfo | None = ...
) -> ((bytes) -> Any): ...
# only args
class StartResponse(Protocol):
def __call__(
self, status: str, headers: List[Tuple[str, str]], exc_info: _OptExcInfo | None = ...
) -> bytes -> Any: ...
# full
def lru_cache(maxsize: int | None = ..., typed: bool = ...) -> (((...) -> _T) -> _lru_cache_wrapper[_T]): ...
# only args
def lru_cache(maxsize: int | None = ..., typed: bool = ...) -> (...) -> _T -> _lru_cache_wrapper[_T]: ...
# full
def module_for_loader(fxn: ((...) -> types.ModuleType)) -> ((...) -> types.ModuleType): ...
# only args
def module_for_loader(fxn: (...) -> types.ModuleType) -> (...) -> types.ModuleType: ...
# A quick example to illustrate
def f(x: str, y: int) -> bool: ...
# full parentheses syntax
f: ((str, int) -> bool)
# outer only
f: (str, int -> bool)
# args only
f: (str, int) -> bool
def f(x: str, y: int) -> bool: ...
f: (str, int -> bool) # standard outer-only syntax
def g() -> bool: ...
g: (() -> bool) # variant 1: use a special () for empty args
g: (-> bool) # variant 2: use a bare -> for empty args
# typshed examples, using variant 1
def check_call_abbreviated(
args: _CMD,
bufsize: int = ...,
preexec_fn: (() -> Any) = ...,
close_fds: bool = ...,
) -> int: ...
class UnixDatagramServer(BaseServer):
def __init__(
self,
server_address: str | bytes,
RequestHandlerClass: (... -> BaseRequestHandler),
bind_and_activate: bool = ...,
) -> None: ...
def configure_abbreviated(
self,
foreground: _Color = ...,
postcommand: (() -> Any) | str = ...,
relief: _Relief = ...,
tearoffcommand: (str, str -> Any) | str = ...,
title: str = ...,
) -> Dict[str, Tuple[str, str, str, Any, Any]] | None: ...
def Pool(
processes: Optional[int] = ...,
initializer: (... -> Any) | None = ...,
initargs: Iterable[Any] = ...,
) -> pool.Pool: ...
class ProcessPoolExecutor(Executor):
def __init__(
self,
max_workers: int | None = ...,
initializer: (... -> None) | None = ...,
initargs: Tuple[Any, ...] = ...,
) -> None: ...
def tag_bind(
self, tagname: str, sequence: str | None = ..., callback: (tkinter.Event[Treeview] -> Any) | None = ...
) -> str: ...
class Element(MutableSequence[Element]):
def __init__(self, tag: str | (... -> Element), attrib: Dict[str, str] = ..., **extra: str) -> None: ...
def start_new_thread(function: (... -> Any), args: Any, kwargs: Any = ...) -> int: ...
def takewhile(predicate: (_T -> Any), iterable: Iterable[_T]) -> Iterator[_T]: ...
def min(__arg1: _T, __arg2: _T, *_args: _T, key: (_T -> SupportsLessThanT)) -> _T: ...
def contextmanager(func: (... -> Iterator[_T])) -> (... -> ContextManager[_T]): ...
class StartResponse(Protocol):
def __call__(
self, status: str, headers: List[Tuple[str, str]], exc_info: _OptExcInfo | None = ...
) -> (bytes -> Any): ...
def lru_cache(maxsize: int | None = ..., typed: bool = ...) -> ((... -> _T) -> _lru_cache_wrapper[_T]): ...
def module_for_loader(fxn: (... -> types.ModuleType)) -> (... -> types.ModuleType): ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment