Skip to content
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

openpyxl: Use duck typing for workbook sheets #11718

Merged
merged 6 commits into from
Apr 27, 2024
Merged
Changes from 2 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
30 changes: 23 additions & 7 deletions stubs/openpyxl/openpyxl/workbook/workbook.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from _typeshed import Incomplete, Unused
from collections.abc import Iterator
from datetime import datetime
from typing import Any, Final
from typing import Any, Final, Protocol, type_check_only
from typing_extensions import TypeAlias, deprecated
from zipfile import ZipFile

Expand All @@ -17,6 +17,22 @@ from openpyxl.worksheet.worksheet import Worksheet
_WorkbookWorksheet: TypeAlias = Worksheet | WriteOnlyWorksheet | ReadOnlyWorksheet
_WorkbookSheet: TypeAlias = _WorkbookWorksheet | Chartsheet

# The type of worksheets in a workbook are the same as the `*Param` aliases above.
# However, because Worksheet adds a lots of attributes that other _WorkbookChild subclasses don't have
# (ReadOnlyWorksheet doesn't even inherit form it), this ends up being too disruptive
Avasam marked this conversation as resolved.
Show resolved Hide resolved
# to the typical usage of openpyxl where sheets are just Worksheets.
# Using Any may just loose too much type information and duck-typing
# from Worksheet works great here, even if the instance type might be wrong.
@type_check_only
class _WorksheetLike( # type: ignore[misc]
Worksheet, WriteOnlyWorksheet, ReadOnlyWorksheet, Protocol # pyright: ignore[reportGeneralTypeIssues]
Avasam marked this conversation as resolved.
Show resolved Hide resolved
): ...

@type_check_only
class _WorksheetOrChartsheetLike( # type: ignore[misc]
Chartsheet, _WorksheetLike, Protocol # pyright: ignore[reportGeneralTypeIssues]
): ...

INTEGER_TYPES: Final[tuple[type[int]]]

class Workbook:
Expand All @@ -37,7 +53,7 @@ class Workbook:
views: Incomplete
# Useful as a reference of what "sheets" can be for other types
# ExcelReader can add ReadOnlyWorksheet in read_only mode.
# _sheets: list[_WorkbookSheet]
# _sheets: list[_WorksheetOrChartsheetLike]
def __init__(self, write_only: bool = False, iso_dates: bool = False) -> None: ...
@property
def epoch(self) -> datetime: ...
Expand All @@ -52,7 +68,7 @@ class Workbook:
@property
def excel_base_date(self) -> datetime: ...
@property
def active(self) -> _WorkbookSheet | None: ...
def active(self) -> _WorksheetOrChartsheetLike | None: ...
@active.setter
def active(self, value: Worksheet | Chartsheet | int) -> None: ...
# read_only workbook cannot call this method
Expand All @@ -66,18 +82,18 @@ class Workbook:
def remove_sheet(self, worksheet: _WorkbookSheet) -> None: ...
def create_chartsheet(self, title: str | _Decodable | None = None, index: int | None = None) -> Chartsheet: ...
@deprecated("Use wb[sheetname]")
def get_sheet_by_name(self, name: str) -> _WorkbookSheet: ...
def get_sheet_by_name(self, name: str) -> _WorksheetOrChartsheetLike: ...
def __contains__(self, key: str) -> bool: ...
def index(self, worksheet: _WorkbookWorksheet) -> int: ...
@deprecated("Use wb.index(worksheet)")
def get_index(self, worksheet: _WorkbookWorksheet) -> int: ...
def __getitem__(self, key: str) -> _WorkbookSheet: ...
def __getitem__(self, key: str) -> _WorksheetOrChartsheetLike: ...
def __delitem__(self, key: str) -> None: ...
def __iter__(self) -> Iterator[_WorkbookWorksheet]: ...
def __iter__(self) -> Iterator[_WorksheetLike]: ...
@deprecated("Use wb.sheetnames")
def get_sheet_names(self) -> list[str]: ...
@property
def worksheets(self) -> list[_WorkbookWorksheet]: ...
def worksheets(self) -> list[_WorksheetLike]: ...
@property
def chartsheets(self) -> list[Chartsheet]: ...
@property
Expand Down
Loading