From 83f9d833fb931af86ad80c9771856a4a37ef4e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Sun, 29 Dec 2019 17:02:18 +0200 Subject: [PATCH] Fixed missing methods, wrong names and types (#3560) Changes: * Added the missing methods `selected_alpn_protocol()` and `version()` to `SSLObject` * Fixed the naming of the `shared_ciphers()` method in `SSLSocket` and `SSLObject` (was missing the last "s") * Fixed return type of `cipher()` (it's documented to return `None` if no connection has been established) * Fixed second argument in cipher and shared_ciphers The TLS version is a string, like "TLSv1.2". * Added explicit overloads for getpeercert() Its return type can be determined statically based on the `binary_form` argument. --- stdlib/2and3/ssl.pyi | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/stdlib/2and3/ssl.pyi b/stdlib/2and3/ssl.pyi index 3dd685ff0..806f6529b 100644 --- a/stdlib/2and3/ssl.pyi +++ b/stdlib/2and3/ssl.pyi @@ -2,12 +2,18 @@ from typing import ( Any, Callable, ClassVar, Dict, List, NamedTuple, Optional, Set, Text, Type, Tuple, Union, + overload ) import enum import socket import sys import os +if sys.version_info >= (3, 8): + from typing import Literal +else: + from typing_extensions import Literal + _PCTRTT = Tuple[Tuple[str, str], ...] _PCTRTTT = Tuple[_PCTRTT, ...] _PeerCertRetDictType = Dict[str, Union[str, _PCTRTTT, _PCTRTT]] @@ -200,10 +206,15 @@ class SSLSocket(socket.socket): buffer: Optional[bytearray] = ...) -> bytes: ... def write(self, buf: bytes) -> int: ... def do_handshake(self) -> None: ... - def getpeercert(self, binary_form: bool = ...) -> _PeerCertRetType: ... - def cipher(self) -> Tuple[str, int, int]: ... + @overload + def getpeercert(self, binary_form: Literal[False] = ...) -> Optional[_PeerCertRetDictType]: ... + @overload + def getpeercert(self, binary_form: Literal[True]) -> Optional[bytes]: ... + @overload + def getpeercert(self, binary_form: bool) -> _PeerCertRetType: ... + def cipher(self) -> Optional[Tuple[str, str, int]]: ... if sys.version_info >= (3, 5): - def shared_cipher(self) -> Optional[List[Tuple[str, int, int]]]: ... + def shared_ciphers(self) -> Optional[List[Tuple[str, str, int]]]: ... def compression(self) -> Optional[str]: ... def get_channel_binding(self, cb_type: str = ...) -> Optional[bytes]: ... def selected_alpn_protocol(self) -> Optional[str]: ... @@ -284,14 +295,21 @@ if sys.version_info >= (3, 5): def read(self, len: int = ..., buffer: Optional[bytearray] = ...) -> bytes: ... def write(self, buf: bytes) -> int: ... - def getpeercert(self, binary_form: bool = ...) -> _PeerCertRetType: ... + @overload + def getpeercert(self, binary_form: Literal[False] = ...) -> Optional[_PeerCertRetDictType]: ... + @overload + def getpeercert(self, binary_form: Literal[True]) -> Optional[bytes]: ... + @overload + def getpeercert(self, binary_form: bool) -> _PeerCertRetType: ... + def selected_alpn_protocol(self) -> Optional[str]: ... def selected_npn_protocol(self) -> Optional[str]: ... - def cipher(self) -> Tuple[str, int, int]: ... - def shared_cipher(self) -> Optional[List[Tuple[str, int, int]]]: ... + def cipher(self) -> Optional[Tuple[str, str, int]]: ... + def shared_ciphers(self) -> Optional[List[Tuple[str, str, int]]]: ... def compression(self) -> Optional[str]: ... def pending(self) -> int: ... def do_handshake(self) -> None: ... def unwrap(self) -> None: ... + def version(self) -> Optional[str]: ... def get_channel_binding(self, cb_type: str = ...) -> Optional[bytes]: ... if sys.version_info >= (3, 8): def verify_client_post_handshake(self) -> None: ...