mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Fix checking multiple assignments based on tuple unpacking involving partially initialised variables (Fixes #12915).

Open tyralla opened this issue 3 years ago • 4 comments

Fix checking multiple assignments based on tuple unpacking involving partially initialised variables (Fixes #12915).

This commit introduces two changes:

  • It converts unions of tuples to tuples of unions during multi-assignment checking when all (original) tuples have the same length. This fixes #12915.
  • It removes the undefined_rvalue logic completely. This logic bothered me because it introduced the necessity to make the mentioned conversion twice (which was not always successful due to reasons I do not fully understand).

Test new case testDefinePartiallyInitialisedVariableDuringTupleUnpacking covers issue #12915.

I had to adjust some other test cases. In my opinion, the error messages are now more transparent, consistent, and complete. Removing the undefined_rvalue logic also seems to fix one more bug. In the test cases testInferenceWithTypeVariableTwiceInReturnType and testInferenceWithTypeVariableTwiceInReturnTypeAndMultipleVariables, there were three assignments for which Mypy did not report errors but, in my opinion, should.

So, for the current test data set, removing the undefined_rvalue logic seems to introduce mere benefits than drawbacks (meaning, different error messages than before). I hope it does not break any Mypy functionalities not covered by the tests.

tyralla avatar Jan 10 '23 13:01 tyralla

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 10 '23 14:01 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 10 '23 16:01 github-actions[bot]

After rethinking my approach, I decided to extend it to cover unions containing other iterable items. The result is #14440, which involves even more changes than this pull request. I consider #14440 better but leave this one open if I am wrong.

tyralla avatar Jan 12 '23 15:01 tyralla

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 16 '23 05:01 github-actions[bot]