-
-
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
openpyxl: Type usages of PIL
and zipfile
#10706
Changes from 4 commits
a2e8d15
f08c6f1
29c9218
52d38ff
2b71759
0e5c562
9c52815
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,9 +1,20 @@ | ||
from _typeshed import Incomplete | ||
from _typeshed import SupportsRead | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Any | ||
from typing_extensions import Literal, TypeAlias | ||
|
||
# Is actually PIL.Image.Image | ||
_PILImageImage: TypeAlias = Any | ||
# same as first parameter of PIL.Image.open | ||
_PILImageFilePath: TypeAlias = str | bytes | Path | SupportsRead[bytes] | ||
|
||
PILImage: ModuleType | Literal[False] | ||
|
||
class Image: | ||
anchor: str | ||
ref: Incomplete | ||
format: Incomplete | ||
def __init__(self, img) -> None: ... | ||
ref: _PILImageImage | _PILImageFilePath | ||
format: str | ||
def __init__(self, img: _PILImageImage | _PILImageFilePath) -> None: ... | ||
@property | ||
def path(self): ... | ||
def path(self) -> str: ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
def find_images(archive, path): ... | ||
from zipfile import ZipFile | ||
|
||
from openpyxl.chart._chart import ChartBase | ||
from openpyxl.drawing.image import Image | ||
|
||
def find_images(archive: ZipFile, path: str) -> tuple[list[ChartBase], list[Image]]: ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from _typeshed import Incomplete, StrPath, SupportsRead | ||
from _typeshed import Incomplete, StrPath | ||
from typing import IO | ||
from typing_extensions import Final, Literal, TypeAlias | ||
from zipfile import ZipFile | ||
|
||
|
@@ -26,7 +27,7 @@ class ExcelReader: | |
|
||
def __init__( | ||
self, | ||
fn: SupportsRead[bytes] | str, | ||
fn: StrPath | IO[bytes], | ||
read_only: bool = False, | ||
keep_vba: bool = False, | ||
data_only: bool = False, | ||
|
@@ -44,7 +45,7 @@ class ExcelReader: | |
def read(self) -> None: ... | ||
|
||
def load_workbook( | ||
filename: SupportsRead[bytes] | StrPath, | ||
filename: StrPath | IO[bytes], | ||
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. https://github.com/python/typeshed/pull/9511/files#r1113278179 Avasam
AlexWaygood
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 change is responsible for the 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. Given the primer hit I think we should bite the bullet, introduce a protocol in zipfile, and use an equivalent protocol here. 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 don't mind holding off on this PR until we have a proper Protocol defined for zipfile. I'll link #5835 as this is another use-case for it. Otherwise we'll have to duplicate the protocol in openpyxl for a while. 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 don't think #5835 is happening anytime soon so for now we'll just have to duplicate it. |
||
read_only: bool = False, | ||
keep_vba: bool = False, | ||
data_only: bool = False, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
from _typeshed import Incomplete | ||
from _typeshed import StrPath | ||
from typing import IO | ||
from typing_extensions import Literal | ||
from zipfile import ZipFile | ||
|
||
from openpyxl.packaging.manifest import Manifest | ||
from openpyxl.workbook.workbook import Workbook | ||
|
||
class ExcelWriter: | ||
workbook: Incomplete | ||
manifest: Incomplete | ||
vba_modified: Incomplete | ||
def __init__(self, workbook, archive) -> None: ... | ||
workbook: Workbook | ||
manifest: Manifest | ||
vba_modified: set[str | None] | ||
def __init__(self, workbook: Workbook, archive: ZipFile) -> None: ... | ||
def write_data(self) -> None: ... | ||
def write_worksheet(self, ws) -> None: ... | ||
def save(self) -> None: ... | ||
|
||
def save_workbook(workbook, filename): ... | ||
def save_workbook(workbook: Workbook, filename: StrPath | IO[bytes]) -> Literal[True]: ... |
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.
Isn't this redundant with the next overload?
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.
I wanna say you're right? Probably a leftover from having
id
being optional.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.
Not sure how doable it is, but I suggested catching this at mypy: python/mypy#16164