← Back to all posts

October 21, 2020

Simplify negative list access

New Refactorings

Simplify negative list access

This refactoring uses the fact that Python allows negative list indices to be accessed directly.

It converts this:

last_element = a[len(a) - 1]

into this:

last_element = a[-1]

Merge Comparison refactoring extended to the negative case

The merge-comparison proposal will now apply in this case:

if x != a and x != b:

This will be converted to:

if x not in [a, b]:

Bug fixes

  • Ensure statements that write global state can't be hoisted out of loops (e.g. function calls)
  • Do not try to remove pass statements at class level
  • Do not wrap the targets of annotated assignments in brackets