IronLanguages / IronLanguages/ironpython3

Comparison AST does not round trip properly

Open
#1,281 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module-ast
Dominant language
C#
Stars
2.8k
Forks
316
Avg merge
1d 9h
Merged PRs (30d)
1

Description

Discovered this when working on ipy3 stuff:

x = ast.parse("assert False is (2 is 3)")
exec(compile(x, "", "exec"))

The reason it fails is that it's executed as if it was "assert False is 2 is 3".

Note that the generated AST is correct, it's breaking in the conversion between the Python AST and our internal AST. Python's representation is a Compare expression with a list of ops/comparators while we just chain BinaryExpression and assume that if the Right expression is also a BinaryExpression then we have chained comparisons. When we convert from Python AST we just blindly put the right expression in BinaryExpression, in this case it's a BinaryExpression so it thinks we're chaining comparions.

The easy fix would be to wrap the right expressions in a ParenthesisExpression when reverting the Python AST. A better fix would probably be to add a ComparisonExpression type and use that for comparisons instead of BinaryExpression.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the conversion between Python AST and the internal AST, focusing on Compare, BinaryExpression, and ParenthesisExpression handling. Reproduce the provided assert example and trace how its nested comparison is represented. Done means the expression round trips without being flattened into a chained comparison and executes with the original semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.