-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Additional type hints added to stub openpyxl
#9487
Changes from all commits
b68f9eb
948d5f6
ad01e57
dd58853
ed44b07
933b9f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,12 @@ | ||||||||||
from _typeshed import Incomplete | ||||||||||
from datetime import datetime | ||||||||||
from typing import Any | ||||||||||
|
||||||||||
from openpyxl.comments.comments import Comment | ||||||||||
from openpyxl.styles.cell_style import StyleArray | ||||||||||
from openpyxl.styles.styleable import StyleableObject | ||||||||||
from openpyxl.worksheet.hyperlink import Hyperlink | ||||||||||
from openpyxl.worksheet.worksheet import Worksheet | ||||||||||
|
||||||||||
__docformat__: str | ||||||||||
TIME_TYPES: Incomplete | ||||||||||
|
@@ -19,60 +25,60 @@ TYPE_ERROR: str | |||||||||
TYPE_FORMULA_CACHE_STRING: str | ||||||||||
VALID_TYPES: Incomplete | ||||||||||
|
||||||||||
def get_type(t, value): ... | ||||||||||
def get_time_format(t): ... | ||||||||||
def get_type(t: Any, value: Any) -> Any: ... | ||||||||||
def get_time_format(t: Any) -> Any: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
class Cell(StyleableObject): | ||||||||||
row: Incomplete | ||||||||||
column: Incomplete | ||||||||||
row: int | ||||||||||
column: int | ||||||||||
data_type: str | ||||||||||
def __init__( | ||||||||||
self, | ||||||||||
worksheet, | ||||||||||
row: Incomplete | None = ..., | ||||||||||
column: Incomplete | None = ..., | ||||||||||
value: Incomplete | None = ..., | ||||||||||
style_array: Incomplete | None = ..., | ||||||||||
worksheet: Worksheet, | ||||||||||
row: int | None = ..., | ||||||||||
column: int | None = ..., | ||||||||||
value: str | float | int | datetime | None = ..., | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP 484 specifies that functions annotated as accepting
Suggested change
|
||||||||||
style_array: StyleArray | None = ..., | ||||||||||
) -> None: ... | ||||||||||
@property | ||||||||||
def coordinate(self): ... | ||||||||||
def coordinate(self) -> str: ... | ||||||||||
@property | ||||||||||
def col_idx(self): ... | ||||||||||
def col_idx(self) -> int: ... | ||||||||||
@property | ||||||||||
def column_letter(self): ... | ||||||||||
def column_letter(self) -> str: ... | ||||||||||
@property | ||||||||||
def encoding(self): ... | ||||||||||
def encoding(self) -> str: ... | ||||||||||
@property | ||||||||||
def base_date(self): ... | ||||||||||
def check_string(self, value): ... | ||||||||||
def check_error(self, value): ... | ||||||||||
def base_date(self) -> Any: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This property is a >>> from openpyxl import Workbook
>>> cell = Workbook().active['A1']
>>> type(cell.base_date)
<class 'datetime.datetime'>
Suggested change
|
||||||||||
def check_string(self, value: str): ... | ||||||||||
def check_error(self, value: Any) -> Any | str: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
@property | ||||||||||
def value(self): ... | ||||||||||
def value(self) -> str | float | int | datetime | None: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
@value.setter | ||||||||||
def value(self, value) -> None: ... | ||||||||||
def value(self, value: str | float | int | datetime | None) -> None: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
@property | ||||||||||
def internal_value(self): ... | ||||||||||
def internal_value(self) -> Any: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this just have the same type as the |
||||||||||
@property | ||||||||||
def hyperlink(self): ... | ||||||||||
def hyperlink(self) -> Hyperlink | str: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Moreover, if you do
Suggested change
|
||||||||||
@hyperlink.setter | ||||||||||
def hyperlink(self, val) -> None: ... | ||||||||||
def hyperlink(self, val: Hyperlink | str | None) -> None: ... | ||||||||||
@property | ||||||||||
def is_date(self): ... | ||||||||||
def offset(self, row: int = ..., column: int = ...): ... | ||||||||||
def is_date(self) -> bool: ... | ||||||||||
def offset(self, row: int = ..., column: int = ...) -> Cell: ... | ||||||||||
@property | ||||||||||
def comment(self): ... | ||||||||||
def comment(self) -> Comment: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be
Suggested change
|
||||||||||
@comment.setter | ||||||||||
def comment(self, value) -> None: ... | ||||||||||
def comment(self, value: Comment) -> None: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's okay to pass
Suggested change
|
||||||||||
|
||||||||||
class MergedCell(StyleableObject): | ||||||||||
data_type: str | ||||||||||
comment: Incomplete | ||||||||||
hyperlink: Incomplete | ||||||||||
row: Incomplete | ||||||||||
column: Incomplete | ||||||||||
def __init__(self, worksheet, row: Incomplete | None = ..., column: Incomplete | None = ...) -> None: ... | ||||||||||
comment: Comment | ||||||||||
hyperlink: Hyperlink | ||||||||||
Comment on lines
+75
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both of these attributes have
Suggested change
|
||||||||||
row: int | ||||||||||
column: int | ||||||||||
def __init__(self, worksheet: Worksheet, row: int | None = ..., column: int | None = ...) -> None: ... | ||||||||||
@property | ||||||||||
def coordinate(self): ... | ||||||||||
value: Incomplete | ||||||||||
def coordinate(self) -> str: ... | ||||||||||
value: str | float | int | datetime | None | ||||||||||
|
||||||||||
def WriteOnlyCell(ws: Incomplete | None = ..., value: Incomplete | None = ...): ... | ||||||||||
def WriteOnlyCell(ws: Worksheet | None = ..., value: str | float | int | datetime | None = ...) -> Cell: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,8 @@ | ||||||
from _typeshed import Incomplete | ||||||
from typing import Any | ||||||
|
||||||
from openpyxl.chartsheet.chartsheet import Chartsheet | ||||||
from openpyxl.workbook.workbook import Workbook | ||||||
|
||||||
SUPPORTED_FORMATS: Incomplete | ||||||
|
||||||
|
@@ -10,7 +14,7 @@ class ExcelReader: | |||||
data_only: Incomplete | ||||||
keep_links: Incomplete | ||||||
shared_strings: Incomplete | ||||||
def __init__(self, fn, read_only: bool = ..., keep_vba=..., data_only: bool = ..., keep_links: bool = ...) -> None: ... | ||||||
def __init__(self, fn: Any, read_only: bool = ..., keep_vba=..., data_only: bool = ..., keep_links: bool = ...) -> None: ... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We have a protocol in the
Suggested change
|
||||||
package: Incomplete | ||||||
def read_manifest(self) -> None: ... | ||||||
def read_strings(self) -> None: ... | ||||||
|
@@ -19,8 +23,10 @@ class ExcelReader: | |||||
def read_workbook(self) -> None: ... | ||||||
def read_properties(self) -> None: ... | ||||||
def read_theme(self) -> None: ... | ||||||
def read_chartsheet(self, sheet, rel) -> None: ... | ||||||
def read_chartsheet(self, sheet: Chartsheet, rel: Any) -> None: ... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely sure what type
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rel is a Relationship from openpyxl.packaging.relationship |
||||||
def read_worksheets(self) -> None: ... | ||||||
def read(self) -> None: ... | ||||||
|
||||||
def load_workbook(filename, read_only: bool = ..., keep_vba=..., data_only: bool = ..., keep_links: bool = ...): ... | ||||||
def load_workbook( | ||||||
filename: str, read_only: bool = ..., keep_vba: bool = ..., data_only: bool = ..., keep_links: bool = ... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the
Suggested change
|
||||||
) -> Workbook: ... |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,9 @@ | ||||||||
from _typeshed import Incomplete | ||||||||
from typing import Any, Iterator | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer to use PEP 585 imports wherever possible
Suggested change
|
||||||||
|
||||||||
from openpyxl.chartsheet.chartsheet import Chartsheet | ||||||||
from openpyxl.styles.named_styles import NamedStyle | ||||||||
from openpyxl.worksheet.worksheet import Worksheet | ||||||||
|
||||||||
INTEGER_TYPES: Incomplete | ||||||||
|
||||||||
|
@@ -20,54 +25,54 @@ class Workbook: | |||||||
views: Incomplete | ||||||||
def __init__(self, write_only: bool = ..., iso_dates: bool = ...) -> None: ... | ||||||||
@property | ||||||||
def epoch(self): ... | ||||||||
def epoch(self) -> Any: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is always a
Suggested change
|
||||||||
@epoch.setter | ||||||||
def epoch(self, value) -> None: ... | ||||||||
def epoch(self, value: Any) -> None: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This raises
Suggested change
|
||||||||
@property | ||||||||
def read_only(self): ... | ||||||||
def read_only(self) -> bool: ... | ||||||||
@property | ||||||||
def data_only(self): ... | ||||||||
def data_only(self) -> bool: ... | ||||||||
@property | ||||||||
def write_only(self): ... | ||||||||
def write_only(self) -> bool: ... | ||||||||
@property | ||||||||
def excel_base_date(self): ... | ||||||||
def excel_base_date(self) -> Any: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
@property | ||||||||
def active(self): ... | ||||||||
def active(self) -> Worksheet: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed with None. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring indicates that it's a https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/3.0/openpyxl/workbook/workbook.py#L154 |
||||||||
@active.setter | ||||||||
def active(self, value) -> None: ... | ||||||||
def create_sheet(self, title: Incomplete | None = ..., index: Incomplete | None = ...): ... | ||||||||
def move_sheet(self, sheet, offset: int = ...) -> None: ... | ||||||||
def remove(self, worksheet) -> None: ... | ||||||||
def remove_sheet(self, worksheet) -> None: ... | ||||||||
def create_chartsheet(self, title: Incomplete | None = ..., index: Incomplete | None = ...): ... | ||||||||
def get_sheet_by_name(self, name): ... | ||||||||
def __contains__(self, key): ... | ||||||||
def index(self, worksheet): ... | ||||||||
def get_index(self, worksheet): ... | ||||||||
def __getitem__(self, key): ... | ||||||||
def __delitem__(self, key) -> None: ... | ||||||||
def __iter__(self): ... | ||||||||
def get_sheet_names(self): ... | ||||||||
def active(self, value: Worksheet) -> None: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed with Int. |
||||||||
def create_sheet(self, title: str | None = ..., index: int | None = ...): ... | ||||||||
def move_sheet(self, sheet: Worksheet, offset: int = ...) -> None: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be
Suggested change
|
||||||||
def remove(self, worksheet: Worksheet) -> None: ... | ||||||||
def remove_sheet(self, worksheet: Worksheet) -> None: ... | ||||||||
def create_chartsheet(self, title: str | None = ..., index: int | None = ...) -> None: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't return
Suggested change
|
||||||||
def get_sheet_by_name(self, name: str) -> Worksheet: ... | ||||||||
def __contains__(self, key: str) -> bool: ... | ||||||||
def index(self, worksheet: Worksheet) -> int: ... | ||||||||
def get_index(self, worksheet: Worksheet) -> int: ... | ||||||||
def __getitem__(self, key: str) -> Worksheet: ... | ||||||||
def __delitem__(self, key: str) -> None: ... | ||||||||
def __iter__(self) -> Iterator[Worksheet]: ... | ||||||||
def get_sheet_names(self) -> list[Worksheet]: ... | ||||||||
@property | ||||||||
def worksheets(self): ... | ||||||||
def worksheets(self) -> list[Worksheet]: ... | ||||||||
@property | ||||||||
def chartsheets(self): ... | ||||||||
def chartsheets(self) -> list[Chartsheet]: ... | ||||||||
@property | ||||||||
def sheetnames(self): ... | ||||||||
def sheetnames(self) -> list[str]: ... | ||||||||
def create_named_range( | ||||||||
self, name, worksheet: Incomplete | None = ..., value: Incomplete | None = ..., scope: Incomplete | None = ... | ||||||||
self, name: str, worksheet: Worksheet | None = ..., value: str | Any | None = ..., scope: Incomplete | None = ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to continue to use
Suggested change
|
||||||||
) -> None: ... | ||||||||
def add_named_style(self, style) -> None: ... | ||||||||
def add_named_style(self, style: Any) -> None: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This calls
Suggested change
|
||||||||
@property | ||||||||
def named_styles(self): ... | ||||||||
def get_named_ranges(self): ... | ||||||||
def add_named_range(self, named_range) -> None: ... | ||||||||
def get_named_range(self, name): ... | ||||||||
def remove_named_range(self, named_range) -> None: ... | ||||||||
def named_styles(self) -> list[NamedStyle]: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a list of
Suggested change
|
||||||||
def get_named_ranges(self) -> list[Any]: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't return a
The |
||||||||
def add_named_range(self, named_range: Any) -> None: ... | ||||||||
def get_named_range(self, name: Any) -> Any: ... | ||||||||
def remove_named_range(self, named_range: Any) -> None: ... | ||||||||
Comment on lines
+69
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave these unannotated if you don't know what the type is. In typeshed, we use |
||||||||
@property | ||||||||
def mime_type(self): ... | ||||||||
def save(self, filename) -> None: ... | ||||||||
def mime_type(self) -> Any: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave this unannotated if you don't know what the type is |
||||||||
def save(self, filename: str) -> None: ... | ||||||||
@property | ||||||||
def style_names(self): ... | ||||||||
def copy_worksheet(self, from_worksheet): ... | ||||||||
def style_names(self) -> list[NamedStyle]: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a list of
Suggested change
|
||||||||
def copy_worksheet(self, from_worksheet: Worksheet) -> Worksheet: ... | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't return WorksheetCopy, but rather a Worksheet or WriteOnlyWorksheet. The WorksheetCopy operatates on the result of Workbook.create_sheet(), but doesn't itself get returned. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||||||||
def close(self) -> None: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t
looks like it has to be a class of some kind, due to this line here, where it's used as a key in a dictionary that only has classes as keys: https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/3.0/openpyxl/cell/cell.py#L73value
could be literally any object, so it's better to useobject
here rather thanAny
.Any
is an "escape hatch" in the type system for things that are truly inexpressible. It's very unsafe, so it's better to useobject
whenever possible.str
orNone
.