Skip to content

Commit a3c0633

Browse files
mattmess1221jmcnamara
authored andcommitted
worksheet: add overload signatures for cell, range and column methods
1 parent aa04e9b commit a3c0633

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

.github/workflows/python-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Install test dependencies
3131
run: |
3232
python -m pip install --upgrade pip
33-
pip install pytest flake8 black ruff isort pylint polars pandas
33+
pip install pytest flake8 black ruff isort pylint polars pandas typing-extensions
3434
3535
- name: Test the code style
3636
run: |

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ testpythons:
4646
@~/.pythonbrew/pythons/Python-3.14.0/bin/py.test -q
4747

4848
test_flake8:
49-
@ls -1 xlsxwriter/*.py | egrep -v "theme|__init__" | xargs flake8 --show-source --max-line-length=88 --ignore=E203,W503
49+
@ls -1 xlsxwriter/*.py | egrep -v "theme|__init__" | xargs flake8 --show-source --max-line-length=88 --ignore=E203,E704,W503
5050
@flake8 --ignore=E501 xlsxwriter/theme.py
5151
@find xlsxwriter/test -name \*.py | xargs flake8 --ignore=E501,F841,W503
5252

xlsxwriter/worksheet.py

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
from functools import wraps
2222
from io import BytesIO, StringIO
2323
from math import isinf, isnan
24-
from typing import Any, Dict, List, Literal, Optional, Union
24+
from typing import (
25+
TYPE_CHECKING,
26+
Any,
27+
Callable,
28+
Dict,
29+
List,
30+
Literal,
31+
Optional,
32+
TypeVar,
33+
Union,
34+
)
2535
from warnings import warn
2636

2737
from xlsxwriter.chart import Chart
@@ -52,6 +62,59 @@
5262
)
5363
from .xmlwriter import XMLwriter
5464

65+
if TYPE_CHECKING:
66+
from typing_extensions import Concatenate, ParamSpec, Protocol, overload
67+
68+
ReturnTypeT_co = TypeVar("ReturnTypeT_co", covariant=True)
69+
P = ParamSpec("P")
70+
71+
class CellMethod(Protocol[P, ReturnTypeT_co]):
72+
"""Overloads to support cell notation."""
73+
74+
@overload
75+
def __call__(
76+
self, row: int, col: int, /, *args: P.args, **kwargs: P.kwargs
77+
) -> ReturnTypeT_co: ...
78+
79+
@overload
80+
def __call__(
81+
self, cell: str, /, *args: P.args, **kwargs: P.kwargs
82+
) -> ReturnTypeT_co: ...
83+
84+
class RangeMethod(Protocol[P, ReturnTypeT_co]):
85+
"""Overloads to support range notation."""
86+
87+
@overload
88+
def __call__(
89+
self,
90+
first_row: int,
91+
first_col: int,
92+
last_row: int,
93+
last_col: int,
94+
/,
95+
*args: P.args,
96+
**kwargs: P.kwargs,
97+
) -> ReturnTypeT_co: ...
98+
99+
@overload
100+
def __call__(
101+
self, cell_range: str, /, *args: P.args, **kwargs: P.kwargs
102+
) -> ReturnTypeT_co: ...
103+
104+
class ColumnMethod(Protocol[P, ReturnTypeT_co]):
105+
"""Overloads to support column range notation."""
106+
107+
@overload
108+
def __call__(
109+
self, first_col: int, last_col: int, /, *args: P.args, **kwargs: P.kwargs
110+
) -> ReturnTypeT_co: ...
111+
112+
@overload
113+
def __call__(
114+
self, col_range: str, /, *args: P.args, **kwargs: P.kwargs
115+
) -> ReturnTypeT_co: ...
116+
117+
55118
re_dynamic_function = re.compile(
56119
r"""
57120
\bANCHORARRAY\( |
@@ -92,7 +155,9 @@
92155
# Decorator functions.
93156
#
94157
###############################################################################
95-
def convert_cell_args(method):
158+
def convert_cell_args(
159+
method: "Callable[Concatenate[Any, int, int, P], ReturnTypeT_co]",
160+
) -> "CellMethod[P, ReturnTypeT_co]":
96161
"""
97162
Decorator function to convert A1 notation in cell method calls
98163
to the default row/col notation.
@@ -116,7 +181,9 @@ def cell_wrapper(self, *args, **kwargs):
116181
return cell_wrapper
117182

118183

119-
def convert_range_args(method):
184+
def convert_range_args(
185+
method: "Callable[Concatenate[Any, int, int, int, int, P], ReturnTypeT_co]",
186+
) -> "RangeMethod[P, ReturnTypeT_co]":
120187
"""
121188
Decorator function to convert A1 notation in range method calls
122189
to the default row/col notation.
@@ -148,7 +215,9 @@ def cell_wrapper(self, *args, **kwargs):
148215
return cell_wrapper
149216

150217

151-
def convert_column_args(method):
218+
def convert_column_args(
219+
method: "Callable[Concatenate[Any, int, int, P], ReturnTypeT_co]",
220+
) -> "ColumnMethod[P, ReturnTypeT_co]":
152221
"""
153222
Decorator function to convert A1 notation in columns method calls
154223
to the default row/col notation.

0 commit comments

Comments
 (0)