From 01abd3432774da0a49c6fa868aa73f0944d8c4a7 Mon Sep 17 00:00:00 2001 From: Siva Chandra Date: Mon, 3 Sep 2018 09:50:01 -0700 Subject: [PATCH] Use class with __call__ method instead of callable. (#2418) This will enable checking positional and keyword parameters. --- stdlib/2and3/codecs.pyi | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stdlib/2and3/codecs.pyi b/stdlib/2and3/codecs.pyi index 8b6830adf..332c15499 100644 --- a/stdlib/2and3/codecs.pyi +++ b/stdlib/2and3/codecs.pyi @@ -1,5 +1,5 @@ import sys -from typing import Any, BinaryIO, Callable, Generator, IO, Iterable, List, Optional, Text, TextIO, Tuple, Type, TypeVar, Union +from typing import Any, BinaryIO, Callable, Generator, IO, Iterable, List, Optional, Protocol, Text, TextIO, Tuple, Type, TypeVar, Union from abc import abstractmethod import types @@ -13,14 +13,17 @@ import types _Decoded = Text _Encoded = bytes -# TODO: It is not possible to specify these signatures correctly, because -# they have an optional positional or keyword argument for errors=. -_Encoder = Callable[[_Decoded], Tuple[_Encoded, int]] # signature of Codec().encode -_Decoder = Callable[[_Encoded], Tuple[_Decoded, int]] # signature of Codec().decode +class _Encoder(Protocol): + def __call__(self, input: _Decoded, errors: str = ...) -> Tuple[_Encoded, int]: ... # signature of Codec().encode +class _Decoder(Protocol): + def __call__(self, input: _Encoded, errors: str = ...) -> Tuple[_Decoded, int]: ... # signature of Codec().decode + +# TODO: Replace the following Callable definitions with protocol classes as above. _StreamReader = Callable[[IO[_Encoded]], StreamReader] # signature of StreamReader __init__ _StreamWriter = Callable[[IO[_Encoded]], StreamWriter] # signature of StreamWriter __init__ _IncrementalEncoder = Callable[[], IncrementalEncoder] # signature of IncrementalEncoder __init__ _IncrementalDecoder = Callable[[], IncrementalDecoder] # signature of IncrementalDecoder __init__ + def encode(obj: _Decoded, encoding: str = ..., errors: str = ...) -> _Encoded: ... def decode(obj: _Encoded, encoding: str = ..., errors: str = ...) -> _Decoded: ... def lookup(encoding: str) -> CodecInfo: ...