Skip to content

Commit

Permalink
Add type annotations for write (#4686)
Browse files Browse the repository at this point in the history
* Add type annotations for write

* Add format annotation

* Branching import

* Revert branching import
  • Loading branch information
harahu committed May 6, 2022
1 parent b95c9aa commit ffcbd2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/streamlit/elements/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def slider(
max_value=None,
value=None,
step=None,
format=None,
format: Optional[str] = None,
key: Optional[Key] = None,
help: Optional[str] = None,
on_change: Optional[WidgetCallback] = None,
Expand Down
21 changes: 12 additions & 9 deletions lib/streamlit/elements/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@
import inspect
import json as json
import types
from typing import cast, Any, List, Tuple, Type
from typing import cast, Any, List, Tuple, Type, TYPE_CHECKING
from typing_extensions import Final

import numpy as np

import streamlit
from streamlit import type_util
from streamlit.errors import StreamlitAPIException
from streamlit.state import SessionStateProxy

# Special methods:
if TYPE_CHECKING:
from streamlit.delta_generator import DeltaGenerator


HELP_TYPES = (
# Special methods:
HELP_TYPES: Final[Tuple[Type[Any], ...]] = (
types.BuiltinFunctionType,
types.BuiltinMethodType,
types.FunctionType,
types.MethodType,
types.ModuleType,
) # type: Tuple[Type[Any], ...]
)


class WriteMixin:
def write(self, *args, **kwargs):
def write(self, *args: Any, **kwargs: Any) -> None:
"""Write arguments to the app.
This is the Swiss Army knife of Streamlit commands: it does different
Expand Down Expand Up @@ -149,7 +152,7 @@ def write(self, *args, **kwargs):
height: 300px
"""
string_buffer = [] # type: List[str]
string_buffer: List[str] = []
unsafe_allow_html = kwargs.get("unsafe_allow_html", False)

# This bans some valid cases like: e = st.empty(); e.write("a", "b").
Expand Down Expand Up @@ -234,6 +237,6 @@ def flush_buffer():
flush_buffer()

@property
def dg(self) -> "streamlit.delta_generator.DeltaGenerator":
def dg(self) -> "DeltaGenerator":
"""Get our DeltaGenerator."""
return cast("streamlit.delta_generator.DeltaGenerator", self)
return cast("DeltaGenerator", self)

0 comments on commit ffcbd2d

Please sign in to comment.