Pylance reports incorrect error for openpyxl

Question:

I’m using VSCode for python and am getting a typing error with Pylance that appears incorrect and is not reported as an error by mypy --strict.

from openpyxl import load_workbook

wb = load_workbook("c:/temp/file.xlsx")
ws = wb["Sheet1"]
x = ws["A1"]

Pylance reports "__getitem__" method not defined on type "Chartsheet" for ws["A1"]. mypy reports Success: no issues found in 1 source file

How can I get Pylance to behave better, other than #type: ignore? Or should I just use mypy instead?

Asked By: foosion

||

Answers:

It appears the problem is that I installed openpyxl-stubs, which mypy wants but which causes Pylance to generate the error. You can right click on an import and Go To Declaration which shows the file where the function is declared. openpyxl-stubs has a return type of Workbook while uninstalling it shows a declaration without a return type, which is interpreted as Any, so no error (but no real typechecking). With the stubs file mypy is happy, without it Pylance is happy

Answered By: foosion