Pylance reports reportUnboundVariable when using walrus operator with a while loop that rebinds an input (intermittent) #864
Labels
bug
Something isn't working
fixed in next version (main)
A fix has been implemented and will appear in an upcoming version
Environment data
Expected behaviour
No variable in this code (a cut down toy example, which functions correctly) should be marked as an unbound variable:
(Yes this isn't a great way of removing instances of 's' from a string. My original code that I found this bug on was structurally the same and performed a more complex task. I simplified it for easier testing.)
Actual behaviour
Both instances of i in the line
text = text[:i] + text[i+1:]
are marked as unbound by pylance. 'i' is obviously bound on the previous line.Sometimes editing the code and changing it back makes the unbound variable reports go away temporarily. If that happens, adding and removing a space makes it come back.
Logs
Nothing that looks useful.
Code Snippet / Additional information
This problem seems to be very specific as the following (incorrect) snippets are not marked with any variables unbound (or any warnings or errors at all).
Not changing the value of 'text':
setting 'i' to a variable instead of a field of the variable:
Adding a break (or return) statement also prevents the warning. However, only if it is unconditional.
The warning also does not occur if I change 'while' to 'if'.
The text was updated successfully, but these errors were encountered: