You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#9764 actually made load_workbook() return a Workbook. Previously, it returned Any. The problem is that wb.active can return other workbook children (and even None), not only Worksheets. The easiest way to work around this problem is to use:
wb=openpyxl.load_workbook(path)
ws=wb.activeassertisinstance(ws, Worksheet)
header= [cell.valueforcellinws[1]] # line 120
Thanks for the quick answer. I'll apply the workaround to fix the code.
Maybe it could a good idea to write in the changelog that the type stubs are now more accurate and this may require changes to existing code bases that implicitly relied on wb.active to be Any. A link to the fix in this thread could also be useful.
@tritemio Unfortunately, our changelogs are currently auto-generated from git commit messages, which is why they are a bit messy and we can't edit them retroactively.
#9764 actually made load_workbook() return a Workbook. Previously, it returned Any. The problem is that wb.active can return other workbook children (and even None), not only Worksheets. The easiest way to work around this problem is to use:
wb=openpyxl.load_workbook(path)
ws=wb.activeassertisinstance(ws, Worksheet)
header= [cell.valueforcellinws[1]] # line 120
Hello, apparently using the assert doesn't prevent mypy from reporting the error.. type hinting doesn't do much either, do you have any other solutions?
I think the problems with wb.active have been explained. There isn't a better solution at the moment, until something like python/typing#566 becomes available.
Activity
srittau commentedon Mar 6, 2023
#9764 actually made
load_workbook()return aWorkbook. Previously, it returnedAny. The problem is thatwb.activecan return other workbook children (and even None), not onlyWorksheets. The easiest way to work around this problem is to use:Cc @mynhardtburger
Avasam commentedon Mar 7, 2023
+1 what @srittau said.
Even in #9511 I made
load_workbookreturn aWorkbook, the type comes from: https://foss.heptapod.net/openpyxl/openpyxl/-/blob/branch/3.1/openpyxl/reader/workbook.py#L31tritemio commentedon Mar 8, 2023
Thanks for the quick answer. I'll apply the workaround to fix the code.
Maybe it could a good idea to write in the changelog that the type stubs are now more accurate and this may require changes to existing code bases that implicitly relied on wb.active to be
Any. A link to the fix in this thread could also be useful.srittau commentedon Mar 8, 2023
@tritemio Unfortunately, our changelogs are currently auto-generated from git commit messages, which is why they are a bit messy and we can't edit them retroactively.
AlbertoPuritano commentedon Mar 9, 2023
Hello, apparently using the assert doesn't prevent mypy from reporting the error.. type hinting doesn't do much either, do you have any other solutions?
srittau commentedon Jun 12, 2023
I think the problems with
wb.activehave been explained. There isn't a better solution at the moment, until something like python/typing#566 becomes available.