pythonpython-typingpyright

Pylance shows error when variable type changes


How to avoid this VSCode + Pylance static type check error?

def process_row(row: dict[str, Any]):
    row = SimpleNamespace(**row)

For this, vscode shows red underline for SimpleNamespace(**row) with messsage: Expression of type "SimpleNamespace" cannot be assigned to declared type "dict[str, Any]"


Solution

  • After more searching and going through github issues for pylance and mypy, it appears that this is legitimate open issue for pylance.

    mypy kind of fixed this. For above function, running mypy typetest.py gave 1 error: Name "row" already defined on line 5 but rnunning with mypy --allow-redefinition typetest.py was all green.

    You can not yet turn of this in pylance it seems but let people know here if anyone point this out. Thanks.