Skip to content
12 changes: 7 additions & 5 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,13 +1197,15 @@ def get_expr_length(expr: Expression) -> int | None:
and isinstance(expr.node, Var)
and expr.node.is_final
and isinstance(expr.node.final_value, str)
and expr.node.has_explicit_value
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized when looking that this line is unnecessary if the above checks pass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#19930 renders this irrelevant

):
return len(expr.node.final_value)
# TODO: extend this, passing length of listcomp and genexp should have worthwhile
# performance boost and can be (sometimes) figured out pretty easily. set and dict
# comps *can* be done as well but will need special logic to consider the possibility
# of key conflicts. Range, enumerate, zip are all simple logic.
elif isinstance(expr, ListComprehension):
return get_expr_length(expr.generator)
elif isinstance(expr, GeneratorExpr) and len(expr.sequences) == 1 and not expr.condlists:
return get_expr_length(expr.sequences[0])
# TODO: extend this, set and dict comps can be done as well but will
# need special logic to consider the possibility of key conflicts.
# Range, enumerate, zip are all simple logic.
return None


Expand Down
Loading