Skip to content

[mypyc] Recognize 'six.moves.xrange' as an alias of 'range' #9896

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

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing_extensions import Type, ClassVar

from mypy.nodes import (
Lvalue, Expression, TupleExpr, CallExpr, RefExpr, GeneratorExpr, ARG_POS, MemberExpr
Lvalue, Expression, TupleExpr, CallExpr, RefExpr, GeneratorExpr, ARG_POS, MemberExpr, TypeAlias
)
from mypyc.ir.ops import (
Value, BasicBlock, Integer, Branch, Register, TupleGet, TupleSet, IntOp
Expand Down Expand Up @@ -156,6 +156,11 @@ def loop_contents(
handle_loop(loop_params)


def is_range_ref(expr: RefExpr) -> bool:
return (expr.fullname == 'builtins.range'
or isinstance(expr.node, TypeAlias) and expr.fullname == 'six.moves.xrange')


def make_for_loop_generator(builder: IRBuilder,
index: Lvalue,
expr: Expression,
Expand Down Expand Up @@ -189,7 +194,7 @@ def make_for_loop_generator(builder: IRBuilder,

if (isinstance(expr, CallExpr)
and isinstance(expr.callee, RefExpr)):
if (expr.callee.fullname == 'builtins.range'
if (is_range_ref(expr.callee)
and (len(expr.args) <= 2
or (len(expr.args) == 3
and builder.extract_int(expr.args[2]) is not None))
Expand Down