Skip to content

Commit a8f6707

Browse files
committed
Overload hash generation functions to make return types more specific
Before this change, the return type for these functions was always `bytes| str`. However, the type can be narrowed based on the value passed to `raw_output`. This change adds overload annotations for the two allowable values to allow type checkers to narrow the type and correctly infer the exact return type. Resolves #2046
1 parent db5a5ca commit a8f6707

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

faker/providers/misc/__init__.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import uuid
1010
import zipfile
1111

12-
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Type, Union
12+
from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Set, Tuple, Type, Union, overload
1313

1414
from faker.exceptions import UnsupportedFeature
1515

@@ -56,6 +56,14 @@ def binary(self, length: int = (1 * 1024 * 1024)) -> bytes:
5656
# Generator is unseeded anyway, just use urandom
5757
return os.urandom(length)
5858

59+
@overload
60+
def md5(self, raw_output: Literal[True]) -> bytes:
61+
...
62+
63+
@overload
64+
def md5(self, raw_output: Literal[False]) -> str:
65+
...
66+
5967
def md5(self, raw_output: bool = False) -> Union[bytes, str]:
6068
"""Generate a random MD5 hash.
6169
@@ -70,6 +78,14 @@ def md5(self, raw_output: bool = False) -> Union[bytes, str]:
7078
return res.digest()
7179
return res.hexdigest()
7280

81+
@overload
82+
def sha1(self, raw_output: Literal[True]) -> bytes:
83+
...
84+
85+
@overload
86+
def sha1(self, raw_output: Literal[False]) -> str:
87+
...
88+
7389
def sha1(self, raw_output: bool = False) -> Union[bytes, str]:
7490
"""Generate a random SHA-1 hash.
7591
@@ -84,6 +100,14 @@ def sha1(self, raw_output: bool = False) -> Union[bytes, str]:
84100
return res.digest()
85101
return res.hexdigest()
86102

103+
@overload
104+
def sha256(self, raw_output: Literal[True]) -> bytes:
105+
...
106+
107+
@overload
108+
def sha256(self, raw_output: Literal[False]) -> str:
109+
...
110+
87111
def sha256(self, raw_output: bool = False) -> Union[bytes, str]:
88112
"""Generate a random SHA-256 hash.
89113

faker/proxy.pyi

+43-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ from typing import (
2020
Type,
2121
TypeVar,
2222
Union,
23+
overload,
2324
)
2425
from uuid import UUID
2526

@@ -1944,7 +1945,20 @@ class Faker:
19441945
:meth:`json() <faker.providers.misc.Provider.json>` which is used under the hood.
19451946
"""
19461947
...
1947-
def md5(self, raw_output: bool = ...) -> Union[bytes, str]:
1948+
@overload
1949+
def md5(self, raw_output: Literal[True]) -> bytes:
1950+
"""
1951+
Generate a random MD5 hash.
1952+
1953+
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the MD5 hash
1954+
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
1955+
1956+
:sample: raw_output=False
1957+
:sample: raw_output=True
1958+
"""
1959+
...
1960+
@overload
1961+
def md5(self, raw_output: Literal[False]) -> str:
19481962
"""
19491963
Generate a random MD5 hash.
19501964
@@ -2001,7 +2015,20 @@ class Faker:
20012015
num_rows=10, include_row_ids=True
20022016
"""
20032017
...
2004-
def sha1(self, raw_output: bool = ...) -> Union[bytes, str]:
2018+
@overload
2019+
def sha1(self, raw_output: Literal[True]) -> bytes:
2020+
"""
2021+
Generate a random SHA-1 hash.
2022+
2023+
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the SHA-1 hash
2024+
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
2025+
2026+
:sample: raw_output=False
2027+
:sample: raw_output=True
2028+
"""
2029+
...
2030+
@overload
2031+
def sha1(self, raw_output: Literal[False]) -> str:
20052032
"""
20062033
Generate a random SHA-1 hash.
20072034
@@ -2012,7 +2039,20 @@ class Faker:
20122039
:sample: raw_output=True
20132040
"""
20142041
...
2015-
def sha256(self, raw_output: bool = ...) -> Union[bytes, str]:
2042+
@overload
2043+
def sha256(self, raw_output: Literal[True]) -> bytes:
2044+
"""
2045+
Generate a random SHA-256 hash.
2046+
2047+
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the SHA-256 hash
2048+
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
2049+
2050+
:sample: raw_output=False
2051+
:sample: raw_output=True
2052+
"""
2053+
...
2054+
@overload
2055+
def sha256(self, raw_output: Literal[False]) -> str:
20162056
"""
20172057
Generate a random SHA-256 hash.
20182058

0 commit comments

Comments
 (0)