Skip to content

Treat assignments in unreachable code as local bindings - #11666

Open
Henry Su (hsusul) wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix/unreachable-local-binding
Open

Treat assignments in unreachable code as local bindings#11666
Henry Su (hsusul) wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix/unreachable-local-binding

Conversation

@hsusul

Copy link
Copy Markdown
Contributor

Summary

  • Fixes Local variables in unused code are not detected #11449.
  • CPython makes a name local for the whole function if it is assigned anywhere in the function body, including after return.
  • The binder skipped unreachable statements, so those names were treated as globals and UnboundLocalError was missed. Nested nonlocal lookups into such names also failed.

Test plan

  • npx jest typeEvaluator2.test.ts -t "Unbound" --forceExit (from packages/pyright-internal)
  • npx jest typeEvaluator1.test.ts -t "Unreachable1" --forceExit
  • Confirm a read before an assignment that appears after return reports an unbound variable in the language server

CPython treats any assignment in a function as making the name local, even
after a return. Skipping those statements left later-assigned names looking
global and hid UnboundLocalError.

Fixes microsoft#11449
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local variables in unused code are not detected

1 participant