Skip to content

Commit

Permalink
type hinting, backwards compatibility,
Browse files Browse the repository at this point in the history
  • Loading branch information
ragardner committed Jan 31, 2024
1 parent c468ad7 commit ff62eec
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 92 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
- `set_options()` keyword argument `row_index_width` renamed `default_row_index_width`
- Move doc files to new docs folder
- Delete `version.py` file, move `__version__` variable to `__init__.py`
- Add backwards compatibility for `Sheet()` initialization parameters:
- `column_width`
- `header_height`
- `row_height`
- `row_index_width`

### Version 7.0.1
#### Removed:
Expand Down
2 changes: 0 additions & 2 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ def __init__(
default_row_index: Literal["letters", "numbers", "both"] = "numbers",
data_reference: None | Sequence[Sequence[object]] = None,
data: None | Sequence[Sequence[object]] = None,
# either (start row, end row, "rows"), (start column, end column, "rows") or
# (cells start row, cells start column, cells end row, cells end column, "cells") # noqa: E501
startup_select: tuple[int, int, str] | tuple[int, int, int, int, str] = None,
startup_focus: bool = True,
total_columns: int | None = None,
Expand Down
31 changes: 20 additions & 11 deletions tksheet/column_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(self, *args, **kwargs):
self.align = kwargs["header_align"]
self.basic_bindings()

def basic_bindings(self, enable=True):
def basic_bindings(self, enable: bool = True):
if enable:
self.bind("<Motion>", self.mouse_motion)
self.bind("<ButtonPress-1>", self.b1_press)
Expand Down Expand Up @@ -167,7 +167,7 @@ def mousewheel(self, event: object):
self.lines_start_at -= 1
self.MT.main_table_redraw_grid_and_text(redraw_header=True, redraw_row_index=False, redraw_table=False)

def set_height(self, new_height, set_TL=False):
def set_height(self, new_height: int, set_TL: bool = False) -> None:
self.current_height = new_height
try:
self.config(height=new_height)
Expand All @@ -176,7 +176,7 @@ def set_height(self, new_height, set_TL=False):
if set_TL and self.TL is not None:
self.TL.set_dimensions(new_h=new_height)

def rc(self, event: object):
def rc(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
self.focus_set()
popup_menu = None
Expand Down Expand Up @@ -204,7 +204,7 @@ def rc(self, event: object):
self.popup_menu_loc = c
popup_menu.tk_popup(event.x_root, event.y_root)

def ctrl_b1_press(self, event: object):
def ctrl_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
if (
(self.drag_and_drop_enabled or self.col_selection_enabled)
Expand All @@ -228,7 +228,7 @@ def ctrl_b1_press(self, event: object):
elif not self.MT.ctrl_select_enabled:
self.b1_press(event)

def ctrl_shift_b1_press(self, event: object):
def ctrl_shift_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
x = event.x
c = self.MT.identify_col(x=x)
Expand Down Expand Up @@ -262,7 +262,7 @@ def ctrl_shift_b1_press(self, event: object):
elif not self.MT.ctrl_select_enabled:
self.shift_b1_press(event)

def shift_b1_press(self, event: object):
def shift_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
x = event.x
c = self.MT.identify_col(x=x)
Expand All @@ -289,13 +289,22 @@ def shift_b1_press(self, event: object):
to_move=sorted(self.MT.get_selected_cols()),
)

def get_shift_select_box(self, c, min_c):
def get_shift_select_box(self, c: int, min_c: int) -> tuple[int, int, int, int, str]:
if c > min_c:
return (0, min_c, len(self.MT.row_positions) - 1, c + 1, "columns")
elif c < min_c:
return (0, c, len(self.MT.row_positions) - 1, min_c + 1, "columns")

def create_resize_line(self, x1, y1, x2, y2, width, fill, tag):
def create_resize_line(
self,
x1: int,
y1: int,
x2: int,
y2: int,
width: int,
fill: str,
tag: str | tuple[str],
) -> None:
if self.hidd_resize_lines:
t, sh = self.hidd_resize_lines.popitem()
self.coords(t, x1, y1, x2, y2)
Expand All @@ -308,20 +317,20 @@ def create_resize_line(self, x1, y1, x2, y2, width, fill, tag):
t = self.create_line(x1, y1, x2, y2, width=width, fill=fill, tag=tag)
self.disp_resize_lines[t] = True

def delete_resize_lines(self):
def delete_resize_lines(self) -> None:
self.hidd_resize_lines.update(self.disp_resize_lines)
self.disp_resize_lines = {}
for t, sh in self.hidd_resize_lines.items():
if sh:
self.itemconfig(t, tags=("",), state="hidden")
self.hidd_resize_lines[t] = False

def check_mouse_position_width_resizers(self, x, y):
def check_mouse_position_width_resizers(self, x: int, y: int) -> int | None:
for c, (x1, y1, x2, y2) in self.visible_col_dividers.items():
if x >= x1 and y >= y1 and x <= x2 and y <= y2:
return c

def mouse_motion(self, event: object):
def mouse_motion(self, event: object) -> None:
if not self.currently_resizing_height and not self.currently_resizing_width:
x = self.canvasx(event.x)
y = self.canvasy(event.y)
Expand Down
28 changes: 19 additions & 9 deletions tksheet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def event_dict(
being_selected: None | tuple = None,
named_spans: None | dict = None,
**kwargs,
) -> dict:
) -> EventDataDict:
return EventDataDict(
eventname="" if name is None else name,
sheetname="!sheet" if sheet is None else sheet,
Expand Down Expand Up @@ -157,12 +157,12 @@ def event_dict(
)


def change_eventname(event_dict: dict, newname: str) -> EventDataDict:
def change_eventname(event_dict: EventDataDict, newname: str) -> EventDataDict:
return EventDataDict({**event_dict, **{"eventname": newname}})


def ev_stack_dict(d) -> EventDataDict:
return EventDataDict(
def ev_stack_dict(d: DotDict) -> DotDict:
return DotDict(
name=d["eventname"],
data=pickle_compress(d),
)
Expand Down Expand Up @@ -297,7 +297,10 @@ def get_n2a(n: int = 0, _type: str = "numbers") -> str:
return f"{num2alpha(n)} {n + 1}"


def get_index_of_gap_in_sorted_integer_seq_forward(seq, start=0):
def get_index_of_gap_in_sorted_integer_seq_forward(
seq: list[int],
start: int = 0,
) -> int | None:
prevn = seq[start]
for idx, n in enumerate(islice(seq, start + 1, None), start + 1):
if n != prevn + 1:
Expand All @@ -306,7 +309,10 @@ def get_index_of_gap_in_sorted_integer_seq_forward(seq, start=0):
return None


def get_index_of_gap_in_sorted_integer_seq_reverse(seq, start=0):
def get_index_of_gap_in_sorted_integer_seq_reverse(
seq: list[int],
start: int = 0,
) -> int | None:
prevn = seq[start]
for idx, n in zip(range(start, -1, -1), reversed(seq[:start])):
if n != prevn - 1:
Expand All @@ -315,7 +321,11 @@ def get_index_of_gap_in_sorted_integer_seq_reverse(seq, start=0):
return None


def get_seq_without_gaps_at_index(seq, position, get_st_end=False):
def get_seq_without_gaps_at_index(
seq: list[int],
position: int,
get_st_end: bool = False,
) -> tuple[int, int] | list[int]:
start_idx = bisect.bisect_left(seq, position)
forward_gap = get_index_of_gap_in_sorted_integer_seq_forward(seq, start_idx)
reverse_gap = get_index_of_gap_in_sorted_integer_seq_reverse(seq, start_idx)
Expand All @@ -328,7 +338,7 @@ def get_seq_without_gaps_at_index(seq, position, get_st_end=False):
return seq


def consecutive_chunks(seq: list) -> list:
def consecutive_chunks(seq: list[object]) -> list[object]:
if not seq:
yield seq
start = 0
Expand All @@ -349,7 +359,7 @@ def is_contiguous(seq: list[int]) -> bool:
return all(i == (prev := prev + 1) for i in itr)


def index_exists(seq: Sequence, index: int) -> bool:
def index_exists(seq: Sequence[object], index: int) -> bool:
try:
seq[index]
return True
Expand Down
31 changes: 20 additions & 11 deletions tksheet/row_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(self, *args, **kwargs):
self.default_index = kwargs["default_row_index"].lower()
self.basic_bindings()

def basic_bindings(self, enable=True):
def basic_bindings(self, enable: bool = True) -> None:
if enable:
self.bind("<Motion>", self.mouse_motion)
self.bind("<ButtonPress-1>", self.b1_press)
Expand All @@ -144,7 +144,7 @@ def basic_bindings(self, enable=True):
self.unbind("<Double-Button-1>")
self.unbind(rc_binding)

def set_width(self, new_width, set_TL=False):
def set_width(self, new_width: int, set_TL: bool = False) -> None:
self.current_width = new_width
try:
self.config(width=new_width)
Expand All @@ -153,7 +153,7 @@ def set_width(self, new_width, set_TL=False):
if set_TL:
self.TL.set_dimensions(new_w=new_width)

def rc(self, event: object):
def rc(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
self.focus_set()
popup_menu = None
Expand Down Expand Up @@ -181,7 +181,7 @@ def rc(self, event: object):
self.popup_menu_loc = r
popup_menu.tk_popup(event.x_root, event.y_root)

def ctrl_b1_press(self, event: object):
def ctrl_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
if (
(self.drag_and_drop_enabled or self.row_selection_enabled)
Expand All @@ -205,7 +205,7 @@ def ctrl_b1_press(self, event: object):
elif not self.MT.ctrl_select_enabled:
self.b1_press(event)

def ctrl_shift_b1_press(self, event: object):
def ctrl_shift_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
y = event.y
r = self.MT.identify_row(y=y)
Expand Down Expand Up @@ -239,7 +239,7 @@ def ctrl_shift_b1_press(self, event: object):
elif not self.MT.ctrl_select_enabled:
self.shift_b1_press(event)

def shift_b1_press(self, event: object):
def shift_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
y = event.y
r = self.MT.identify_row(y=y)
Expand All @@ -266,13 +266,22 @@ def shift_b1_press(self, event: object):
to_move=sorted(self.MT.get_selected_rows()),
)

def get_shift_selection_box(self, r, min_r):
def get_shift_selection_box(self, r: int, min_r: int) -> tuple[int, int, int, int, str]:
if r > min_r:
return (min_r, 0, r + 1, len(self.MT.col_positions) - 1, "rows")
elif r < min_r:
return (r, 0, min_r + 1, len(self.MT.col_positions) - 1, "rows")

def create_resize_line(self, x1, y1, x2, y2, width, fill, tag):
def create_resize_line(
self,
x1: int,
y1: int,
x2: int,
y2: int,
width: int,
fill: str,
tag: str | tuple[str],
) -> None:
if self.hidd_resize_lines:
t, sh = self.hidd_resize_lines.popitem()
self.coords(t, x1, y1, x2, y2)
Expand All @@ -285,20 +294,20 @@ def create_resize_line(self, x1, y1, x2, y2, width, fill, tag):
t = self.create_line(x1, y1, x2, y2, width=width, fill=fill, tag=tag)
self.disp_resize_lines[t] = True

def delete_resize_lines(self):
def delete_resize_lines(self) -> None:
self.hidd_resize_lines.update(self.disp_resize_lines)
self.disp_resize_lines = {}
for t, sh in self.hidd_resize_lines.items():
if sh:
self.itemconfig(t, tags=("",), state="hidden")
self.hidd_resize_lines[t] = False

def check_mouse_position_height_resizers(self, x, y):
def check_mouse_position_height_resizers(self, x: int, y: int) -> int | None:
for r, (x1, y1, x2, y2) in self.visible_row_dividers.items():
if x >= x1 and y >= y1 and x <= x2 and y <= y2:
return r

def mouse_motion(self, event: object):
def mouse_motion(self, event: object) -> None:
if not self.currently_resizing_height and not self.currently_resizing_width:
x = self.canvasx(event.x)
y = self.canvasy(event.y)
Expand Down
13 changes: 9 additions & 4 deletions tksheet/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ def __init__(
show_horizontal_grid: bool = True,
display_selected_fg_over_highlights: bool = False,
show_selected_cells_border: bool = True,
# backwards compatibility
column_width: int | None = None,
header_height: str | int | None = None,
row_height: str | int | None = None,
row_index_width: int | None = None,
) -> None:
tk.Frame.__init__(
self,
Expand Down Expand Up @@ -268,10 +273,10 @@ def __init__(
max_header_height=max_header_height,
max_row_height=max_row_height,
max_index_width=max_index_width,
default_row_index_width=default_row_index_width,
default_header_height=default_header_height,
default_column_width=default_column_width,
default_row_height=default_row_height,
default_row_index_width=default_row_index_width if row_index_width is None else row_index_width,
default_header_height=default_header_height if header_height is None else header_height,
default_column_width=default_column_width if column_width is None else column_width,
default_row_height=default_row_height if row_height is None else row_height,
show_index=show_row_index,
show_header=show_header,
column_headers_canvas=self.CH,
Expand Down
Loading

0 comments on commit ff62eec

Please sign in to comment.