Skip to content

Commit c30c85d

Browse files
authored
Type all @property in openpyxl (#10787)
1 parent 741f751 commit c30c85d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+245
-174
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1+
from datetime import date, datetime, time, timedelta
2+
from decimal import Decimal
3+
from typing_extensions import TypeAlias
4+
5+
from openpyxl.cell.rich_text import CellRichText
6+
from openpyxl.worksheet.formula import ArrayFormula, DataTableFormula
7+
18
from .cell import Cell as Cell, MergedCell as MergedCell, WriteOnlyCell as WriteOnlyCell
29
from .read_only import ReadOnlyCell as ReadOnlyCell
10+
11+
_TimeTypes: TypeAlias = datetime | date | time | timedelta
12+
_CellValue: TypeAlias = ( # noqa: Y047 # Used in other modules
13+
# if numpy is installed also numpy bool and number types
14+
bool
15+
| float
16+
| Decimal
17+
| str
18+
| CellRichText
19+
| _TimeTypes
20+
| DataTableFormula
21+
| ArrayFormula
22+
)

stubs/openpyxl/openpyxl/cell/cell.pyi

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
from datetime import date, datetime, time, timedelta
2-
from decimal import Decimal
1+
from _typeshed import ReadableBuffer
2+
from datetime import datetime
33
from re import Pattern
44
from typing import overload
5-
from typing_extensions import Final, TypeAlias
5+
from typing_extensions import Final
66

7-
from openpyxl.cell.rich_text import CellRichText
7+
from openpyxl.cell import _CellValue, _TimeTypes
88
from openpyxl.comments.comments import Comment
99
from openpyxl.styles.cell_style import StyleArray
1010
from openpyxl.styles.styleable import StyleableObject
11-
from openpyxl.worksheet.formula import ArrayFormula, DataTableFormula
1211
from openpyxl.worksheet.hyperlink import Hyperlink
1312
from openpyxl.worksheet.worksheet import Worksheet
1413

15-
_TimeTypes: TypeAlias = datetime | date | time | timedelta
16-
_CellValue: TypeAlias = ( # if numpy is installed also numpy bool and number types
17-
bool | float | Decimal | str | CellRichText | _TimeTypes | DataTableFormula | ArrayFormula
18-
)
19-
2014
__docformat__: Final = "restructuredtext en"
2115
TIME_TYPES: Final[tuple[type, ...]]
2216
TIME_FORMATS: Final[dict[type[_TimeTypes], str]]
@@ -65,14 +59,14 @@ class Cell(StyleableObject):
6559
@overload
6660
def check_string(self, value: None) -> None: ...
6761
@overload
68-
def check_string(self, value: str | bytes) -> str: ...
62+
def check_string(self, value: str | ReadableBuffer) -> str: ...
6963
def check_error(self, value: object) -> str: ...
7064
@property
71-
def value(self) -> _CellValue: ...
65+
def value(self) -> _CellValue | None: ...
7266
@value.setter
7367
def value(self, value: _CellValue | bytes | None) -> None: ...
7468
@property
75-
def internal_value(self) -> _CellValue: ...
69+
def internal_value(self) -> _CellValue | None: ...
7670
@property
7771
def hyperlink(self) -> Hyperlink | None: ...
7872
@hyperlink.setter
@@ -92,6 +86,7 @@ class MergedCell(StyleableObject):
9286
row: int
9387
column: int
9488
def __init__(self, worksheet: Worksheet, row: int | None = None, column: int | None = None) -> None: ...
89+
# Same as Cell.coordinate
9590
@property
9691
def coordinate(self) -> str: ...
9792
value: str | float | int | datetime | None

stubs/openpyxl/openpyxl/cell/read_only.pyi

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from _typeshed import Incomplete
22
from typing_extensions import Final
33

4+
from openpyxl.cell import _CellValue
5+
from openpyxl.styles.alignment import Alignment
6+
from openpyxl.styles.borders import Border
7+
from openpyxl.styles.cell_style import StyleArray
8+
from openpyxl.styles.fills import Fill
9+
from openpyxl.styles.fonts import Font
10+
from openpyxl.styles.protection import Protection
11+
412
class ReadOnlyCell:
513
parent: Incomplete
614
row: Incomplete
@@ -9,35 +17,38 @@ class ReadOnlyCell:
917
def __init__(self, sheet, row, column, value, data_type: str = "n", style_id: int = 0) -> None: ...
1018
def __eq__(self, other): ...
1119
def __ne__(self, other): ...
12-
# defined twice in the implementation
20+
# Same as Cell.coordinate
21+
# Defined twice in the implementation
1322
@property
14-
def coordinate(self): ...
23+
def coordinate(self) -> str: ...
24+
# Same as Cell.column_letter
1525
@property
16-
def column_letter(self): ...
26+
def column_letter(self) -> str: ...
1727
@property
18-
def style_array(self): ...
28+
def style_array(self) -> StyleArray: ...
1929
@property
20-
def has_style(self): ...
30+
def has_style(self) -> bool: ...
2131
@property
22-
def number_format(self): ...
32+
def number_format(self) -> str: ...
2333
@property
24-
def font(self): ...
34+
def font(self) -> Font: ...
2535
@property
26-
def fill(self): ...
36+
def fill(self) -> Fill: ...
2737
@property
28-
def border(self): ...
38+
def border(self) -> Border: ...
2939
@property
30-
def alignment(self): ...
40+
def alignment(self) -> Alignment: ...
3141
@property
32-
def protection(self): ...
42+
def protection(self) -> Protection: ...
43+
# Same as Cell.is_date
3344
@property
34-
def is_date(self): ...
45+
def is_date(self) -> bool: ...
3546
@property
36-
def internal_value(self): ...
47+
def internal_value(self) -> _CellValue | None: ...
3748
@property
38-
def value(self): ...
49+
def value(self) -> _CellValue | None: ...
3950
@value.setter
40-
def value(self, value) -> None: ...
51+
def value(self, value: None) -> None: ...
4152

4253
class EmptyCell:
4354
value: Incomplete

stubs/openpyxl/openpyxl/cell/text.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ class Text(Serialisable):
9595
__elements__: ClassVar[tuple[str, ...]]
9696
def __init__(self, t: object = None, r=(), rPh=(), phoneticPr: _PhoneticProperties | None = None) -> None: ...
9797
@property
98-
def content(self): ...
98+
def content(self) -> str: ...

stubs/openpyxl/openpyxl/chart/_chart.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ class ChartBase(Serialisable):
4646
def add_data(self, data, from_rows: bool = False, titles_from_data: bool = False) -> None: ...
4747
def append(self, value) -> None: ...
4848
@property
49-
def path(self): ...
49+
def path(self) -> str: ...

stubs/openpyxl/openpyxl/chart/reference.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class Reference(Strict):
4141
def __len__(self) -> int: ...
4242
def __eq__(self, other): ...
4343
@property
44-
def rows(self) -> Generator[Incomplete, None, None]: ...
44+
def rows(self) -> Generator[Reference, None, None]: ...
4545
@property
46-
def cols(self) -> Generator[Incomplete, None, None]: ...
46+
def cols(self) -> Generator[Reference, None, None]: ...
4747
def pop(self): ...
4848
@property
49-
def sheetname(self): ...
49+
def sheetname(self) -> str: ...

stubs/openpyxl/openpyxl/comments/comment_sheet.pyi

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from typing_extensions import Literal, TypeAlias
55

66
from openpyxl.cell.text import Text
77
from openpyxl.comments.author import AuthorList
8+
from openpyxl.comments.comments import Comment
89
from openpyxl.descriptors.base import Bool, Integer, Set, String, Typed, _ConvertibleToBool, _ConvertibleToInt
910
from openpyxl.descriptors.excel import ExtensionList
1011
from openpyxl.descriptors.serialisable import Serialisable
@@ -101,7 +102,7 @@ class CommentRecord(Serialisable):
101102
@classmethod
102103
def from_cell(cls, cell): ...
103104
@property
104-
def content(self): ...
105+
def content(self) -> str: ...
105106

106107
class CommentSheet(Serialisable):
107108
tagname: ClassVar[str]
@@ -113,9 +114,9 @@ class CommentSheet(Serialisable):
113114
def __init__(self, authors: AuthorList, commentList: Incomplete | None = None, extLst: Unused = None) -> None: ...
114115
def to_tree(self): ...
115116
@property
116-
def comments(self) -> Generator[Incomplete, None, None]: ...
117+
def comments(self) -> Generator[tuple[str, Comment], None, None]: ...
117118
@classmethod
118119
def from_comments(cls, comments): ...
119120
def write_shapes(self, vml: Incomplete | None = None): ...
120121
@property
121-
def path(self): ...
122+
def path(self) -> str: ...
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from _typeshed import Incomplete
2+
from typing import Any
23

34
class Comment:
45
content: Incomplete
@@ -7,12 +8,12 @@ class Comment:
78
width: Incomplete
89
def __init__(self, text, author, height: int = 79, width: int = 144) -> None: ...
910
@property
10-
def parent(self): ...
11+
def parent(self) -> Any: ... # AnyOf[Cell, MergedCell, ReadOnlyCell]
1112
def __eq__(self, other): ...
1213
def __copy__(self): ...
1314
def bind(self, cell) -> None: ...
1415
def unbind(self) -> None: ...
1516
@property
16-
def text(self): ...
17+
def text(self) -> str: ...
1718
@text.setter
18-
def text(self, value) -> None: ...
19+
def text(self, value: str) -> None: ...

stubs/openpyxl/openpyxl/drawing/drawing.pyi

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from _typeshed import Incomplete
22

3+
from .spreadsheet_drawing import AbsoluteAnchor, OneCellAnchor
4+
35
class Drawing:
46
count: int
57
name: str
@@ -14,13 +16,13 @@ class Drawing:
1416
anchorrow: int
1517
def __init__(self) -> None: ...
1618
@property
17-
def width(self): ...
19+
def width(self) -> int: ...
1820
@width.setter
19-
def width(self, w) -> None: ...
21+
def width(self, w: int) -> None: ...
2022
@property
21-
def height(self): ...
23+
def height(self) -> int: ...
2224
@height.setter
23-
def height(self, h) -> None: ...
25+
def height(self, h: int) -> None: ...
2426
def set_dimension(self, w: int = 0, h: int = 0) -> None: ...
2527
@property
26-
def anchor(self): ...
28+
def anchor(self) -> AbsoluteAnchor | OneCellAnchor: ...

stubs/openpyxl/openpyxl/drawing/image.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ class Image:
66
format: Incomplete
77
def __init__(self, img) -> None: ...
88
@property
9-
def path(self): ...
9+
def path(self) -> str: ...

stubs/openpyxl/openpyxl/drawing/spreadsheet_drawing.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ class SpreadsheetDrawing(Serialisable):
118118
def __hash__(self) -> int: ...
119119
def __bool__(self) -> bool: ...
120120
@property
121-
def path(self): ...
121+
def path(self) -> str: ...

stubs/openpyxl/openpyxl/formula/tokenizer.pyi

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
from _typeshed import Incomplete
22
from re import Pattern
3-
from typing_extensions import Final
3+
from typing_extensions import Final, Literal, TypeAlias
4+
5+
_TokenTypesNotOperand: TypeAlias = Literal[
6+
"LITERAL", "FUNC", "ARRAY", "PAREN", "SEP", "OPERATOR-PREFIX", "OPERATOR-INFIX", "OPERATOR-POSTFIX", "WHITE-SPACE"
7+
]
8+
_TokenTypes: TypeAlias = Literal["OPERAND", _TokenTypesNotOperand]
9+
_TokenOperandSubtypes: TypeAlias = Literal["TEXT", "NUMBER", "LOGICAL", "ERROR", "RANGE"]
10+
_TokenSubtypes: TypeAlias = Literal["", _TokenOperandSubtypes, "OPEN", "CLOSE", "ARG", "ROW"]
411

512
class TokenizerError(Exception): ...
613

@@ -33,9 +40,9 @@ class Token:
3340
OP_POST: Final = "OPERATOR-POSTFIX"
3441
WSPACE: Final = "WHITE-SPACE"
3542
value: Incomplete
36-
type: Incomplete
37-
subtype: Incomplete
38-
def __init__(self, value, type_, subtype: str = "") -> None: ...
43+
type: _TokenTypes
44+
subtype: _TokenSubtypes
45+
def __init__(self, value, type_: _TokenTypes, subtype: _TokenSubtypes = "") -> None: ...
3946
TEXT: Final = "TEXT"
4047
NUMBER: Final = "NUMBER"
4148
LOGICAL: Final = "LOGICAL"
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from abc import ABC, ABCMeta, abstractmethod
1+
from abc import ABC, abstractmethod
22

3-
class ISerialisableFile(ABC, metaclass=ABCMeta):
3+
# This interface is unused. Nothing implements `id` as property either.
4+
# IDs can be ints, strings, None, or a Descriptor returning those throughout the codebase.
5+
class ISerialisableFile(ABC):
46
@property
57
@abstractmethod
6-
def id(self): ...
8+
def id(self) -> str | int | None: ...

stubs/openpyxl/openpyxl/packaging/manifest.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class Manifest(Serialisable):
3131
__elements__: ClassVar[tuple[str, ...]]
3232
def __init__(self, Default=(), Override=()) -> None: ...
3333
@property
34-
def filenames(self): ...
34+
def filenames(self) -> list[str]: ...
3535
@property
36-
def extensions(self): ...
36+
def extensions(self) -> list[tuple[str, str]]: ...
3737
def to_tree(self): ...
3838
def __contains__(self, content_type): ...
3939
def find(self, content_type): ...

stubs/openpyxl/openpyxl/packaging/workbook.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ class WorkbookPackage(Serialisable):
101101
) -> None: ...
102102
def to_tree(self): ...
103103
@property
104-
def active(self): ...
104+
def active(self) -> int: ...

