Skip to content

Commit 61b54b4

Browse files
authored
Openpyxl: type cell values (#13929)
1 parent 5c7fe07 commit 61b54b4

File tree

6 files changed

+40
-42
lines changed

6 files changed

+40
-42
lines changed

stubs/openpyxl/openpyxl/cell/__init__.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from .cell import Cell as Cell, MergedCell as MergedCell, WriteOnlyCell as Write
1010
from .read_only import ReadOnlyCell as ReadOnlyCell
1111

1212
_TimeTypes: TypeAlias = datetime | date | time | timedelta
13-
_CellValue: TypeAlias = ( # noqa: Y047 # Used in other modules
13+
_CellGetValue: TypeAlias = ( # noqa: Y047 # Used in other modules
1414
# if numpy is installed also numpy bool and number types
1515
bool
1616
| float
@@ -20,7 +20,9 @@ _CellValue: TypeAlias = ( # noqa: Y047 # Used in other modules
2020
| _TimeTypes
2121
| DataTableFormula
2222
| ArrayFormula
23+
| None
2324
)
24-
_AnyCellValue: TypeAlias = Any # Any of _CellValue # noqa: Y047 # Used in other modules
25+
_AnyCellValue: TypeAlias = Any # AnyOf _CellGetValue # noqa: Y047 # Used in other modules
26+
_CellSetValue: TypeAlias = _CellGetValue | bytes # noqa: Y047 # Used in other modules
2527

2628
_CellOrMergedCell: TypeAlias = Cell | MergedCell # noqa: Y047 # Used in other modules

stubs/openpyxl/openpyxl/cell/cell.pyi

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from datetime import datetime
33
from re import Pattern
44
from typing import Final, Literal, overload
55

6-
from openpyxl.cell import _CellOrMergedCell, _CellValue, _TimeTypes
6+
from openpyxl.cell import _CellGetValue, _CellOrMergedCell, _CellSetValue, _TimeTypes
77
from openpyxl.comments.comments import Comment
88
from openpyxl.compat.numbers import NUMERIC_TYPES as NUMERIC_TYPES # cell numeric types
99
from openpyxl.styles.cell_style import StyleArray
@@ -45,7 +45,7 @@ class Cell(StyleableObject):
4545
worksheet: _WorkbookChild | ReadOnlyWorksheet,
4646
row: int,
4747
column: int,
48-
value: str | float | datetime | None = None,
48+
value: _CellSetValue = None,
4949
style_array: StyleArray | None = None,
5050
) -> None: ...
5151
@property
@@ -64,11 +64,11 @@ class Cell(StyleableObject):
6464
def check_string(self, value: str | ReadableBuffer) -> str: ...
6565
def check_error(self, value: object) -> str: ...
6666
@property
67-
def value(self) -> _CellValue | None: ...
67+
def value(self) -> _CellGetValue: ...
6868
@value.setter
69-
def value(self, value: _CellValue | bytes | None) -> None: ...
69+
def value(self, value: _CellSetValue) -> None: ...
7070
@property
71-
def internal_value(self) -> _CellValue | None: ...
71+
def internal_value(self) -> _CellGetValue: ...
7272
@property
7373
def hyperlink(self) -> Hyperlink | None: ...
7474
@hyperlink.setter
@@ -94,6 +94,7 @@ class MergedCell(StyleableObject):
9494
# https://github.com/python/mypy/issues/6700
9595
@property
9696
def coordinate(self) -> str: ...
97-
value: str | float | int | datetime | None
97+
# The value of a MergedCell is always None.
98+
value: None
9899

99100
def WriteOnlyCell(ws: _WorkbookChild | ReadOnlyWorksheet, value: str | float | datetime | None = None) -> Cell: ...

stubs/openpyxl/openpyxl/cell/read_only.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from _typeshed import Incomplete
22
from typing import Final
33

4-
from openpyxl.cell import _CellValue
4+
from openpyxl.cell import _CellGetValue
55
from openpyxl.styles.alignment import Alignment
66
from openpyxl.styles.borders import Border
77
from openpyxl.styles.cell_style import StyleArray
@@ -51,9 +51,9 @@ class ReadOnlyCell:
5151
@property
5252
def is_date(self) -> bool: ...
5353
@property
54-
def internal_value(self) -> _CellValue | None: ...
54+
def internal_value(self) -> _CellGetValue: ...
5555
@property
56-
def value(self) -> _CellValue | None: ...
56+
def value(self) -> _CellGetValue: ...
5757
@value.setter
5858
def value(self, value: None) -> None: ...
5959

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from _typeshed import Incomplete
1+
from .reference import Reference
22

33
def SeriesFactory(
4-
values,
5-
xvalues: Incomplete | None = None,
6-
zvalues: Incomplete | None = None,
4+
values: Reference | str,
5+
xvalues: Reference | str | None = None,
6+
zvalues: Reference | str | None = None,
77
title: object = None,
88
title_from_data: bool = False,
99
): ...

stubs/openpyxl/openpyxl/worksheet/_read_only.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from _typeshed import SupportsGetItem
22
from collections.abc import Generator
33

44
from openpyxl import _VisibilityType
5-
from openpyxl.cell import _CellOrMergedCell, _CellValue
5+
from openpyxl.cell import _CellGetValue, _CellOrMergedCell
66
from openpyxl.utils.cell import _RangeBoundariesTuple
77
from openpyxl.workbook.workbook import Workbook
88
from openpyxl.worksheet.worksheet import Worksheet
@@ -15,7 +15,7 @@ class ReadOnlyWorksheet:
1515
# Same as Worksheet.values
1616
# https://github.com/python/mypy/issues/6700
1717
@property
18-
def values(self) -> Generator[tuple[_CellValue, ...], None, None]: ...
18+
def values(self) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
1919
# Same as Worksheet.rows
2020
# https://github.com/python/mypy/issues/6700
2121
@property

stubs/openpyxl/openpyxl/worksheet/worksheet.pyi

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from _typeshed import ConvertibleToInt, Incomplete
22
from collections.abc import Generator, Iterable, Iterator
3-
from datetime import datetime
43
from types import GeneratorType
54
from typing import Any, Final, Literal, NoReturn, overload
65
from typing_extensions import deprecated
76

87
from openpyxl import _Decodable, _VisibilityType
9-
from openpyxl.cell import _CellOrMergedCell, _CellValue
8+
from openpyxl.cell import _AnyCellValue, _CellGetValue, _CellOrMergedCell, _CellSetValue
109
from openpyxl.cell.cell import Cell
1110
from openpyxl.chart._chart import ChartBase
1211
from openpyxl.drawing.image import Image
@@ -87,7 +86,11 @@ class Worksheet(_WorkbookChild):
8786
def freeze_panes(self) -> str | None: ...
8887
@freeze_panes.setter
8988
def freeze_panes(self, topLeftCell: str | Cell | None = None) -> None: ...
90-
def cell(self, row: int, column: int, value: _CellValue | None = None) -> _CellOrMergedCell: ...
89+
# A MergedCell value should be kept to None
90+
@overload
91+
def cell(self, row: int, column: int, value: None = None) -> _CellOrMergedCell: ...
92+
@overload
93+
def cell(self, row: int, column: int, value: _CellSetValue = None) -> Cell: ...
9194
# An int is necessarily a row selection
9295
@overload
9396
def __getitem__(self, key: int) -> tuple[_CellOrMergedCell, ...]: ...
@@ -99,7 +102,7 @@ class Worksheet(_WorkbookChild):
99102
def __getitem__(
100103
self, key: str
101104
) -> Any: ... # AnyOf[_CellOrMergedCell, tuple[_CellOrMergedCell, ...], tuple[tuple[_CellOrMergedCell, ...], ...]]
102-
def __setitem__(self, key: str, value: _CellValue) -> None: ...
105+
def __setitem__(self, key: str, value: _CellSetValue) -> None: ...
103106
def __iter__(self) -> Iterator[tuple[_CellOrMergedCell, ...]]: ...
104107
def __delitem__(self, key: str) -> None: ...
105108
@property
@@ -116,7 +119,7 @@ class Worksheet(_WorkbookChild):
116119
@overload
117120
def iter_rows(
118121
self, min_row: int | None, max_row: int | None, min_col: int | None, max_col: int | None, values_only: Literal[True]
119-
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
122+
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
120123
@overload
121124
def iter_rows(
122125
self,
@@ -126,7 +129,7 @@ class Worksheet(_WorkbookChild):
126129
max_col: int | None = None,
127130
*,
128131
values_only: Literal[True],
129-
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
132+
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
130133
@overload
131134
def iter_rows(
132135
self,
@@ -139,9 +142,7 @@ class Worksheet(_WorkbookChild):
139142
@overload
140143
def iter_rows(
141144
self, min_row: int | None, max_row: int | None, min_col: int | None, max_col: int | None, values_only: bool
142-
) -> (
143-
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
144-
): ...
145+
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
145146
@overload
146147
def iter_rows(
147148
self,
@@ -151,17 +152,15 @@ class Worksheet(_WorkbookChild):
151152
max_col: int | None = None,
152153
*,
153154
values_only: bool,
154-
) -> (
155-
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
156-
): ...
155+
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
157156
@property
158157
def rows(self) -> Generator[tuple[_CellOrMergedCell, ...], None, None]: ...
159158
@property
160-
def values(self) -> Generator[tuple[_CellValue | None, ...]]: ...
159+
def values(self) -> Generator[tuple[_CellGetValue, ...]]: ...
161160
@overload
162161
def iter_cols(
163162
self, min_col: int | None, max_col: int | None, min_row: int | None, max_row: int | None, values_only: Literal[True]
164-
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
163+
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
165164
@overload
166165
def iter_cols(
167166
self,
@@ -171,7 +170,7 @@ class Worksheet(_WorkbookChild):
171170
max_row: int | None = None,
172171
*,
173172
values_only: Literal[True],
174-
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
173+
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
175174
@overload
176175
def iter_cols(
177176
self,
@@ -184,9 +183,7 @@ class Worksheet(_WorkbookChild):
184183
@overload
185184
def iter_cols(
186185
self, min_col: int | None, max_col: int | None, min_row: int | None, max_row: int | None, values_only: bool
187-
) -> (
188-
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
189-
): ...
186+
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
190187
@overload
191188
def iter_cols(
192189
self,
@@ -196,9 +193,7 @@ class Worksheet(_WorkbookChild):
196193
max_row: int | None = None,
197194
*,
198195
values_only: bool,
199-
) -> (
200-
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
201-
): ...
196+
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
202197
@property
203198
def columns(self) -> Generator[tuple[_CellOrMergedCell, ...], None, None]: ...
204199
@property
@@ -252,11 +247,11 @@ class Worksheet(_WorkbookChild):
252247
def append(
253248
self,
254249
iterable: (
255-
list[Any] # lists are invariant, but any subtype or union will do
256-
| tuple[_CellOrMergedCell | str | float | datetime | None, ...]
250+
list[_AnyCellValue]
251+
| tuple[_CellOrMergedCell | _CellGetValue, ...]
257252
| range
258-
| GeneratorType[_CellOrMergedCell | str | float | datetime | None, object, object]
259-
| dict[int | str, str | float | datetime | None]
253+
| GeneratorType[_CellOrMergedCell | _CellGetValue, object, object]
254+
| dict[int | str, _AnyCellValue]
260255
),
261256
) -> None: ...
262257
def insert_rows(self, idx: int, amount: int = 1) -> None: ...

0 commit comments

Comments
 (0)