Skip to content

Commit 44f7869

Browse files
quantsiniJelleZijlstra
authored andcommitted
Fix blake2 binding (#1663)
* Fix blake2 binding Currently calling `hashlib.blake2b` results in the following type errors: Cannot instantiate abstract class '_BlakeHash' with abstract attributes 'copy', 'digest', 'hexdigest' and 'update' Missing positional arguments "data", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node" in call to "_BlakeHash" * Additional changes to reflect the hashlib implementation Modifies the type signatures of: * blake2b * blake2s * sha3_224 * sha3_256 * sha3_384 * sha3_512 * shake_128 * shake_256 To reflect the types that are implemented in the standard library. These should be exposed as `type`s instead of `builtin_function_or_method`s. e.g. In [40]: type(hashlib.blake2b) Out[40]: type In [41]: type(hashlib.md5) Out[41]: builtin_function_or_method
1 parent 9656feb commit 44f7869

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

stdlib/3/hashlib.pyi

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Stubs for hashlib
22

33
import sys
4-
from abc import abstractmethod, ABCMeta
54
from typing import AbstractSet, Optional, Union
65

76
_DataType = Union[bytes, bytearray, memoryview]
87

9-
class _Hash(metaclass=ABCMeta):
8+
class _Hash(object):
109
digest_size = ... # type: int
1110
block_size = ... # type: int
1211

@@ -15,14 +14,12 @@ class _Hash(metaclass=ABCMeta):
1514
# formally specified, so may not exist on some platforms
1615
name = ... # type: str
1716

18-
@abstractmethod
19-
def update(self, arg: _DataType) -> None: ...
20-
@abstractmethod
17+
def __init__(self, data: _DataType = ...) -> None: ...
18+
19+
def copy(self) -> _Hash: ...
2120
def digest(self) -> bytes: ...
22-
@abstractmethod
2321
def hexdigest(self) -> str: ...
24-
@abstractmethod
25-
def copy(self) -> _Hash: ...
22+
def update(self, arg: _DataType) -> None: ...
2623

2724
def md5(arg: _DataType = ...) -> _Hash: ...
2825
def sha1(arg: _DataType = ...) -> _Hash: ...
@@ -42,27 +39,24 @@ if sys.version_info >= (3, 4):
4239
def pbkdf2_hmac(hash_name: str, password: _DataType, salt: _DataType, iterations: int, dklen: Optional[int] = ...) -> bytes: ...
4340

4441
if sys.version_info >= (3, 6):
45-
class _VarLenHash(metaclass=ABCMeta):
42+
class _VarLenHash(object):
4643
digest_size = ... # type: int
4744
block_size = ... # type: int
4845
name = ... # type: str
4946

50-
@abstractmethod
47+
def __init__(self, data: _DataType = ...) -> None: ...
48+
49+
def copy(self) -> _VarLenHash: ...
5150
def digest(self, length: int) -> bytes: ...
52-
@abstractmethod
5351
def hexdigest(self, length: int) -> str: ...
54-
@abstractmethod
5552
def update(self, arg: _DataType) -> None: ...
56-
@abstractmethod
57-
def copy(self) -> _VarLenHash: ...
58-
59-
def sha3_224(arg: _DataType = ...) -> _Hash: ...
60-
def sha3_256(arg: _DataType = ...) -> _Hash: ...
61-
def sha3_384(arg: _DataType = ...) -> _Hash: ...
62-
def sha3_512(arg: _DataType = ...) -> _Hash: ...
6353

64-
def shake_128(arg: _DataType = ...) -> _VarLenHash: ...
65-
def shake_256(arg: _DataType = ...) -> _VarLenHash: ...
54+
sha3_224 = _Hash
55+
sha3_256 = _Hash
56+
sha3_384 = _Hash
57+
sha3_512 = _Hash
58+
shake_128 = _VarLenHash
59+
shake_256 = _VarLenHash
6660

6761
def scrypt(password: _DataType, *, salt: _DataType, n: int, r: int, p: int, maxmem: int = ..., dklen: int = ...) -> bytes: ...
6862

@@ -72,7 +66,7 @@ if sys.version_info >= (3, 6):
7266
PERSON_SIZE = ... # type: int
7367
SALT_SIZE = ... # type: int
7468

75-
def __init__(self, data: _DataType, digest_size: int = ..., key: _DataType = ..., salt: _DataType = ..., person: _DataType = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ...) -> None: ...
69+
def __init__(self, data: _DataType = ..., digest_size: int = ..., key: _DataType = ..., salt: _DataType = ..., person: _DataType = ..., fanout: int = ..., depth: int = ..., leaf_size: int = ..., node_offset: int = ..., node_depth: int = ..., inner_size: int = ..., last_node: bool = ...) -> None: ...
7670

7771
blake2b = _BlakeHash
7872
blake2s = _BlakeHash

0 commit comments

Comments
 (0)