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 type disagreements #11710

Closed
multimeric opened this issue Apr 3, 2024 · 2 comments · Fixed by #11717
Closed

openpyxl type disagreements #11710

multimeric opened this issue Apr 3, 2024 · 2 comments · Fixed by #11717

Comments

@multimeric
Copy link

I originally posted this on the openpyxl GitLab but it seems that they don't maintain the type annotations.

The first issue is that worksheet[start:end] seems to return a tuple[tuple[Cell]], but the type annotations imply that it returns a tuple[cell].

You can see this issue in the following code:

from typing import reveal_type
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet

wb = Workbook()
ws = wb.active
if isinstance(ws, Worksheet):
    for cell in ws["A1":"A12"]:
        reveal_type(cell)

Type checkers believe one thing:

$ pyright tuple_excel.py
tuple_excel.py:9:21 - information: Type of "cell" is "Cell"

Python disagrees:

$ python tuple_excel.py
Runtime type is 'tuple'

Versions:

openpyxl==3.1.2
types-openpyxl==3.1.0.20240402
Python 3.12.2

The second issue relates to a single string index:

from typing import reveal_type
from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet

wb = Workbook()
ws = wb.active
if isinstance(ws, Worksheet):
    reveal_type(ws["A"])
$ python tuple_excel.py
Runtime type is 'tuple'
$ pyright tuple_excel.py
tuple_excel.py:8:17 - information: Type of "ws["A"]" is "Any"
@srittau
Copy link
Collaborator

srittau commented Apr 4, 2024

Cc @Avasam

@Avasam
Copy link
Collaborator

Avasam commented Apr 4, 2024

Unfortunately, when it comes to using a str as a key, there is simply no way to know statically whether you are targeting an individual cell, row, column or full range. So the type would be Cell | tuple[Cell, ...] | tuple[tuple[Cell, ...], ...], which is too distruptive so we fallback to Any (see python/mypy#1693)

The case for slicing can be improved, but unfortunately only to remove false-positives, in favor of false-negatives. (which we prefer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants