MartinThoma / openpyxl-stubs

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

wb.active property type #29

Open mutricyl opened 6 months ago

mutricyl commented 6 months ago

Hello

looking at workbook.workbook.pyi it looks like active property return None or _WorkbookChild however looking at openpyxl code :

    @property
    def active(self):
        """Get the currently active sheet or None

        :type: :class:`openpyxl.worksheet.worksheet.Worksheet`
        """
        try:
            return self._sheets[self._active_sheet_index]
        except IndexError:
            pass

return value is either of type Workbook or None

>>> from openpyxl import Workbook      
>>> wb = Workbook()
>>> type(wb.active)
<class 'openpyxl.worksheet.worksheet.Worksheet'>

Am I reading the code correctly ?

When I write:

from openpyxl import Workbook      
wb = Workbook()
ws = wb.active
cell = ws["A1"]

mypy complains : error: Value of type "_WorkbookChild" is not indexable [index]

Avasam commented 4 months ago

It's actually Worksheet | WriteOnlyWorksheet | ReadOnlyWorksheet | Chartsheet since it depends on the type of worksheets your workbook has (and whether it's readonly/writeonly).

Even worst: ReadOnlyWorksheet doesn't even subclass _WorkbookChild !

This was recently solve for https://pypi.org/project/types-openpyxl/ in https://github.com/python/typeshed/pull/11718