stubs/openpyxl/openpyxl/pivot/cache.pyi

+5-5
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class ServerFormatList(Serialisable):
101101
__attrs__: ClassVar[tuple[str, ...]]
102102
def __init__(self, count: Incomplete | None = None, serverFormat: Incomplete | None = None) -> None: ...
103103
@property
104-
def count(self): ...
104+
def count(self) -> int: ...
105105

106106
class Query(Serialisable):
107107
tagname: ClassVar[str]
@@ -384,7 +384,7 @@ class GroupItems(Serialisable):
384384
__attrs__: ClassVar[tuple[str, ...]]
385385
def __init__(self, count: Incomplete | None = None, m=(), n=(), b=(), e=(), s=(), d=()) -> None: ...
386386
@property
387-
def count(self): ...
387+
def count(self) -> int: ...
388388

389389
class DiscretePr(Serialisable):
390390
tagname: ClassVar[str]
@@ -475,7 +475,7 @@ class SharedItems(Serialisable):
475475
longText: _ConvertibleToBool | None = None,
476476
) -> None: ...
477477
@property
478-
def count(self): ...
478+
def count(self) -> int: ...
479479

480480
class CacheField(Serialisable):
481481
tagname: ClassVar[str]
@@ -585,7 +585,7 @@ class Page(Serialisable):
585585
__elements__: ClassVar[tuple[str, ...]]
586586
def __init__(self, count: Incomplete | None = None, pageItem: Incomplete | None = None) -> None: ...
587587
@property
588-
def count(self): ...
588+
def count(self) -> int: ...
589589

590590
class Consolidation(Serialisable):
591591
tagname: ClassVar[str]
@@ -723,4 +723,4 @@ class CacheDefinition(Serialisable):
723723
) -> None: ...
724724
def to_tree(self): ...
725725
@property
726-
def path(self): ...
726+
def path(self) -> str: ...

stubs/openpyxl/openpyxl/pivot/record.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class RecordList(Serialisable):
3737
__attrs__: ClassVar[tuple[str, ...]]
3838
def __init__(self, count: Unused = None, r=(), extLst: ExtensionList | None = None) -> None: ...
3939
@property
40-
def count(self): ...
40+
def count(self) -> int: ...
4141
def to_tree(self): ...
4242
@property
43-
def path(self): ...
43+
def path(self) -> str: ...

0 commit comments

Comments
 (0)