Skip to content

Commit

Permalink
Overload hash generation functions to make return types more specific (
Browse files Browse the repository at this point in the history
…#2047)

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

Co-authored-by: Flavio Curella <[email protected]>
  • Loading branch information
samueljsb and fcurella authored Mar 5, 2025
1 parent 1d21c00 commit cfb6ee4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
24 changes: 24 additions & 0 deletions faker/providers/misc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def binary(self, length: int = (1 * 1024 * 1024)) -> bytes:
# Generator is unseeded anyway, just use urandom
return os.urandom(length)

@overload
def md5(self, raw_output: Literal[True]) -> bytes:
...

@overload
def md5(self, raw_output: Literal[False]) -> str:
...

def md5(self, raw_output: bool = False) -> Union[bytes, str]:
"""Generate a random MD5 hash.
Expand All @@ -74,6 +82,14 @@ def md5(self, raw_output: bool = False) -> Union[bytes, str]:
return res.digest()
return res.hexdigest()

@overload
def sha1(self, raw_output: Literal[True]) -> bytes:
...

@overload
def sha1(self, raw_output: Literal[False]) -> str:
...

def sha1(self, raw_output: bool = False) -> Union[bytes, str]:
"""Generate a random SHA-1 hash.
Expand All @@ -88,6 +104,14 @@ def sha1(self, raw_output: bool = False) -> Union[bytes, str]:
return res.digest()
return res.hexdigest()

@overload
def sha256(self, raw_output: Literal[True]) -> bytes:
...

@overload
def sha256(self, raw_output: Literal[False]) -> str:
...

def sha256(self, raw_output: bool = False) -> Union[bytes, str]:
"""Generate a random SHA-256 hash.
Expand Down
46 changes: 43 additions & 3 deletions faker/proxy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,20 @@ class Faker:
"""
...

def md5(self, raw_output: bool = ...) -> Union[bytes, str]:
@overload
def md5(self, raw_output: Literal[True]) -> bytes:
"""
Generate a random MD5 hash.
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the MD5 hash
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
:sample: raw_output=False
:sample: raw_output=True
"""
...
@overload
def md5(self, raw_output: Literal[False]) -> str:
"""
Generate a random MD5 hash.
Expand Down Expand Up @@ -2263,7 +2276,8 @@ class Faker:
"""
...

def sha1(self, raw_output: bool = ...) -> Union[bytes, str]:
@overload
def sha1(self, raw_output: Literal[True]) -> bytes:
"""
Generate a random SHA-1 hash.
Expand All @@ -2275,7 +2289,33 @@ class Faker:
"""
...

def sha256(self, raw_output: bool = ...) -> Union[bytes, str]:
@overload
def sha1(self, raw_output: Literal[False]) -> str:
"""
Generate a random SHA-1 hash.
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the SHA-1 hash
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
:sample: raw_output=False
:sample: raw_output=True
"""
...

@overload
def sha256(self, raw_output: Literal[True]) -> bytes:
"""
Generate a random SHA-256 hash.
If ``raw_output`` is ``False`` (default), a hexadecimal string representation of the SHA-256 hash
will be returned. If ``True``, a ``bytes`` object representation will be returned instead.
:sample: raw_output=False
:sample: raw_output=True
"""
...
@overload
def sha256(self, raw_output: Literal[False]) -> str:
"""
Generate a random SHA-256 hash.
Expand Down

0 comments on commit cfb6ee4

Please sign in to comment.