Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tanant opened this issue May 20, 2021 · 0 comments
Open

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

tanant opened this issue May 20, 2021 · 0 comments

Comments

@tanant
Copy link

tanant commented May 20, 2021

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

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

No branches or pull requests

1 participant