Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Improve typecheck in calc_md5 #7255

Merged
merged 2 commits into from Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/streamlit/util.py
Expand Up @@ -21,7 +21,7 @@
import hashlib
import os
import subprocess
from typing import Any, Dict, Iterable, List, Mapping, Set, TypeVar, Union, cast
from typing import Any, Dict, Iterable, List, Mapping, Set, TypeVar, Union

from typing_extensions import Final

Expand Down Expand Up @@ -173,9 +173,7 @@ def calc_md5(s: Union[bytes, str]) -> str:
"""Return the md5 hash of the given string."""
h = hashlib.new("md5")

# mypy seems to have trouble inferring that the type of the if/else expression is
# always bytes.
b = cast(bytes, s.encode("utf-8") if type(s) is str else s)
b = s.encode("utf-8") if isinstance(s, str) else s

h.update(b)
return h.hexdigest()
Expand Down