MartinThoma / openpyxl-stubs

Type stubs for openpyxl
MIT License
9 stars 9 forks source link

ReadOnlyCell not defined ? #14

Open johann50 opened 3 years ago

johann50 commented 3 years ago

I can't find where ReadOnlyCell is defined. If I call the cell function of a worksheet I am getting that the function returns type "MergedCell** | Unknown | Cell" because ReadOnlyCell is not defined.

Avasam commented 6 months ago

If you just want the type directly:

https://github.com/python/typeshed/blob/main/stubs/openpyxl/openpyxl/cell/read_only.pyi

from openpyxl.cell.read_only import ReadOnlyCell

also re-exported in: https://github.com/python/typeshed/blob/main/stubs/openpyxl/openpyxl/cell/__init__.pyi

from openpyxl.cell import ReadOnlyCell

Although even typeshed (https://pypi.org/project/types-openpyxl) doesn't yet support specific cell types per worksheet type: https://github.com/python/typeshed/issues/9940#issuecomment-2037843062 & https://github.com/python/typeshed/issues/10952

If you just want to avoid the partial Unknown in pyright, then the typeshed stub will work for you.

If you need the return type to be ReadOnlyCell, for now the best thing is to cast:

from typing import cast
from openpyxl.cell.read_only import ReadOnlyCell

ws: ReadOnlyWorksheet = ...

cell = cast(ReadOnlyCell, ws.cell(1, 1))