Fix email typing errors (#288)

* Change (Py3) email message payload type

The docstring of the Message get_payload method indicates that the
possible return types include None and bytes. Additionally the
set_payload method accepts bytes. Therefore I've changed the
PayloadType to include bytes and the get_payload return type to be an
Optional PayloadType.

* Change email (Py3) message_from_file IO types

Instead of using TextIO and BinaryIO use IO[str] and IO[bytes]
respectively to match the type returned by the open builtin. This
follows the recommendations in #283.

The error that prompts this is reproduced via:
```
from email import message_from_file

with open('email') as file_:
    email = message_from_file(file_)
```
Argument 1 to "message_from_file" has incompatible type IO[Any]; expected "TextIO"
This commit is contained in:
Phil Jones
2016-06-11 18:43:08 +01:00
committed by Guido van Rossum
parent 89dfe15008
commit f6decefd02
2 changed files with 8 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
# Stubs for email (Python 3.4)
from typing import Callable, Optional, BinaryIO, TextIO
from typing import Callable, Optional, IO
import sys
from email.message import Message, Policy
@@ -9,9 +9,9 @@ if sys.version_info >= (3, 3):
policy: Policy = ...) -> Message: ...
def message_from_bytes(s: bytes, _class: Callable[[], Message] = ..., *,
policy: Policy = ...) -> Message: ...
def message_from_file(fp: TextIO, _class: Callable[[], Message] = ..., *,
def message_from_file(fp: IO[str], _class: Callable[[], Message] = ..., *,
policy: Policy = ...) -> Message: ...
def message_from_binary_file(fp: BinaryIO,
def message_from_binary_file(fp: IO[bytes],
_class: Callable[[], Message] = ..., *,
policy: Policy = ...) -> Message: ...
elif sys.version_info >= (3, 2):
@@ -21,10 +21,10 @@ elif sys.version_info >= (3, 2):
def message_from_bytes(s: bytes, # type: ignore
_class: Callable[[], Message] = ..., *,
strict: Optional[bool] = ...) -> Message: ...
def message_from_file(fp: TextIO, # type: ignore
def message_from_file(fp: IO[str], # type: ignore
_class: Callable[[], Message] = ..., *,
strict: Optional[bool] = ...) -> Message: ...
def message_from_binary_file(fp: BinaryIO, # type: ignore
def message_from_binary_file(fp: IO[bytes], # type: ignore
_class: Callable[[], Message] = ..., *,
strict: Optional[bool] = ...) -> Message: ...

View File

@@ -11,7 +11,7 @@ from email.contentmanager import ContentManager
_T = TypeVar('_T')
_PayloadType = Union[List[Message], str]
_PayloadType = Union[List[Message], str, bytes]
_CharsetType = Union[Charset, str, None]
_ParamsType = Union[str, None, Tuple[str, Optional[str], str]]
_ParamType = Union[str, Tuple[Optional[str], Optional[str], str]]
@@ -25,7 +25,8 @@ class Message:
def set_unixfrom(self, unixfrom: str) -> None: ...
def get_unixfrom(self) -> Optional[str]: ...
def attach(self, payload: 'Message') -> None: ...
def get_payload(self, i: int = ..., decode: bool = ...) -> _PayloadType: ...
def get_payload(self, i: int = ..., decode: bool = ...) \
-> Optional[_PayloadType]: ...
def set_payload(self, payload: _PayloadType,
charset: _CharsetType = ...) -> None: ...
def set_charset(self, charset: _CharsetType) -> None: ...