Skip to content

Openpyxl: type cell values #13929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions stubs/openpyxl/openpyxl/cell/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ from .cell import Cell as Cell, MergedCell as MergedCell, WriteOnlyCell as Write
from .read_only import ReadOnlyCell as ReadOnlyCell

_TimeTypes: TypeAlias = datetime | date | time | timedelta
_CellValue: TypeAlias = ( # noqa: Y047 # Used in other modules
_CellGetValue: TypeAlias = ( # noqa: Y047 # Used in other modules
# if numpy is installed also numpy bool and number types
bool
| float
Expand All @@ -20,7 +20,9 @@ _CellValue: TypeAlias = ( # noqa: Y047 # Used in other modules
| _TimeTypes
| DataTableFormula
| ArrayFormula
| None
)
_AnyCellValue: TypeAlias = Any # Any of _CellValue # noqa: Y047 # Used in other modules
_AnyCellValue: TypeAlias = Any # AnyOf _CellGetValue # noqa: Y047 # Used in other modules
_CellSetValue: TypeAlias = _CellGetValue | bytes # noqa: Y047 # Used in other modules

_CellOrMergedCell: TypeAlias = Cell | MergedCell # noqa: Y047 # Used in other modules
13 changes: 7 additions & 6 deletions stubs/openpyxl/openpyxl/cell/cell.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from datetime import datetime
from re import Pattern
from typing import Final, Literal, overload

from openpyxl.cell import _CellOrMergedCell, _CellValue, _TimeTypes
from openpyxl.cell import _CellGetValue, _CellOrMergedCell, _CellSetValue, _TimeTypes
from openpyxl.comments.comments import Comment
from openpyxl.compat.numbers import NUMERIC_TYPES as NUMERIC_TYPES # cell numeric types
from openpyxl.styles.cell_style import StyleArray
Expand Down Expand Up @@ -45,7 +45,7 @@ class Cell(StyleableObject):
worksheet: _WorkbookChild | ReadOnlyWorksheet,
row: int,
column: int,
value: str | float | datetime | None = None,
value: _CellSetValue = None,
style_array: StyleArray | None = None,
) -> None: ...
@property
Expand All @@ -64,11 +64,11 @@ class Cell(StyleableObject):
def check_string(self, value: str | ReadableBuffer) -> str: ...
def check_error(self, value: object) -> str: ...
@property
def value(self) -> _CellValue | None: ...
def value(self) -> _CellGetValue: ...
@value.setter
def value(self, value: _CellValue | bytes | None) -> None: ...
def value(self, value: _CellSetValue) -> None: ...
@property
def internal_value(self) -> _CellValue | None: ...
def internal_value(self) -> _CellGetValue: ...
@property
def hyperlink(self) -> Hyperlink | None: ...
@hyperlink.setter
Expand All @@ -94,6 +94,7 @@ class MergedCell(StyleableObject):
# https://github.com/python/mypy/issues/6700
@property
def coordinate(self) -> str: ...
value: str | float | int | datetime | None
# The value of a MergedCell is always None.
value: None

def WriteOnlyCell(ws: _WorkbookChild | ReadOnlyWorksheet, value: str | float | datetime | None = None) -> Cell: ...
6 changes: 3 additions & 3 deletions stubs/openpyxl/openpyxl/cell/read_only.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from _typeshed import Incomplete
from typing import Final

from openpyxl.cell import _CellValue
from openpyxl.cell import _CellGetValue
from openpyxl.styles.alignment import Alignment
from openpyxl.styles.borders import Border
from openpyxl.styles.cell_style import StyleArray
Expand Down Expand Up @@ -51,9 +51,9 @@ class ReadOnlyCell:
@property
def is_date(self) -> bool: ...
@property
def internal_value(self) -> _CellValue | None: ...
def internal_value(self) -> _CellGetValue: ...
@property
def value(self) -> _CellValue | None: ...
def value(self) -> _CellGetValue: ...
@value.setter
def value(self, value: None) -> None: ...

Expand Down
8 changes: 4 additions & 4 deletions stubs/openpyxl/openpyxl/chart/series_factory.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from _typeshed import Incomplete
from .reference import Reference

def SeriesFactory(
values,
xvalues: Incomplete | None = None,
zvalues: Incomplete | None = None,
values: Reference | str,
xvalues: Reference | str | None = None,
zvalues: Reference | str | None = None,
title: object = None,
title_from_data: bool = False,
): ...
4 changes: 2 additions & 2 deletions stubs/openpyxl/openpyxl/worksheet/_read_only.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from _typeshed import SupportsGetItem
from collections.abc import Generator

from openpyxl import _VisibilityType
from openpyxl.cell import _CellOrMergedCell, _CellValue
from openpyxl.cell import _CellGetValue, _CellOrMergedCell
from openpyxl.utils.cell import _RangeBoundariesTuple
from openpyxl.workbook.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet
Expand All @@ -15,7 +15,7 @@ class ReadOnlyWorksheet:
# Same as Worksheet.values
# https://github.com/python/mypy/issues/6700
@property
def values(self) -> Generator[tuple[_CellValue, ...], None, None]: ...
def values(self) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
# Same as Worksheet.rows
# https://github.com/python/mypy/issues/6700
@property
Expand Down
45 changes: 20 additions & 25 deletions stubs/openpyxl/openpyxl/worksheet/worksheet.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from _typeshed import ConvertibleToInt, Incomplete
from collections.abc import Generator, Iterable, Iterator
from datetime import datetime
from types import GeneratorType
from typing import Any, Final, Literal, NoReturn, overload
from typing_extensions import deprecated

from openpyxl import _Decodable, _VisibilityType
from openpyxl.cell import _CellOrMergedCell, _CellValue
from openpyxl.cell import _AnyCellValue, _CellGetValue, _CellOrMergedCell, _CellSetValue
from openpyxl.cell.cell import Cell
from openpyxl.chart._chart import ChartBase
from openpyxl.drawing.image import Image
Expand Down Expand Up @@ -87,7 +86,11 @@ class Worksheet(_WorkbookChild):
def freeze_panes(self) -> str | None: ...
@freeze_panes.setter
def freeze_panes(self, topLeftCell: str | Cell | None = None) -> None: ...
def cell(self, row: int, column: int, value: _CellValue | None = None) -> _CellOrMergedCell: ...
# A MergedCell value should be kept to None
@overload
def cell(self, row: int, column: int, value: None = None) -> _CellOrMergedCell: ...
@overload
def cell(self, row: int, column: int, value: _CellSetValue = None) -> Cell: ...
# An int is necessarily a row selection
@overload
def __getitem__(self, key: int) -> tuple[_CellOrMergedCell, ...]: ...
Expand All @@ -99,7 +102,7 @@ class Worksheet(_WorkbookChild):
def __getitem__(
self, key: str
) -> Any: ... # AnyOf[_CellOrMergedCell, tuple[_CellOrMergedCell, ...], tuple[tuple[_CellOrMergedCell, ...], ...]]
def __setitem__(self, key: str, value: _CellValue) -> None: ...
def __setitem__(self, key: str, value: _CellSetValue) -> None: ...
def __iter__(self) -> Iterator[tuple[_CellOrMergedCell, ...]]: ...
def __delitem__(self, key: str) -> None: ...
@property
Expand All @@ -116,7 +119,7 @@ class Worksheet(_WorkbookChild):
@overload
def iter_rows(
self, min_row: int | None, max_row: int | None, min_col: int | None, max_col: int | None, values_only: Literal[True]
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_rows(
self,
Expand All @@ -126,7 +129,7 @@ class Worksheet(_WorkbookChild):
max_col: int | None = None,
*,
values_only: Literal[True],
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_rows(
self,
Expand All @@ -139,9 +142,7 @@ class Worksheet(_WorkbookChild):
@overload
def iter_rows(
self, min_row: int | None, max_row: int | None, min_col: int | None, max_col: int | None, values_only: bool
) -> (
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
): ...
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_rows(
self,
Expand All @@ -151,17 +152,15 @@ class Worksheet(_WorkbookChild):
max_col: int | None = None,
*,
values_only: bool,
) -> (
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
): ...
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
@property
def rows(self) -> Generator[tuple[_CellOrMergedCell, ...], None, None]: ...
@property
def values(self) -> Generator[tuple[_CellValue | None, ...]]: ...
def values(self) -> Generator[tuple[_CellGetValue, ...]]: ...
@overload
def iter_cols(
self, min_col: int | None, max_col: int | None, min_row: int | None, max_row: int | None, values_only: Literal[True]
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_cols(
self,
Expand All @@ -171,7 +170,7 @@ class Worksheet(_WorkbookChild):
max_row: int | None = None,
*,
values_only: Literal[True],
) -> Generator[tuple[str | float | datetime | None, ...], None, None]: ...
) -> Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_cols(
self,
Expand All @@ -184,9 +183,7 @@ class Worksheet(_WorkbookChild):
@overload
def iter_cols(
self, min_col: int | None, max_col: int | None, min_row: int | None, max_row: int | None, values_only: bool
) -> (
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
): ...
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
@overload
def iter_cols(
self,
Expand All @@ -196,9 +193,7 @@ class Worksheet(_WorkbookChild):
max_row: int | None = None,
*,
values_only: bool,
) -> (
Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[str | float | datetime | None, ...], None, None]
): ...
) -> Generator[tuple[_CellOrMergedCell, ...], None, None] | Generator[tuple[_CellGetValue, ...], None, None]: ...
@property
def columns(self) -> Generator[tuple[_CellOrMergedCell, ...], None, None]: ...
@property
Expand Down Expand Up @@ -252,11 +247,11 @@ class Worksheet(_WorkbookChild):
def append(
self,
iterable: (
list[Any] # lists are invariant, but any subtype or union will do
| tuple[_CellOrMergedCell | str | float | datetime | None, ...]
list[_AnyCellValue]
| tuple[_CellOrMergedCell | _CellGetValue, ...]
| range
| GeneratorType[_CellOrMergedCell | str | float | datetime | None, object, object]
| dict[int | str, str | float | datetime | None]
| GeneratorType[_CellOrMergedCell | _CellGetValue, object, object]
| dict[int | str, _AnyCellValue]
),
) -> None: ...
def insert_rows(self, idx: int, amount: int = 1) -> None: ...
Expand Down