Skip to content

Running modernize converts range to xrange which isn't 1:1 #249

Open
@tanant

Description

@tanant

I'm doing a conversion here locally and I've just been debugging an error that was introduced after running modernize over some code. I've caught it and can fix this a couple ways however, should modernize be converting range like this?

Here's the stripped back example with static indexes and suchlike:

for x in range(10)[5:]:   # skip first values
    print x

after modernize

from __future__ import print_function
from six.moves import range
for x in range(10)[5:]:   # skip first values
    print(x)

The problem here is that range from six.moves is xrange which doesn't support slice notation. Running the new code (at least in my 2.7.3 environment) results in the following traceback:

Traceback (most recent call last):
  File "file.py", line 3, in <module>
    for x in range(10)[5:]:   # skip first values
TypeError: sequence index must be integer, not 'slice'

I can locally trap this with tests, but this seems like a case where the range -> six.moves.range call almost would need to be wrapped with a list for py2 purposes? list(range (10))[5:] would've been fine.

If this behaviour isn't expected, I can try draft up a patch - it's not a major challenge in the code I'm looking at (it's a 'nice' hard fail if there's a test suite covering the code path) but this feels slightly strange to convert range from a list to an xrange generator

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions