Repository: sympy/sympy
File: sympy/parsing/tests/test_sympy_parser.py (Python)

Code before:
    x = Symbol('x')
    y = Symbol('y')
    assert parse_expr("1*2=x", transformations=transformations) == Eq(2, x)
    assert parse_expr("y = x", transformations=transformations) == Eq(y, x)
    assert parse_expr("(2*y = x) = False",
        transformations=transformations) == Eq(Eq(2*y, x), False)


def test_parse_function_issue_3539():
    x = Symbol('x')
    f = Function('f')
    assert parse_expr('f(x)') == f(x)

def test_issue_24288():
    assert parse_expr("1 < 2", evaluate=False) == Lt(1, 2, evaluate=False)
    assert parse_expr("1 <= 2", evaluate=False) == Le(1, 2, evaluate=False)
    assert parse_expr("1 > 2", evaluate=False) == Gt(1, 2, evaluate=False)
    assert parse_expr("1 >= 2", evaluate=False) == Ge(1, 2, evaluate=False)
    assert parse_expr("1 != 2", evaluate=False) == Ne(1, 2, evaluate=False)
    assert parse_expr("1 == 2", evaluate=False) == Eq(1, 2, evaluate=False)
    assert parse_expr("1 < 2 < 3", evaluate=False) == And(Lt(1, 2, evaluate=False), Lt(2, 3, evaluate=False), evaluate=False)
    assert parse_expr("1 <= 2 <= 3", evaluate=False) == And(Le(1, 2, evaluate=False), Le(2, 3, evaluate=False), evaluate=False)
    assert parse_expr("1 < 2 <= 3 < 4", evaluate=False) == \
        And(Lt(1, 2, evaluate=False), Le(2, 3, evaluate=False), Lt(3, 4, evaluate=False), evaluate=False)
    # parse_expr with evaluate=True also falls back to Python comparison for them
    assert parse_expr(r"1 in {1}", evaluate=False)
    assert parse_expr(r"1 not in {2}", evaluate=False)
    assert parse_expr(r"1 is 1", evaluate=False)
    assert parse_expr(r"1 is not 2", evaluate=False)

def test_split_symbols_numeric():
    transformations = (
        standard_transformations +
        (implicit_multiplication_application,))

    n = Symbol('n')
    expr1 = parse_expr('2**n * 3**n')
    expr2 = parse_expr('2**n3**n', transformations=transformations)
    assert expr1 == expr2 == 2**n*3**n

    expr1 = parse_expr('n12n34', transformations=transformations)
    assert expr1 == n*12*n*34


def test_unicode_names():
    assert parse_expr('α') == Symbol('α')


def test_python3_features():
    # Make sure the tokenizer can handle Python 3-only features
    if sys.version_info < (3, 8):

Review comment (suggestion):
I'm not sure if this should be allowed. It uses the Python set containment checking:
```python
In [1]: parse_expr('x in {y}')
Out[1]: False

In [2]: x in S({y})
...
TypeError: did not evaluate to a bool: Eq(y, x)

In [3]: Contains(x, {y})
Out[3]: x ∈ {y}
```
In principle `parse_expr` could use `Contains` but I don't know whether this should be treated as semantic containment checking or just that `parse_expr` represents what the Python code would do.

Code after:
    x = Symbol('x')
    y = Symbol('y')
    assert parse_expr("1*2=x", transformations=transformations) == Eq(2, x)
    assert parse_expr("y = x", transformations=transformations) == Eq(y, x)
    assert parse_expr("(2*y = x) = False",
        transformations=transformations) == Eq(Eq(2*y, x), False)


def test_parse_function_issue_3539():
    x = Symbol('x')
    f = Function('f')
    assert parse_expr('f(x)') == f(x)

def test_issue_24288():
    assert parse_expr("1 < 2", evaluate=False) == Lt(1, 2, evaluate=False)
    assert parse_expr("1 <= 2", evaluate=False) == Le(1, 2, evaluate=False)
    assert parse_expr("1 > 2", evaluate=False) == Gt(1, 2, evaluate=False)
    assert parse_expr("1 >= 2", evaluate=False) == Ge(1, 2, evaluate=False)
    assert parse_expr("1 != 2", evaluate=False) == Ne(1, 2, evaluate=False)
    assert parse_expr("1 == 2", evaluate=False) == Eq(1, 2, evaluate=False)
    assert parse_expr("1 < 2 < 3", evaluate=False) == And(Lt(1, 2, evaluate=False), Lt(2, 3, evaluate=False), evaluate=False)
    assert parse_expr("1 <= 2 <= 3", evaluate=False) == And(Le(1, 2, evaluate=False), Le(2, 3, evaluate=False), evaluate=False)
    assert parse_expr("1 < 2 <= 3 < 4", evaluate=False) == \
        And(Lt(1, 2, evaluate=False), Le(2, 3, evaluate=False), Lt(3, 4, evaluate=False), evaluate=False)
    # Valid Python relational operators that SymPy does not decide how to handle them yet
    raises(ValueError, lambda: parse_expr("1 in 2", evaluate=False))
    raises(ValueError, lambda: parse_expr("1 is 2", evaluate=False))
    raises(ValueError, lambda: parse_expr("1 not in 2", evaluate=False))
    raises(ValueError, lambda: parse_expr("1 is not 2", evaluate=False))

def test_split_symbols_numeric():
    transformations = (
        standard_transformations +
        (implicit_multiplication_application,))

    n = Symbol('n')
    expr1 = parse_expr('2**n * 3**n')
    expr2 = parse_expr('2**n3**n', transformations=transformations)
    assert expr1 == expr2 == 2**n*3**n

    expr1 = parse_expr('n12n34', transformations=transformations)
    assert expr1 == n*12*n*34


def test_unicode_names():
    assert parse_expr('α') == Symbol('α')


def test_python3_features():
    # Make sure the tokenizer can handle Python 3-only features
    if sys.version_info < (3, 8):