Skip to content

Commit

Permalink
Use new annotation features in DeltaGenerator (#6045)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnOctopus committed Feb 1, 2023
1 parent 5565e99 commit 9749510
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions lib/streamlit/delta_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@

"""Allows us to create and absorb changes (aka Deltas) to elements."""

from __future__ import annotations

import sys
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Hashable,
Iterable,
NoReturn,
Optional,
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
Expand Down Expand Up @@ -229,10 +227,10 @@ class DeltaGenerator(
# those, see above.
def __init__(
self,
root_container: Optional[int] = RootContainer.MAIN,
cursor: Optional[Cursor] = None,
parent: Optional["DeltaGenerator"] = None,
block_type: Optional[str] = None,
root_container: int | None = RootContainer.MAIN,
cursor: Cursor | None = None,
parent: DeltaGenerator | None = None,
block_type: str | None = None,
) -> None:
"""Inserts or updates elements in Streamlit apps.
Expand Down Expand Up @@ -271,7 +269,7 @@ def __init__(
self._block_type = block_type

# If this an `st.form` block, this will get filled in.
self._form_data: Optional[FormData] = None
self._form_data: FormData | None = None

# Change the module of all mixin'ed functions to be st.delta_generator,
# instead of the original module (e.g. st.elements.markdown)
Expand Down Expand Up @@ -304,7 +302,7 @@ def __exit__(
return False

@property
def _active_dg(self) -> "DeltaGenerator":
def _active_dg(self) -> DeltaGenerator:
"""Return the DeltaGenerator that's currently 'active'.
If we are the main DeltaGenerator, and are inside a `with` block that
creates a container, our active_dg is that container. Otherwise,
Expand All @@ -322,7 +320,7 @@ def _active_dg(self) -> "DeltaGenerator":
return self

@property
def _main_dg(self) -> "DeltaGenerator":
def _main_dg(self) -> DeltaGenerator:
"""Return this DeltaGenerator's root - that is, the top-level ancestor
DeltaGenerator that we belong to (this generally means the st._main
DeltaGenerator).
Expand Down Expand Up @@ -363,7 +361,7 @@ def _parent_block_types(self) -> ParentBlockTypes:
"""Iterate all the block types used by this DeltaGenerator and all
its ancestor DeltaGenerators.
"""
current_dg: Optional[DeltaGenerator] = self
current_dg: DeltaGenerator | None = self
while current_dg is not None:
if current_dg._block_type is not None:
yield current_dg._block_type
Expand All @@ -373,7 +371,7 @@ def _count_num_of_parent_columns(self, parent_block_types: ParentBlockTypes) ->
return sum(1 for parent_block in parent_block_types if parent_block == "column")

@property
def _cursor(self) -> Optional[Cursor]:
def _cursor(self) -> Cursor | None:
"""Return our Cursor. This will be None if we're not running in a
ScriptThread - e.g., if we're running a "bare" script outside of
Streamlit.
Expand Down Expand Up @@ -409,71 +407,71 @@ def _get_delta_path_str(self) -> str:
def _enqueue( # type: ignore[misc]
self,
delta_type: str,
element_proto: "Message",
element_proto: Message,
return_value: None,
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
) -> "DeltaGenerator":
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> DeltaGenerator:
...

@overload
def _enqueue( # type: ignore[misc]
self,
delta_type: str,
element_proto: "Message",
element_proto: Message,
return_value: Type[NoValue],
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> None:
...

@overload
def _enqueue( # type: ignore[misc]
self,
delta_type: str,
element_proto: "Message",
element_proto: Message,
return_value: Value,
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> Value:
...

@overload
def _enqueue(
self,
delta_type: str,
element_proto: "Message",
element_proto: Message,
return_value: None = None,
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
) -> "DeltaGenerator":
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> DeltaGenerator:
...

@overload
def _enqueue(
self,
delta_type: str,
element_proto: "Message",
return_value: Union[None, Type[NoValue], Value] = None,
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
) -> Union["DeltaGenerator", None, Value]:
element_proto: Message,
return_value: Type[NoValue] | Value | None = None,
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> DeltaGenerator | Value | None:
...

def _enqueue(
self,
delta_type: str,
element_proto: "Message",
return_value: Union[None, Type[NoValue], Value] = None,
last_index: Optional[Hashable] = None,
element_width: Optional[int] = None,
element_height: Optional[int] = None,
) -> Union["DeltaGenerator", None, Value]:
element_proto: Message,
return_value: Type[NoValue] | Value | None = None,
last_index: Hashable | None = None,
element_width: int | None = None,
element_height: int | None = None,
) -> DeltaGenerator | Value | None:
"""Create NewElement delta, fill it, and enqueue it.
Parameters
Expand Down Expand Up @@ -573,7 +571,7 @@ def _enqueue(
def _block(
self,
block_proto: Block_pb2.Block = Block_pb2.Block(),
) -> "DeltaGenerator":
) -> DeltaGenerator:
# Operate on the active DeltaGenerator, in case we're in a `with` block.
dg = self._active_dg

Expand Down Expand Up @@ -641,11 +639,13 @@ def _block(

def _legacy_add_rows(
self: DG,
data: "Data" = None,
**kwargs: Union[
"DataFrame", "npt.NDArray[Any]", Iterable[Any], Dict[Hashable, Any], None
],
) -> Optional[DG]:
data: Data = None,
**kwargs: DataFrame
| npt.NDArray[Any]
| Iterable[Any]
| dict[Hashable, Any]
| None,
) -> DG | None:
"""Concatenate a dataframe to the bottom of the current one.
Parameters
Expand Down Expand Up @@ -756,11 +756,13 @@ def _legacy_add_rows(

def _arrow_add_rows(
self: DG,
data: "Data" = None,
**kwargs: Union[
"DataFrame", "npt.NDArray[Any]", Iterable[Any], Dict[Hashable, Any], None
],
) -> Optional[DG]:
data: Data = None,
**kwargs: DataFrame
| npt.NDArray[Any]
| Iterable[Any]
| dict[Hashable, Any]
| None,
) -> DG | None:
"""Concatenate a dataframe to the bottom of the current one.
Parameters
Expand Down Expand Up @@ -877,12 +879,10 @@ def _maybe_melt_data_for_add_rows(
data: DFT,
delta_type: str,
last_index: Any,
) -> Tuple[Union[DFT, "DataFrame"], Union[int, Any]]:
) -> tuple[DFT | DataFrame, int | Any]:
import pandas as pd

def _melt_data(
df: "DataFrame", last_index: Any
) -> Tuple["DataFrame", Union[int, Any]]:
def _melt_data(df: DataFrame, last_index: Any) -> tuple[DataFrame, int | Any]:
if isinstance(df.index, pd.RangeIndex):
old_step = _get_pandas_index_attr(df, "step")

Expand Down Expand Up @@ -928,9 +928,9 @@ def _melt_data(


def _get_pandas_index_attr(
data: "Union[DataFrame, Series]",
data: DataFrame | Series,
attr: str,
) -> Optional[Any]:
) -> Any | None:
return getattr(data.index, attr, None)


Expand Down Expand Up @@ -958,9 +958,9 @@ def _value_or_dg(value: Value, dg: DG) -> Value:


def _value_or_dg(
value: Union[None, Type[NoValue], Value],
value: Type[NoValue] | Value | None,
dg: DG,
) -> Union[DG, None, Value]:
) -> DG | Value | None:
"""Return either value, or None, or dg.
This is needed because Widgets have meaningful return values. This is
Expand Down

0 comments on commit 9749510

Please sign in to comment.