Skip to content

Commit 5e3e056

Browse files
authored
Improve passlib.utils (#13798)
1 parent 6b8aebc commit 5e3e056

File tree

3 files changed

+63
-45
lines changed

3 files changed

+63
-45
lines changed

stubs/passlib/passlib/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Final
2+
3+
__version__: Final[str]

stubs/passlib/passlib/utils/__init__.pyi

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import random
12
import timeit
2-
from _typeshed import Incomplete
3-
from collections.abc import Generator
3+
from _typeshed import ReadableBuffer
4+
from collections.abc import Iterable
45
from hmac import compare_digest
5-
from typing import Any
6+
from typing import Final, Literal, SupportsBytes, SupportsIndex, overload
67

78
from passlib.utils.compat import JYTHON as JYTHON
89

@@ -34,9 +35,9 @@ __all__ = [
3435
"has_salt_info",
3536
]
3637

37-
sys_bits: Any
38+
sys_bits: Final[int]
3839
unix_crypt_schemes: list[str]
39-
rounds_cost_values: Any
40+
rounds_cost_values: Final[list[str]]
4041

4142
class SequenceMixin:
4243
def __getitem__(self, idx): ...
@@ -47,29 +48,40 @@ class SequenceMixin:
4748

4849
consteq = compare_digest
4950

50-
def str_consteq(left, right): ...
51-
def saslprep(source, param: str = "value"): ...
52-
def render_bytes(source, *args): ...
53-
def xor_bytes(left, right): ...
54-
def is_same_codec(left, right): ...
55-
def is_ascii_safe(source): ...
56-
def to_bytes(source, encoding: str = "utf-8", param: str = "value", source_encoding: Incomplete | None = None): ...
57-
def to_unicode(source, encoding: str = "utf-8", param: str = "value"): ...
58-
def to_native_str(source, encoding: str = "utf-8", param: str = "value"): ...
51+
def str_consteq(left: str | bytes, right: str | bytes) -> bool: ...
52+
def splitcomma(source: str, sep: str = ",") -> list[str]: ...
53+
def saslprep(source: str, param: str = "value") -> str: ...
54+
def render_bytes(source: str | bytes, *args: str | bytes) -> bytes: ...
55+
def bytes_to_int(value: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer) -> int: ...
56+
def int_to_bytes(value: int, count: SupportsIndex) -> bytes: ...
57+
def xor_bytes(
58+
left: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
59+
right: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,
60+
) -> bytes: ...
61+
def repeat_string(source: str | bytes, size: int) -> str | bytes: ...
62+
def is_ascii_codec(codec: str) -> bool: ...
63+
def is_same_codec(left: str, right: str) -> bool: ...
64+
def is_ascii_safe(source: str | bytes) -> bool: ...
65+
def to_bytes(source: str | bytes, encoding: str = "utf-8", param: str = "value", source_encoding: str | None = None) -> bytes: ...
66+
def to_unicode(source: str | bytes, encoding: str = "utf-8", param: str = "value") -> str: ...
67+
def to_native_str(source: str | bytes, encoding: str = "utf-8", param: str = "value") -> str: ...
5968

6069
has_crypt: bool
6170

62-
def safe_crypt(secret, hash) -> None: ...
63-
def test_crypt(secret, hash): ...
71+
def safe_crypt(secret: str | bytes, hash: str | bytes) -> str | None: ...
72+
def test_crypt(secret: str | bytes, hash: str) -> bool: ...
6473

6574
timer = timeit.default_timer
6675
tick = timer
67-
rng: Any
76+
rng: random.Random
6877

69-
def getrandbytes(rng, count) -> Generator[None, None, Any]: ...
70-
def getrandstr(rng, charset, count) -> Generator[None, None, Any]: ...
71-
def generate_password(size: int = 10, charset=...): ...
72-
def is_crypt_handler(obj): ...
73-
def is_crypt_context(obj): ...
74-
def has_rounds_info(handler): ...
75-
def has_salt_info(handler): ...
78+
@overload
79+
def getrandbytes(rng: random.Random, count: None) -> Literal[b""]: ...
80+
@overload
81+
def getrandbytes(rng: random.Random, count) -> bytes: ...
82+
def getrandstr(rng: random.Random, charset: str, count: int) -> str: ...
83+
def generate_password(size: int = 10, charset: str = ...) -> str: ...
84+
def is_crypt_handler(obj) -> bool: ...
85+
def is_crypt_context(obj) -> bool: ...
86+
def has_rounds_info(handler) -> bool: ...
87+
def has_salt_info(handler) -> bool: ...

stubs/passlib/passlib/utils/binary.pyi

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
from _typeshed import Incomplete
2-
from typing import Any
1+
from _typeshed import ReadableBuffer
2+
from logging import Logger
3+
from typing import Any, Final
34

4-
BASE64_CHARS: Any
5-
AB64_CHARS: Any
6-
HASH64_CHARS: Any
7-
BCRYPT_CHARS: Any
8-
PADDED_BASE64_CHARS: Any
9-
HEX_CHARS: Any
10-
UPPER_HEX_CHARS: Any
11-
LOWER_HEX_CHARS: Any
12-
ALL_BYTE_VALUES: Any
5+
log: Logger
136

14-
def compile_byte_translation(mapping, source: Incomplete | None = None): ...
15-
def b64s_encode(data): ...
16-
def b64s_decode(data): ...
17-
def ab64_encode(data): ...
18-
def ab64_decode(data): ...
19-
def b32encode(source): ...
20-
def b32decode(source): ...
7+
BASE64_CHARS: Final[str]
8+
AB64_CHARS: Final[str]
9+
HASH64_CHARS: Final[str]
10+
BCRYPT_CHARS: Final[str]
11+
PADDED_BASE64_CHARS: Final[str]
12+
HEX_CHARS: Final[str]
13+
UPPER_HEX_CHARS: Final[str]
14+
LOWER_HEX_CHARS: Final[str]
15+
ALL_BYTE_VALUES: Final[bytes]
16+
17+
def compile_byte_translation(mapping: dict[str | bytes | int, str | bytes], source: bytes | None = None) -> bytes: ...
18+
def b64s_encode(data: ReadableBuffer) -> bytes: ...
19+
def b64s_decode(data: str | ReadableBuffer) -> bytes: ...
20+
def ab64_encode(data: ReadableBuffer) -> bytes: ...
21+
def ab64_decode(data: str | ReadableBuffer) -> bytes: ...
22+
def b32encode(source: ReadableBuffer) -> str: ...
23+
def b32decode(source: str | bytes) -> bytes: ...
2124

2225
class Base64Engine:
2326
bytemap: Any
@@ -46,9 +49,9 @@ class LazyBase64Engine(Base64Engine):
4649
def __init__(self, *args, **kwds) -> None: ...
4750
def __getattribute__(self, attr: str): ...
4851

49-
h64: Any
50-
h64big: Any
51-
bcrypt64: Any
52+
h64: Base64Engine
53+
h64big: Base64Engine
54+
bcrypt64: Base64Engine
5255

5356
__all__ = [
5457
# constants

0 commit comments

Comments
 (0)