Skip to content

Commit ca9505c

Browse files
authored
Improve imaplib return types (#3670)
* Improve imaplib return types Mark CommandResults as obsolete. Also fix types of tagged_commands and untagged_responses. Based on a discussion in #3655. * Fix type of tagged_commands * Fix IMAP4.tagged_commands type * Mark CommandResults as private * Fix
1 parent 84c6e67 commit ca9505c

File tree

1 file changed

+52
-46
lines changed

1 file changed

+52
-46
lines changed

stdlib/2and3/imaplib.pyi

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Stubs for imaplib (Python 2)
2-
31
import imaplib
42
import subprocess
53
import sys
@@ -8,8 +6,16 @@ from socket import socket as _socket
86
from ssl import SSLSocket, SSLContext
97
from typing import Any, Callable, Dict, IO, List, Optional, Pattern, Text, Tuple, Type, Union
108

11-
CommandResults = Tuple[str, List[Any]]
9+
if sys.version_info >= (3, 8):
10+
from typing import Literal
11+
else:
12+
from typing_extensions import Literal
13+
14+
# TODO: Commands should use their actual return types, not this type alias.
15+
# E.g. Tuple[Literal["OK"], List[bytes]]
16+
_CommandResults = Tuple[str, List[Any]]
1217

18+
_AnyResponseData = Union[List[None], List[Union[bytes, Tuple[bytes, bytes]]]]
1319

1420
class IMAP4:
1521
error: Type[Exception] = ...
@@ -19,8 +25,8 @@ class IMAP4:
1925
debug: int = ...
2026
state: str = ...
2127
literal: Optional[Text] = ...
22-
tagged_commands: Dict[str, str] = ...
23-
untagged_responses: Dict[str, str] = ...
28+
tagged_commands: Dict[bytes, Optional[List[bytes]]]
29+
untagged_responses: Dict[str, List[Union[bytes, Tuple[bytes, bytes]]]]
2430
continuation_response: str = ...
2531
is_readonly: bool = ...
2632
tagnum: int = ...
@@ -41,53 +47,53 @@ class IMAP4:
4147
def send(self, data: bytes) -> None: ...
4248
def shutdown(self) -> None: ...
4349
def socket(self) -> _socket: ...
44-
def recent(self) -> CommandResults: ...
45-
def response(self, code: str) -> CommandResults: ...
50+
def recent(self) -> _CommandResults: ...
51+
def response(self, code: str) -> _CommandResults: ...
4652
def append(self, mailbox: str, flags: str, date_time: str, message: str) -> str: ...
4753
def authenticate(self, mechanism: str, authobject: Callable[[bytes], Optional[bytes]]) -> Tuple[str, str]: ...
48-
def capability(self) -> CommandResults: ...
49-
def check(self) -> CommandResults: ...
50-
def close(self) -> CommandResults: ...
51-
def copy(self, message_set: str, new_mailbox: str) -> CommandResults: ...
52-
def create(self, mailbox: str) -> CommandResults: ...
53-
def delete(self, mailbox: str) -> CommandResults: ...
54-
def deleteacl(self, mailbox: str, who: str) -> CommandResults: ...
54+
def capability(self) -> _CommandResults: ...
55+
def check(self) -> _CommandResults: ...
56+
def close(self) -> _CommandResults: ...
57+
def copy(self, message_set: str, new_mailbox: str) -> _CommandResults: ...
58+
def create(self, mailbox: str) -> _CommandResults: ...
59+
def delete(self, mailbox: str) -> _CommandResults: ...
60+
def deleteacl(self, mailbox: str, who: str) -> _CommandResults: ...
5561
if sys.version_info >= (3, 5):
56-
def enable(self, capability: str) -> CommandResults: ...
62+
def enable(self, capability: str) -> _CommandResults: ...
5763
def __enter__(self) -> IMAP4: ...
5864
def __exit__(self, *args) -> None: ...
59-
def expunge(self) -> CommandResults: ...
60-
def fetch(self, message_set: str, message_parts: str) -> CommandResults: ...
61-
def getacl(self, mailbox: str) -> CommandResults: ...
62-
def getannotation(self, mailbox: str, entry: str, attribute: str) -> CommandResults: ...
63-
def getquota(self, root: str) -> CommandResults: ...
64-
def getquotaroot(self, mailbox: str) -> CommandResults: ...
65-
def list(self, directory: str = ..., pattern: str = ...) -> CommandResults: ...
66-
def login(self, user: str, password: str) -> CommandResults: ...
67-
def login_cram_md5(self, user: str, password: str) -> CommandResults: ...
68-
def logout(self) -> CommandResults: ...
69-
def lsub(self, directory: str = ..., pattern: str = ...) -> CommandResults: ...
70-
def myrights(self, mailbox: str) -> CommandResults: ...
71-
def namespace(self) -> CommandResults: ...
72-
def noop(self) -> CommandResults: ...
73-
def partial(self, message_num: str, message_part: str, start: str, length: str) -> CommandResults: ...
74-
def proxyauth(self, user: str) -> CommandResults: ...
75-
def rename(self, oldmailbox: str, newmailbox: str) -> CommandResults: ...
76-
def search(self, charset: Optional[str], *criteria: str) -> CommandResults: ...
77-
def select(self, mailbox: str = ..., readonly: bool = ...) -> CommandResults: ...
78-
def setacl(self, mailbox: str, who: str, what: str) -> CommandResults: ...
79-
def setannotation(self, *args: str) -> CommandResults: ...
80-
def setquota(self, root: str, limits: str) -> CommandResults: ...
81-
def sort(self, sort_criteria: str, charset: str, *search_criteria: str) -> CommandResults: ...
65+
def expunge(self) -> _CommandResults: ...
66+
def fetch(self, message_set: str, message_parts: str) -> Tuple[str, _AnyResponseData]: ...
67+
def getacl(self, mailbox: str) -> _CommandResults: ...
68+
def getannotation(self, mailbox: str, entry: str, attribute: str) -> _CommandResults: ...
69+
def getquota(self, root: str) -> _CommandResults: ...
70+
def getquotaroot(self, mailbox: str) -> _CommandResults: ...
71+
def list(self, directory: str = ..., pattern: str = ...) -> Tuple[str, _AnyResponseData]: ...
72+
def login(self, user: str, password: str) -> Tuple[Literal["OK"], List[bytes]]: ...
73+
def login_cram_md5(self, user: str, password: str) -> _CommandResults: ...
74+
def logout(self) -> Tuple[str, _AnyResponseData]: ...
75+
def lsub(self, directory: str = ..., pattern: str = ...) -> _CommandResults: ...
76+
def myrights(self, mailbox: str) -> _CommandResults: ...
77+
def namespace(self) -> _CommandResults: ...
78+
def noop(self) -> Tuple[str, List[bytes]]: ...
79+
def partial(self, message_num: str, message_part: str, start: str, length: str) -> _CommandResults: ...
80+
def proxyauth(self, user: str) -> _CommandResults: ...
81+
def rename(self, oldmailbox: str, newmailbox: str) -> _CommandResults: ...
82+
def search(self, charset: Optional[str], *criteria: str) -> _CommandResults: ...
83+
def select(self, mailbox: str = ..., readonly: bool = ...) -> Tuple[str, List[Optional[bytes]]]: ...
84+
def setacl(self, mailbox: str, who: str, what: str) -> _CommandResults: ...
85+
def setannotation(self, *args: str) -> _CommandResults: ...
86+
def setquota(self, root: str, limits: str) -> _CommandResults: ...
87+
def sort(self, sort_criteria: str, charset: str, *search_criteria: str) -> _CommandResults: ...
8288
if sys.version_info >= (3,):
83-
def starttls(self, ssl_context: Optional[Any] = ...) -> CommandResults: ...
84-
def status(self, mailbox: str, names: str) -> CommandResults: ...
85-
def store(self, message_set: str, command: str, flags: str) -> CommandResults: ...
86-
def subscribe(self, mailbox: str) -> CommandResults: ...
87-
def thread(self, threading_algorithm: str, charset: str, *search_criteria: str) -> CommandResults: ...
88-
def uid(self, command: str, *args: str) -> CommandResults: ...
89-
def unsubscribe(self, mailbox: str) -> CommandResults: ...
90-
def xatom(self, name: str, *args: str) -> CommandResults: ...
89+
def starttls(self, ssl_context: Optional[Any] = ...) -> Tuple[Literal["OK"], List[None]]: ...
90+
def status(self, mailbox: str, names: str) -> _CommandResults: ...
91+
def store(self, message_set: str, command: str, flags: str) -> _CommandResults: ...
92+
def subscribe(self, mailbox: str) -> _CommandResults: ...
93+
def thread(self, threading_algorithm: str, charset: str, *search_criteria: str) -> _CommandResults: ...
94+
def uid(self, command: str, *args: str) -> _CommandResults: ...
95+
def unsubscribe(self, mailbox: str) -> _CommandResults: ...
96+
def xatom(self, name: str, *args: str) -> _CommandResults: ...
9197
def print_log(self) -> None: ...
9298

9399
class IMAP4_SSL(IMAP4):

0 commit comments

Comments
 (0)