Skip to content

Commit

Permalink
type updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Jul 24, 2022
1 parent de43fc1 commit 79fea2c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
24 changes: 8 additions & 16 deletions tabulous/_qt/_table/_base/_table_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Any, Callable, overload, TYPE_CHECKING
from typing import Any, Callable, TYPE_CHECKING
import warnings
from qtpy import QtWidgets as QtW, QtGui, QtCore
from qtpy.QtCore import Signal, Qt
Expand All @@ -10,7 +10,7 @@
from collections_undo import UndoManager
from ._model_base import AbstractDataFrameModel
from ..._utils import show_messagebox, QtKeys
from ....types import FilterType, ItemInfo, HeaderInfo, SelectionType
from ....types import FilterType, ItemInfo, HeaderInfo, SelectionType, _Sliceable

if TYPE_CHECKING:
from ._delegate import TableItemDelegate
Expand Down Expand Up @@ -398,6 +398,8 @@ def refresh(self) -> None:


class QMutableTable(QBaseTable):
"""A mutable table widget."""

itemChangedSignal = Signal(ItemInfo)
rowChangedSignal = Signal(HeaderInfo)
columnChangedSignal = Signal(HeaderInfo)
Expand Down Expand Up @@ -435,22 +437,11 @@ def convertValue(self, r: int, c: int, value: Any) -> Any:
"""Convert value before updating DataFrame."""
return value

@overload
def setDataFrameValue(self, r: int, c: int, value: Any) -> None:
...

@overload
def setDataFrameValue(self, r: slice, c: slice, value: pd.DataFrame) -> None:
...

def setDataFrameValue(self, r, c, value) -> None:
def setDataFrameValue(self, r: _Sliceable, c: _Sliceable, value: Any) -> None:
data = self._data_raw

# convert values
if isinstance(r, int) and isinstance(c, int):
_value = self.convertValue(r, c, value)
_is_scalar = True
elif isinstance(r, slice) and isinstance(c, slice):
if isinstance(r, slice) and isinstance(c, slice):
_value: pd.DataFrame = value
if _value.size == 1:
v = _value.values[0, 0]
Expand All @@ -466,7 +457,8 @@ def setDataFrameValue(self, r, c, value) -> None:
)
_is_scalar = False
else:
raise TypeError
_value = self.convertValue(r, c, value)
_is_scalar = True

# if table has filter, indices must be adjusted
if self._filter_slice is None:
Expand Down
5 changes: 4 additions & 1 deletion tabulous/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def _post_insert(self, value) -> None:


class TabPosition(Enum):
"""Enum for tab position."""

top = "top"
left = "left"
bottom = "bottom"
Expand All @@ -127,4 +129,5 @@ class HeaderInfo(NamedTuple):
old_value: Any


SelectionType = List[Tuple[Union[SupportsIndex, slice], Union[SupportsIndex, slice]]]
_Sliceable = Union[SupportsIndex, slice]
SelectionType = List[Tuple[_Sliceable, _Sliceable]]
3 changes: 2 additions & 1 deletion tabulous/widgets/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def add_layer(self, layer: TableBase):
self.current_index = -1 # activate the last table
return layer

def open(self, path: PathLike, *, type=TableType.table) -> None:
def open(self, path: PathLike, *, type: TableType | str = TableType.table) -> None:
"""
Read a table data and add to the viewer.
Expand Down Expand Up @@ -257,6 +257,7 @@ def save(self, path: PathLike) -> None:
raise ValueError(f"Extension {suf} not supported.")

def open_sample(self, sample_name: str, plugin: str = "seaborn") -> Table:
"""Open a sample table."""
df = open_sample(sample_name, plugin)
return self.add_table(df, name=sample_name)

Expand Down

0 comments on commit 79fea2c

Please sign in to comment.