Skip to content

synt.expr.expr ¤

ExprPrecedence ¤

Bases: IntEnum

Python's expression precedence.

Sort sequence: smaller = prior

References

Operator precedence

Source code in synt/expr/expr.py
class ExprPrecedence(IntEnum):
    r"""Python's expression precedence.

    Sort sequence: smaller = prior

    References:
        [Operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence)
    """

    Atom = 0
    """Atom expression: Binding or parenthesized expression, list display, dictionary display, set display,
    and other bound operations.

    Examples:
        ```python
        (expressions...)  # tuple / bind
        [expressions...]  # list
        {key: value...}   # dict
        {expressions...}  # set
        **kwargs          # bound operations (unpacking)
        x for x in y      # bound expressions (generator comprehension)
        yield from tasks  # bound expressions (yield / yield from)
        ```
    """

    Call = 1
    """call.Call-like: subscription, slicing, call, attribute reference.

    Examples:
        ```python
        x[index]       # subscribe
        x[index:index] # slice
        x(args, ...)   # call
        x.attr         # attribute
        ```
    """

    Await = 2
    """Await expression.

    Examples:
        ```python
        await x
        ```
    """

    Exponential = 3
    """Exponential operation: exponentiation.

    **Exception:**
    The power operator `**` binds less tightly than an arithmetic or bitwise unary operator on its right,
    that is, `2 ** -1` is `0.5`.

    Examples:
        ```python
        x ** y
        ```
    """

    Unary = 4
    """Unary operator: positive, negative, bitwise NOT.

    Examples:
        ```python
        +x  # positive
        -x  # negative
        ~x  # bitwise NOT
        ```
    """

    Multiplicative = 5
    """Multiplicative operation: multiplication, matrix multiplication, division, floor division, remainder.

    Notes:
        The `%` operator is also used for string formatting; the same precedence applies.

    Examples:
        ```python
        x * y   # multiplication
        x @ y   # matrix multiplication
        x / y   # division
        x // y  # floor division
        x % y   # remainder
        ```
    """

    Additive = 6
    """Additive operation: addition and subtraction.

    Examples:
        ```python
        x + y  # addition
        x - y  # subtraction
        ```
    """

    Shift = 7
    """Shift operation.

    Examples:
        ```python
        x << y  # left shift
        x >> y  # right shift
        ```
    """

    BitAnd = 8
    """Bitwise AND.

    Examples:
        ```python
        x & y  # bitwise AND
        ```
    """

    BitXor = 9
    """Bitwise XOR.

    Examples:
        ```python
        x ^ y  # bitwise XOR
        ```
    """

    BitOr = 10
    """Bitwise OR.

    Examples:
        ```python
        x | y  # bitwise OR
        ```
    """

    Comparative = 11
    """Boolean operations: comparisons, including membership tests and identity tests.

    Examples:
        ```python
        x in y          # membership tests
        x not in y
        x is y          # identity tests
        x is not y
        x < y           # comparisons
        x <= y
        x > y
        x >= y
        x != y
        x == y
        ```
    """

    BoolNot = 12
    """Boolean NOT.

    Examples:
        ```python
        not x  # boolean NOT
        ```
    """

    BoolAnd = 13
    """Boolean AND.

    Examples:
        ```python
        x and y  # boolean AND
        ```
    """

    BoolOr = 14
    """Boolean OR.

    Examples:
        ```python
        x or y  # boolean OR
        ```
    """

    Conditional = 15
    """Conditional expression: `if` - `else` expression.

    Examples:
        ```python
        x if condition else y
        ```
    """

    Lambda = 16
    """Lambda expression.

    Examples:
        ```python
        lambda x, y: x + y
        ```
    """

    NamedExpr = 17
    """Inline assignment expression.

    Examples:
        ```python
        x := y
        ```
    """

Atom class-attribute instance-attribute ¤

Atom = 0

Atom expression: Binding or parenthesized expression, list display, dictionary display, set display, and other bound operations.

Examples:

1
2
3
4
5
6
7
(expressions...)  # tuple / bind
[expressions...]  # list
{key: value...}   # dict
{expressions...}  # set
**kwargs          # bound operations (unpacking)
x for x in y      # bound expressions (generator comprehension)
yield from tasks  # bound expressions (yield / yield from)

Call class-attribute instance-attribute ¤

Call = 1

call.Call-like: subscription, slicing, call, attribute reference.

Examples:

1
2
3
4
x[index]       # subscribe
x[index:index] # slice
x(args, ...)   # call
x.attr         # attribute

Await class-attribute instance-attribute ¤

Await = 2

Await expression.

Examples:

await x

Exponential class-attribute instance-attribute ¤

Exponential = 3

Exponential operation: exponentiation.

Exception: The power operator ** binds less tightly than an arithmetic or bitwise unary operator on its right, that is, 2 ** -1 is 0.5.

Examples:

x ** y

Unary class-attribute instance-attribute ¤

Unary = 4

Unary operator: positive, negative, bitwise NOT.

Examples:

1
2
3
+x  # positive
-x  # negative
~x  # bitwise NOT

Multiplicative class-attribute instance-attribute ¤

Multiplicative = 5

Multiplicative operation: multiplication, matrix multiplication, division, floor division, remainder.

Notes

The % operator is also used for string formatting; the same precedence applies.

Examples:

1
2
3
4
5
x * y   # multiplication
x @ y   # matrix multiplication
x / y   # division
x // y  # floor division
x % y   # remainder

Additive class-attribute instance-attribute ¤

Additive = 6

Additive operation: addition and subtraction.

Examples:

x + y  # addition
x - y  # subtraction

Shift class-attribute instance-attribute ¤

Shift = 7

Shift operation.

Examples:

x << y  # left shift
x >> y  # right shift

BitAnd class-attribute instance-attribute ¤

BitAnd = 8

Bitwise AND.

Examples:

x & y  # bitwise AND

BitXor class-attribute instance-attribute ¤

BitXor = 9

Bitwise XOR.

Examples:

x ^ y  # bitwise XOR

BitOr class-attribute instance-attribute ¤

BitOr = 10

Bitwise OR.

Examples:

x | y  # bitwise OR

Comparative class-attribute instance-attribute ¤

Comparative = 11

Boolean operations: comparisons, including membership tests and identity tests.

Examples:

x in y          # membership tests
x not in y
x is y          # identity tests
x is not y
x < y           # comparisons
x <= y
x > y
x >= y
x != y
x == y

BoolNot class-attribute instance-attribute ¤

BoolNot = 12

Boolean NOT.

Examples:

not x  # boolean NOT

BoolAnd class-attribute instance-attribute ¤

BoolAnd = 13

Boolean AND.

Examples:

x and y  # boolean AND

BoolOr class-attribute instance-attribute ¤

BoolOr = 14

Boolean OR.

Examples:

x or y  # boolean OR

Conditional class-attribute instance-attribute ¤

Conditional = 15

Conditional expression: if - else expression.

Examples:

x if condition else y

Lambda class-attribute instance-attribute ¤

Lambda = 16

Lambda expression.

Examples:

lambda x, y: x + y

NamedExpr class-attribute instance-attribute ¤

NamedExpr = 17

Inline assignment expression.

Examples:

x := y

ExprType ¤

Bases: IntEnum

Expression type in Synt.

Source code in synt/expr/expr.py
class ExprType(IntEnum):
    r"""Expression type in Synt."""

    Atom = -1
    """Any expression type.

    This type is Reserved for internal use."""
    Unknown = -2
    """Unknown expression type.

    This type is Reserved for internal use."""

    Identifier = 0
    """[`IdentifierExpr`][synt.tokens.ident.IdentifierExpr]"""
    Wrapped = 1
    """[`Wrapped`][synt.expr.wrapped.Wrapped]."""
    KeyValuePair = 1
    """[`KVPair`][synt.tokens.kv_pair.KVPair]."""
    UnaryOp = 2
    """[`unary_op.UnaryOp`][synt.expr.unary_op.UnaryOp]."""
    BinaryOp = 3
    """[`binary_op.BinaryOp`][synt.expr.binary_op.BinaryOp]."""
    List = 4
    """[`ListDisplay`][synt.expr.list.ListDisplay]."""
    Dict = 5
    """[`DictDisplay`][synt.expr.dict.DictDisplay]."""
    Set = 6
    """[`SetDisplay`][synt.expr.set.SetDisplay]."""
    Tuple = 7
    """[`Tuple`][synt.expr.tuple.Tuple]."""
    Closure = 8
    """[`Closure`][synt.expr.closure.Closure]."""
    Condition = 9
    """[`Condition`][synt.expr.condition.Condition]."""
    NamedExpr = 10
    """[`NamedExpr`][synt.expr.named_expr.NamedExpr]."""
    Comprehension = 11
    """[`Comprehension`][synt.expr.comprehension.Comprehension]."""
    FormatString = 12
    """[`FormatString`][synt.expr.fstring.FormatString]"""
    Subscript = 13
    """[`subscript.Subscript`][synt.expr.subscript.Subscript]"""
    Attribute = 14
    """[`attribute.Attribute`][synt.expr.attribute.Attribute]"""
    Call = 15
    """[`call.Call`][synt.expr.call.Call]"""
    Literal = 16
    """[`tokens.lit.Literal`][synt.tokens.lit.Literal]"""
    Empty = 17
    """[`Empty`][synt.expr.empty.Empty]"""

Atom class-attribute instance-attribute ¤

Atom = -1

Any expression type.

This type is Reserved for internal use.

Unknown class-attribute instance-attribute ¤

Unknown = -2

Unknown expression type.

This type is Reserved for internal use.

Identifier class-attribute instance-attribute ¤

Identifier = 0

Wrapped class-attribute instance-attribute ¤

Wrapped = 1

KeyValuePair class-attribute instance-attribute ¤

KeyValuePair = 1

UnaryOp class-attribute instance-attribute ¤

UnaryOp = 2

unary_op.UnaryOp.

BinaryOp class-attribute instance-attribute ¤

BinaryOp = 3

binary_op.BinaryOp.

List class-attribute instance-attribute ¤

List = 4

Dict class-attribute instance-attribute ¤

Dict = 5

Set class-attribute instance-attribute ¤

Set = 6

Tuple class-attribute instance-attribute ¤

Tuple = 7

Closure class-attribute instance-attribute ¤

Closure = 8

Condition class-attribute instance-attribute ¤

Condition = 9

NamedExpr class-attribute instance-attribute ¤

NamedExpr = 10

Comprehension class-attribute instance-attribute ¤

Comprehension = 11

FormatString class-attribute instance-attribute ¤

FormatString = 12

Subscript class-attribute instance-attribute ¤

Subscript = 13

subscript.Subscript

Attribute class-attribute instance-attribute ¤

Attribute = 14

attribute.Attribute

Call class-attribute instance-attribute ¤

Call = 15

call.Call

Literal class-attribute instance-attribute ¤

Literal = 16

tokens.lit.Literal

Empty class-attribute instance-attribute ¤

Empty = 17

IntoExpression ¤

Bases: IntoCode

Abstract class for those that can be converted into an Expression.

Source code in synt/expr/expr.py
class IntoExpression(code.IntoCode, metaclass=ABCMeta):
    r"""Abstract class for those that can be converted into an
    [`Expression`][synt.expr.expr.Expression]."""

    @abstractmethod
    def into_expression(self) -> Expression:
        """Convert the object into an expression."""

    def expr(self) -> Expression:
        """Convert the object into an expression.

        This is a convenience method that calls `into_expression()` and returns the result.
        """
        return self.into_expression()

    def into_code(self) -> str:
        """Convert the object into a code string.

        This is a convenience method that calls `into_expression()` and calls `into_code` on the result.
        """
        return self.into_expression().into_code()

into_expression abstractmethod ¤

into_expression() -> Expression

Convert the object into an expression.

Source code in synt/expr/expr.py
@abstractmethod
def into_expression(self) -> Expression:
    """Convert the object into an expression."""

expr ¤

expr() -> Expression

Convert the object into an expression.

This is a convenience method that calls into_expression() and returns the result.

Source code in synt/expr/expr.py
def expr(self) -> Expression:
    """Convert the object into an expression.

    This is a convenience method that calls `into_expression()` and returns the result.
    """
    return self.into_expression()

into_code ¤

into_code() -> str

Convert the object into a code string.

This is a convenience method that calls into_expression() and calls into_code on the result.

Source code in synt/expr/expr.py
def into_code(self) -> str:
    """Convert the object into a code string.

    This is a convenience method that calls `into_expression()` and calls `into_code` on the result.
    """
    return self.into_expression().into_code()

Expression ¤

Bases: IntoExpression, IntoCode

Base class for any expression in Python.

Source code in synt/expr/expr.py
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
class Expression(IntoExpression, code.IntoCode, metaclass=ABCMeta):
    r"""Base class for any expression in Python."""

    @property
    @abstractmethod
    def precedence(self) -> ExprPrecedence:
        """The expression's precedence."""

    @property
    @abstractmethod
    def expr_type(self) -> ExprType:
        """The expression's type."""

    @abstractmethod
    def into_code(self) -> str:
        """Convert the expression into Python code.

        **No Formatting!**
        """

    def into_expression(self) -> Expression:
        """An `Expression` can always be converted into an `Expression`."""
        return self

    def ensure_identifier(self) -> Identifier:
        """Ensure that the expression is an identifier and returns it.

        Raises:
            ValueError: If the expression is not an identifier.
        """
        if type_check.is_ident(self):
            return self.ident
        else:
            raise ValueError("Expression is not an identifier")

    # alias for binary operation

    # bin op > plain method

    def add(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Add operation.

        Examples:
            ```python
            e = litint(1).add(id_("foo")) # alias ... + ...
            assert e.into_code() == "1 + foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Add, self, other)

    def sub(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Subtract operation.

        Examples:
            ```python
            e = litint(1).sub(id_("foo")) # alias ... - ...
            assert e.into_code() == "1 - foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Sub, self, other)

    def mul(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Multiply operation.

        Examples:
            ```python
            e = litint(1).mul(id_("foo")) # alias ... * ...
            assert e.into_code() == "1 * foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Mul, self, other)

    def div(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Divide operation.

        Examples:
            ```python
            e = litint(1).div(id_("foo")) # alias ... / ...
            assert e.into_code() == "1 / foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Div, self, other)

    def truediv(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`div`][synt.expr.expr.Expression.div]."""
        return self.div(other)

    def floor_div(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Floor divide operation.

        Examples:
            ```python
            e = litint(1).floor_div(id_("foo")) # alias ... // ...
            assert e.into_code() == "1 // foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.FloorDiv, self, other)

    def mod(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Modulus operation.

        Examples:
            ```python
            e = litint(1).mod(id_("foo")) # alias ... % ...
            assert e.into_code() == "1 % foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Mod, self, other)

    def pow(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Exponentiation operation.

        Examples:
            ```python
            e = litint(1).pow(id_("foo")) # alias ... ** ...
            assert e.into_code() == "1 ** foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Pow, self, other)

    def at(self, other: IntoExpression) -> binary_op.BinaryOp:
        """At(matrix multiplication) operation.

        Examples:
            ```python
            e = litint(1).at(id_("foo")) # alias ... @ ...
            assert e.into_code() == "1 @ foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.At, self, other)

    def matmul(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`at`][synt.expr.expr.Expression.matmul]."""
        return self.at(other)

    def lshift(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Left shift operation.

        Examples:
            ```python
            e = litint(1).lshift(id_("foo")) # alias ... << ...
            assert e.into_code() == "1 << foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.LShift, self, other)

    def rshift(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Right shift operation.

        Examples:
            ```python
            e = litint(1).rshift(id_("foo")) # alias ... >> ...
            assert e.into_code() == "1 >> foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.RShift, self, other)

    def lt(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Less than operation.

        Examples:
            ```python
            e = litint(1).lt(id_("foo")) # alias ... < ...
            assert e.into_code() == "1 < foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Less, self, other)

    def le(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Less than or equal to operation.

        Examples:
            ```python
            e = litint(1).le(id_("foo")) # alias ... <= ...
            assert e.into_code() == "1 <= foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.LessEqual, self, other)

    def gt(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Greater than operation.

        Examples:
            ```python
            e = litint(1).gt(id_("foo")) # alias ... > ...
            assert e.into_code() == "1 > foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Greater, self, other)

    def ge(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Greater than or equal to operation.

        Examples:
            ```python
            e = litint(1).ge(id_("foo")) # alias ... >= ...
            assert e.into_code() == "1 >= foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.GreaterEqual, self, other)

    def eq(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Equal to operation.

        Examples:
            ```python
            e = litint(1).eq(id_("foo")) # alias ... == ...
            assert e.into_code() == "1 == foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Equal, self, other)

    def ne(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Not equal to operation.

        Examples:
            ```python
            e = litint(1).ne(id_("foo")) # alias ... != ...
            assert e.into_code() == "1 != foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.NotEqual, self, other)

    def in_(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Membership test operation.

        Examples:
            ```python
            e = litint(1).in_(id_("foo")) # builtin check, no alias
            assert e.into_code() == "1 in foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.In, self, other)

    def not_in(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Negative membership test operation.

        Examples:
            ```python
            e = litint(1).not_in(id_("foo")) # builtin check, no alias
            assert e.into_code() == "1 not in foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.NotIn, self, other)

    def is_(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Identity test operation.

        Examples:
            ```python
            e = litint(1).is_(id_("foo")) # builtin check, no alias
            assert e.into_code() == "1 is foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.Is, self, other)

    def is_not(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Negative identity test operation.

        Examples:
            ```python
            e = litint(1).is_not(id_("foo")) # builtin check, no alias
            assert e.into_code() == "1 is not foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.IsNot, self, other)

    def bool_and(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Boolean AND operation.

        Examples:
            ```python
            e = litint(1).bool_and(id_("foo")) # builtin operation, no alias
            assert e.into_code() == "1 and foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.BoolAnd, self, other)

    def bool_or(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Boolean OR operation.

        Examples:
            ```python
            e = litint(1).bool_or(id_("foo")) # builtin operation, no alias
            assert e.into_code() == "1 or foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.BoolOr, self, other)

    def bit_and(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Bitwise AND operation.

        Examples:
            ```python
            e = litint(1).bit_and(id_("foo")) # alias ... & ...
            assert e.into_code() == "1 & foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.BitAnd, self, other)

    def bit_or(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Bitwise OR operation.

        Examples:
            ```python
            e = litint(1).bit_or(id_("foo")) # alias ... | ...
            assert e.into_code() == "1 | foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.BitOr, self, other)

    def bit_xor(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Bitwise XOR operation.

        Examples:
            ```python
            e = litint(1).bit_xor(id_("foo")) # alias ... ^ ...
            assert e.into_code() == "1 ^ foo"
            ```
        """
        return binary_op.BinaryOp(binary_op.BinaryOpType.BitXor, self, other)

    # bin op > magic method

    def __lt__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`lt`][synt.expr.expr.Expression.lt]."""
        return self.lt(other)

    def __le__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`le`][synt.expr.expr.Expression.le]."""
        return self.le(other)

    def __gt__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`gt`][synt.expr.expr.Expression.gt]."""
        return self.gt(other)

    def __ge__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`ge`][synt.expr.expr.Expression.ge]."""
        return self.ge(other)

    def __eq__(self, other: IntoExpression) -> binary_op.BinaryOp:  # type:ignore[override]
        """Alias [`eq`][synt.expr.expr.Expression.eq]."""
        return self.eq(other)

    def __ne__(self, other: IntoExpression) -> binary_op.BinaryOp:  # type:ignore[override]
        """Alias [`ne`][synt.expr.expr.Expression.ne]."""
        return self.ne(other)

    def __lshift__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`lshift`][synt.expr.expr.Expression.lshift]."""
        return self.lshift(other)

    def __rshift__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`rshift`][synt.expr.expr.Expression.rshift]."""
        return self.rshift(other)

    def __and__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`bit_and`][synt.expr.expr.Expression.bit_and]."""
        return self.bit_and(other)

    def __or__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`bit_or`][synt.expr.expr.Expression.bit_or]."""
        return self.bit_or(other)

    def __xor__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`bit_xor`][synt.expr.expr.Expression.bit_xor]."""
        return self.bit_xor(other)

    def __add__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`add`][synt.expr.expr.Expression.add]."""
        return self.add(other)

    def __sub__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`sub`][synt.expr.expr.Expression.sub]."""
        return self.sub(other)

    def __mul__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`mul`][synt.expr.expr.Expression.mul]."""
        return self.mul(other)

    def __truediv__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`div`][synt.expr.expr.Expression.div]."""
        return self.div(other)

    def __floordiv__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`floor_div`][synt.expr.expr.Expression.floor_div]."""
        return self.floor_div(other)

    def __mod__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`mod`][synt.expr.expr.Expression.mod]."""
        return self.mod(other)

    def __matmul__(self, other: IntoExpression) -> binary_op.BinaryOp:
        """Alias [`at`][synt.expr.expr.Expression.at]."""
        return self.at(other)

    # alias for unary operation

    # unary op > plain method

    def positive(self) -> unary_op.UnaryOp:
        """Positive operation, alias [`positive` function][synt.expr.unary_op.positive]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Positive, self)

    def negative(self) -> unary_op.UnaryOp:
        """Negative operation, alias [`negative` function][synt.expr.unary_op.negative]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Neg, self)

    def neg(self) -> unary_op.UnaryOp:
        """Alias [`negative`][synt.expr.expr.Expression.negative]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Neg, self)

    def not_(self) -> unary_op.UnaryOp:
        """Boolean NOT operation, alias [`not_` function][synt.expr.unary_op.not_]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.BoolNot, self)

    def bool_not(self) -> unary_op.UnaryOp:
        """Alias [`not_`][synt.expr.expr.Expression.not_]."""
        return self.not_()

    def invert(self) -> unary_op.UnaryOp:
        """Bitwise NOT operation, alias [`invert` function][synt.expr.unary_op.invert]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.BitNot, self)

    def bit_not(self) -> unary_op.UnaryOp:
        """Alias [`invert`][synt.expr.expr.Expression.invert]."""
        return self.invert()

    def await_(self) -> unary_op.UnaryOp:
        """Await operation, alias [`await_` function][synt.expr.unary_op.await_]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Await, self)

    def awaited(self) -> unary_op.UnaryOp:
        """Alias [`await_`][synt.expr.expr.Expression.await_]"""
        return self.await_()

    def unpack(self) -> unary_op.UnaryOp:
        """Sequence unpacking operation, Alias [`unpack` function][synt.expr.unary_op.unpack]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Starred, self)

    def starred(self) -> unary_op.UnaryOp:
        """Alias [`unpack`][synt.expr.expr.Expression.unpack]."""
        return self.unpack()

    def unpack_seq(self) -> unary_op.UnaryOp:
        """Alias [`unpack`][synt.expr.expr.Expression.unpack]."""
        return self.unpack()

    def unpack_kv(self) -> unary_op.UnaryOp:
        """K-V pair unpacking operation, Alias [`unpack_kv` function][synt.expr.unary_op.unpack_kv]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.DoubleStarred, self)

    def double_starred(self) -> unary_op.UnaryOp:
        """Alias [`unpack_kv`][synt.expr.expr.Expression.unpack_kv]."""
        return self.unpack_kv()

    def unpack_dict(self) -> unary_op.UnaryOp:
        """Alias [`unpack_kv`][synt.expr.expr.Expression.unpack_kv]."""
        return self.unpack_kv()

    def yield_(self) -> unary_op.UnaryOp:
        """Yield operation, alias [`yield_` function][synt.expr.unary_op.yield_]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.Yield, self)

    def yield_from(self) -> unary_op.UnaryOp:
        """Yield from operation, alias [`yield_from` function][synt.expr.unary_op.yield_from]."""
        return unary_op.UnaryOp(unary_op.UnaryOpType.YieldFrom, self)

    # unary op > magic method

    def __neg__(self) -> unary_op.UnaryOp:
        """Alias [`neg`][synt.expr.expr.Expression.neg]."""
        return self.neg()

    def __not__(self) -> unary_op.UnaryOp:
        """Alias [`not_`][synt.expr.expr.Expression.not_]."""
        return self.not_()

    def __invert__(self) -> unary_op.UnaryOp:
        """Alias [`invert`][synt.expr.expr.Expression.invert]."""
        return self.invert()

    # alias for named value

    def named(self, expr: IntoExpression) -> named_expr.NamedExpr:
        """Assign the expression to `self`.

        Args:
            expr: The expression to assign.

        Raises:
            ValueError: If `self` is not an identifier.

        Examples:
            ```python
            named_expr = id_('a').expr().named(litint(1))
            assert named_expr.into_code() == "a := 1"
            ```
        """
        return named_expr.NamedExpr(self.ensure_identifier(), expr)

    def named_as(self, target: Identifier) -> named_expr.NamedExpr:
        """Assign self to the target.

        Args:
            target: The target identifier.

        Examples:
            ```python
            named_as_expr = (litint(1) + litint(2)).named_as(id_('a')).is_(TRUE)
            assert named_as_expr.into_code() == "(a := 1 + 2) is True"
            ```
        """
        return named_expr.NamedExpr(target, self)

    # alias for attribute

    def attr(self, attr: str) -> attribute.Attribute:
        """attribute.Attribute getting operation.

        Args:
            attr: The attribute name.

        Examples:
            ```python
            attr_expr = id_('a').expr().attr('b').expr().call(litint(1), litint(2))
            assert attr_expr.into_code() == "a.b(1, 2)"
            ```
        """
        return attribute.Attribute(self, attr)

    # alias for call

    def call(
        self,
        *args: IntoExpression | tuple[Identifier, IntoExpression],
        **kwargs: IntoExpression,
    ) -> call.Call:
        """Calling a function or object.

        Args:
            *args: Positional arguments.
            **kwargs: Keyword arguments.

        Raises:
            ValueError: If any argument is not [`IntoExpression`][synt.expr.expr.IntoExpression].

        Examples:
            ```python
            call_expr = id_('a').expr().call(litint(1), litint(2)).call(kw=litint(3))
            assert call_expr.into_code() == "a(1, 2)(kw=3)"
            ```
            With dynamic keyword arguments:
            ```python
            call_expr = id_('a').expr().call(kwarg(id_('b'), litint(42)))
            assert call_expr.into_code() == "a(b=42)"
            ```
        """
        from synt.tokens.ident import Identifier

        kwarg: list[call.Keyword] = []
        arg: list[IntoExpression] = []
        for a in args:
            if isinstance(a, tuple):
                kwarg.append(call.Keyword(a[0], a[1]))
                continue
            if type_check.is_into_expr(a):
                arg.append(a)
                continue
            raise ValueError(f"Invalid argument: {a}")

        for k, v in kwargs.items():
            kwarg.append(call.Keyword(Identifier(k), v))

        return call.Call(self, arg, kwarg)

    # alias for comprehension

    def for_(self, *target: Identifier) -> comprehension.ComprehensionNodeBuilder:
        """Initialize a new comprehension.

        Args:
            target: The target of the iteration.

        Examples:
            ```python
            # `comp_expr` here only implements `IntoExpression` and `IntoCode`
            # because it's still a non-finished builder
            comp_expr = list_comp(id_('x').expr()
                .for_(id_('x')).in_(id_('range').expr().call(litint(5)))
                .if_((id_('x').expr() % litint(2)) == litint(0)))
            assert comp_expr.into_code() == "[x for x in range(5) if x % 2 == 0]"
            ```
        """
        return comprehension.ComprehensionBuilder.init(self, list(target), False)

    def async_for(self, *target: Identifier) -> comprehension.ComprehensionNodeBuilder:
        """Initialize a new async comprehension.

        Args:
            target: The iterator expression of the comprehension.

        Examples:
            ```python
            # `comp_expr` here only implements `IntoExpression` and `IntoCode`
            # because it's still a non-finished builder
            comp_expr = list_comp(id_('x').expr()
                .async_for(id_('x')).in_(id_('range').expr().call(litint(5)))
                .if_((id_('x').expr() % litint(2)) == litint(0)))
            assert comp_expr.into_code() == "[x async for x in range(5) if x % 2 == 0]"
            ```
        """
        return comprehension.ComprehensionBuilder.init(self, list(target), True)

    # alias for condition

    def if_(self, cond: IntoExpression) -> condition.ConditionBuilder:
        """Initialize a new condition expression.

        Args:
            cond: The condition expression.

        Examples:
            ```python
            cond_expr = id_('a').expr().if_(id_('b').expr() > litint(0)).else_(litstr('foo'))
            assert cond_expr.into_code() == "a if b > 0 else 'foo'"
            ```
        """
        return condition.ConditionBuilder(cond, self)

    # alias for subscript

    # subscript > plain method

    def subscribe(
        self, *slices: subscript.Slice | IntoExpression
    ) -> subscript.Subscript:
        """Subscribe to the expression.

        Args:
            slices: The slice expressions.

        Examples:
            ```python
            subscript_expr = id_('a').expr().subscribe(id_('b').expr(), slice_(litint(2), litint(3)))
            # also alias: ...[...]
            assert subscript_expr.into_code() == "a[b, 2:3]"
            ```
        """
        return subscript.Subscript(self, list(slices))

    # subscript > magic method

    def __getitem__(
        self,
        items: tuple[subscript.Slice | IntoExpression, ...]
        | subscript.Slice
        | IntoExpression,
    ) -> subscript.Subscript:
        """Alias [`subscribe`][synt.expr.expr.Expression.subscribe]."""
        if isinstance(items, tuple):
            return self.subscribe(*items)
        else:
            return self.subscribe(items)

    # wrap

    def wrap(self) -> expr_wrapped.Wrapped:
        """Wrap `self` in a pair of parentheses, alias [`Wrapped`][synt.expr.wrapped.Wrapped]."""
        return expr_wrapped.Wrapped(self)

    def wrapped(self) -> expr_wrapped.Wrapped:
        """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
        return self.wrap()

    def par(self) -> expr_wrapped.Wrapped:
        """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
        return self.wrap()

    def atom(self) -> expr_wrapped.Wrapped:
        """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
        return self.wrap()

    # statement constructors

    # assignment

    def assign(self, value: IntoExpression) -> stmt_assign.Assignment:
        """Create a new assignment statement, take `self` as the target.

        Args:
            value: The value to assign.
        """
        return stmt_assign.Assignment(self).assign(value)

    def assign_to(self, target: IntoExpression) -> stmt_assign.Assignment:
        """Create a new assignment statement, take `self` as the value.

        Args:
            target: The value to be assigned.
        """
        return stmt_assign.Assignment(target.into_expression()).assign(self)

    def type(self, ty: IntoExpression) -> stmt_assign.Assignment:
        """Create a new typed assignment statement, take `self` as the target.

        Args:
            ty: The type of the target.
        """
        return stmt_assign.Assignment(self).type(ty)

    def ty(self, ty: IntoExpression) -> stmt_assign.Assignment:
        """Alias [`type`][synt.expr.expr.Expression.type]."""
        return self.type(ty)

    def stmt(self) -> Statement:
        """Convert the expression into a statement."""
        from synt.stmt.expression import stmt

        return stmt(self)

    def as_(self, target: Identifier) -> Alias:
        """Convert the expression into an alias.

        Args:
            target: The target identifier.
        """
        from synt.expr.alias import Alias

        return Alias(self, target)

precedence abstractmethod property ¤

precedence: ExprPrecedence

The expression's precedence.

expr_type abstractmethod property ¤

expr_type: ExprType

The expression's type.

into_code abstractmethod ¤

into_code() -> str

Convert the expression into Python code.

No Formatting!

Source code in synt/expr/expr.py
@abstractmethod
def into_code(self) -> str:
    """Convert the expression into Python code.

    **No Formatting!**
    """

into_expression ¤

into_expression() -> Expression

An Expression can always be converted into an Expression.

Source code in synt/expr/expr.py
def into_expression(self) -> Expression:
    """An `Expression` can always be converted into an `Expression`."""
    return self

ensure_identifier ¤

ensure_identifier() -> Identifier

Ensure that the expression is an identifier and returns it.

Raises:

Type Description
ValueError

If the expression is not an identifier.

Source code in synt/expr/expr.py
def ensure_identifier(self) -> Identifier:
    """Ensure that the expression is an identifier and returns it.

    Raises:
        ValueError: If the expression is not an identifier.
    """
    if type_check.is_ident(self):
        return self.ident
    else:
        raise ValueError("Expression is not an identifier")

add ¤

add(other: IntoExpression) -> BinaryOp

Add operation.

Examples:

e = litint(1).add(id_("foo")) # alias ... + ...
assert e.into_code() == "1 + foo"
Source code in synt/expr/expr.py
def add(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Add operation.

    Examples:
        ```python
        e = litint(1).add(id_("foo")) # alias ... + ...
        assert e.into_code() == "1 + foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Add, self, other)

sub ¤

sub(other: IntoExpression) -> BinaryOp

Subtract operation.

Examples:

e = litint(1).sub(id_("foo")) # alias ... - ...
assert e.into_code() == "1 - foo"
Source code in synt/expr/expr.py
def sub(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Subtract operation.

    Examples:
        ```python
        e = litint(1).sub(id_("foo")) # alias ... - ...
        assert e.into_code() == "1 - foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Sub, self, other)

mul ¤

mul(other: IntoExpression) -> BinaryOp

Multiply operation.

Examples:

e = litint(1).mul(id_("foo")) # alias ... * ...
assert e.into_code() == "1 * foo"
Source code in synt/expr/expr.py
def mul(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Multiply operation.

    Examples:
        ```python
        e = litint(1).mul(id_("foo")) # alias ... * ...
        assert e.into_code() == "1 * foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Mul, self, other)

div ¤

div(other: IntoExpression) -> BinaryOp

Divide operation.

Examples:

e = litint(1).div(id_("foo")) # alias ... / ...
assert e.into_code() == "1 / foo"
Source code in synt/expr/expr.py
def div(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Divide operation.

    Examples:
        ```python
        e = litint(1).div(id_("foo")) # alias ... / ...
        assert e.into_code() == "1 / foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Div, self, other)

truediv ¤

truediv(other: IntoExpression) -> BinaryOp

Alias div.

Source code in synt/expr/expr.py
def truediv(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`div`][synt.expr.expr.Expression.div]."""
    return self.div(other)

floor_div ¤

floor_div(other: IntoExpression) -> BinaryOp

Floor divide operation.

Examples:

e = litint(1).floor_div(id_("foo")) # alias ... // ...
assert e.into_code() == "1 // foo"
Source code in synt/expr/expr.py
def floor_div(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Floor divide operation.

    Examples:
        ```python
        e = litint(1).floor_div(id_("foo")) # alias ... // ...
        assert e.into_code() == "1 // foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.FloorDiv, self, other)

mod ¤

mod(other: IntoExpression) -> BinaryOp

Modulus operation.

Examples:

e = litint(1).mod(id_("foo")) # alias ... % ...
assert e.into_code() == "1 % foo"
Source code in synt/expr/expr.py
def mod(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Modulus operation.

    Examples:
        ```python
        e = litint(1).mod(id_("foo")) # alias ... % ...
        assert e.into_code() == "1 % foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Mod, self, other)

pow ¤

pow(other: IntoExpression) -> BinaryOp

Exponentiation operation.

Examples:

e = litint(1).pow(id_("foo")) # alias ... ** ...
assert e.into_code() == "1 ** foo"
Source code in synt/expr/expr.py
def pow(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Exponentiation operation.

    Examples:
        ```python
        e = litint(1).pow(id_("foo")) # alias ... ** ...
        assert e.into_code() == "1 ** foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Pow, self, other)

at ¤

at(other: IntoExpression) -> BinaryOp

At(matrix multiplication) operation.

Examples:

e = litint(1).at(id_("foo")) # alias ... @ ...
assert e.into_code() == "1 @ foo"
Source code in synt/expr/expr.py
def at(self, other: IntoExpression) -> binary_op.BinaryOp:
    """At(matrix multiplication) operation.

    Examples:
        ```python
        e = litint(1).at(id_("foo")) # alias ... @ ...
        assert e.into_code() == "1 @ foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.At, self, other)

matmul ¤

matmul(other: IntoExpression) -> BinaryOp

Alias at.

Source code in synt/expr/expr.py
def matmul(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`at`][synt.expr.expr.Expression.matmul]."""
    return self.at(other)

lshift ¤

lshift(other: IntoExpression) -> BinaryOp

Left shift operation.

Examples:

e = litint(1).lshift(id_("foo")) # alias ... << ...
assert e.into_code() == "1 << foo"
Source code in synt/expr/expr.py
def lshift(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Left shift operation.

    Examples:
        ```python
        e = litint(1).lshift(id_("foo")) # alias ... << ...
        assert e.into_code() == "1 << foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.LShift, self, other)

rshift ¤

rshift(other: IntoExpression) -> BinaryOp

Right shift operation.

Examples:

e = litint(1).rshift(id_("foo")) # alias ... >> ...
assert e.into_code() == "1 >> foo"
Source code in synt/expr/expr.py
def rshift(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Right shift operation.

    Examples:
        ```python
        e = litint(1).rshift(id_("foo")) # alias ... >> ...
        assert e.into_code() == "1 >> foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.RShift, self, other)

lt ¤

lt(other: IntoExpression) -> BinaryOp

Less than operation.

Examples:

e = litint(1).lt(id_("foo")) # alias ... < ...
assert e.into_code() == "1 < foo"
Source code in synt/expr/expr.py
def lt(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Less than operation.

    Examples:
        ```python
        e = litint(1).lt(id_("foo")) # alias ... < ...
        assert e.into_code() == "1 < foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Less, self, other)

le ¤

le(other: IntoExpression) -> BinaryOp

Less than or equal to operation.

Examples:

e = litint(1).le(id_("foo")) # alias ... <= ...
assert e.into_code() == "1 <= foo"
Source code in synt/expr/expr.py
def le(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Less than or equal to operation.

    Examples:
        ```python
        e = litint(1).le(id_("foo")) # alias ... <= ...
        assert e.into_code() == "1 <= foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.LessEqual, self, other)

gt ¤

gt(other: IntoExpression) -> BinaryOp

Greater than operation.

Examples:

e = litint(1).gt(id_("foo")) # alias ... > ...
assert e.into_code() == "1 > foo"
Source code in synt/expr/expr.py
def gt(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Greater than operation.

    Examples:
        ```python
        e = litint(1).gt(id_("foo")) # alias ... > ...
        assert e.into_code() == "1 > foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Greater, self, other)

ge ¤

ge(other: IntoExpression) -> BinaryOp

Greater than or equal to operation.

Examples:

e = litint(1).ge(id_("foo")) # alias ... >= ...
assert e.into_code() == "1 >= foo"
Source code in synt/expr/expr.py
def ge(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Greater than or equal to operation.

    Examples:
        ```python
        e = litint(1).ge(id_("foo")) # alias ... >= ...
        assert e.into_code() == "1 >= foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.GreaterEqual, self, other)

eq ¤

eq(other: IntoExpression) -> BinaryOp

Equal to operation.

Examples:

e = litint(1).eq(id_("foo")) # alias ... == ...
assert e.into_code() == "1 == foo"
Source code in synt/expr/expr.py
def eq(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Equal to operation.

    Examples:
        ```python
        e = litint(1).eq(id_("foo")) # alias ... == ...
        assert e.into_code() == "1 == foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Equal, self, other)

ne ¤

ne(other: IntoExpression) -> BinaryOp

Not equal to operation.

Examples:

e = litint(1).ne(id_("foo")) # alias ... != ...
assert e.into_code() == "1 != foo"
Source code in synt/expr/expr.py
def ne(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Not equal to operation.

    Examples:
        ```python
        e = litint(1).ne(id_("foo")) # alias ... != ...
        assert e.into_code() == "1 != foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.NotEqual, self, other)

in_ ¤

in_(other: IntoExpression) -> BinaryOp

Membership test operation.

Examples:

e = litint(1).in_(id_("foo")) # builtin check, no alias
assert e.into_code() == "1 in foo"
Source code in synt/expr/expr.py
def in_(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Membership test operation.

    Examples:
        ```python
        e = litint(1).in_(id_("foo")) # builtin check, no alias
        assert e.into_code() == "1 in foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.In, self, other)

not_in ¤

not_in(other: IntoExpression) -> BinaryOp

Negative membership test operation.

Examples:

e = litint(1).not_in(id_("foo")) # builtin check, no alias
assert e.into_code() == "1 not in foo"
Source code in synt/expr/expr.py
def not_in(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Negative membership test operation.

    Examples:
        ```python
        e = litint(1).not_in(id_("foo")) # builtin check, no alias
        assert e.into_code() == "1 not in foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.NotIn, self, other)

is_ ¤

is_(other: IntoExpression) -> BinaryOp

Identity test operation.

Examples:

e = litint(1).is_(id_("foo")) # builtin check, no alias
assert e.into_code() == "1 is foo"
Source code in synt/expr/expr.py
def is_(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Identity test operation.

    Examples:
        ```python
        e = litint(1).is_(id_("foo")) # builtin check, no alias
        assert e.into_code() == "1 is foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.Is, self, other)

is_not ¤

is_not(other: IntoExpression) -> BinaryOp

Negative identity test operation.

Examples:

e = litint(1).is_not(id_("foo")) # builtin check, no alias
assert e.into_code() == "1 is not foo"
Source code in synt/expr/expr.py
def is_not(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Negative identity test operation.

    Examples:
        ```python
        e = litint(1).is_not(id_("foo")) # builtin check, no alias
        assert e.into_code() == "1 is not foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.IsNot, self, other)

bool_and ¤

bool_and(other: IntoExpression) -> BinaryOp

Boolean AND operation.

Examples:

e = litint(1).bool_and(id_("foo")) # builtin operation, no alias
assert e.into_code() == "1 and foo"
Source code in synt/expr/expr.py
def bool_and(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Boolean AND operation.

    Examples:
        ```python
        e = litint(1).bool_and(id_("foo")) # builtin operation, no alias
        assert e.into_code() == "1 and foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.BoolAnd, self, other)

bool_or ¤

bool_or(other: IntoExpression) -> BinaryOp

Boolean OR operation.

Examples:

e = litint(1).bool_or(id_("foo")) # builtin operation, no alias
assert e.into_code() == "1 or foo"
Source code in synt/expr/expr.py
def bool_or(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Boolean OR operation.

    Examples:
        ```python
        e = litint(1).bool_or(id_("foo")) # builtin operation, no alias
        assert e.into_code() == "1 or foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.BoolOr, self, other)

bit_and ¤

bit_and(other: IntoExpression) -> BinaryOp

Bitwise AND operation.

Examples:

e = litint(1).bit_and(id_("foo")) # alias ... & ...
assert e.into_code() == "1 & foo"
Source code in synt/expr/expr.py
def bit_and(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Bitwise AND operation.

    Examples:
        ```python
        e = litint(1).bit_and(id_("foo")) # alias ... & ...
        assert e.into_code() == "1 & foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.BitAnd, self, other)

bit_or ¤

bit_or(other: IntoExpression) -> BinaryOp

Bitwise OR operation.

Examples:

e = litint(1).bit_or(id_("foo")) # alias ... | ...
assert e.into_code() == "1 | foo"
Source code in synt/expr/expr.py
def bit_or(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Bitwise OR operation.

    Examples:
        ```python
        e = litint(1).bit_or(id_("foo")) # alias ... | ...
        assert e.into_code() == "1 | foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.BitOr, self, other)

bit_xor ¤

bit_xor(other: IntoExpression) -> BinaryOp

Bitwise XOR operation.

Examples:

e = litint(1).bit_xor(id_("foo")) # alias ... ^ ...
assert e.into_code() == "1 ^ foo"
Source code in synt/expr/expr.py
def bit_xor(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Bitwise XOR operation.

    Examples:
        ```python
        e = litint(1).bit_xor(id_("foo")) # alias ... ^ ...
        assert e.into_code() == "1 ^ foo"
        ```
    """
    return binary_op.BinaryOp(binary_op.BinaryOpType.BitXor, self, other)

__lt__ ¤

__lt__(other: IntoExpression) -> BinaryOp

Alias lt.

Source code in synt/expr/expr.py
def __lt__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`lt`][synt.expr.expr.Expression.lt]."""
    return self.lt(other)

__le__ ¤

__le__(other: IntoExpression) -> BinaryOp

Alias le.

Source code in synt/expr/expr.py
def __le__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`le`][synt.expr.expr.Expression.le]."""
    return self.le(other)

__gt__ ¤

__gt__(other: IntoExpression) -> BinaryOp

Alias gt.

Source code in synt/expr/expr.py
def __gt__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`gt`][synt.expr.expr.Expression.gt]."""
    return self.gt(other)

__ge__ ¤

__ge__(other: IntoExpression) -> BinaryOp

Alias ge.

Source code in synt/expr/expr.py
def __ge__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`ge`][synt.expr.expr.Expression.ge]."""
    return self.ge(other)

__eq__ ¤

__eq__(other: IntoExpression) -> BinaryOp

Alias eq.

Source code in synt/expr/expr.py
def __eq__(self, other: IntoExpression) -> binary_op.BinaryOp:  # type:ignore[override]
    """Alias [`eq`][synt.expr.expr.Expression.eq]."""
    return self.eq(other)

__ne__ ¤

__ne__(other: IntoExpression) -> BinaryOp

Alias ne.

Source code in synt/expr/expr.py
def __ne__(self, other: IntoExpression) -> binary_op.BinaryOp:  # type:ignore[override]
    """Alias [`ne`][synt.expr.expr.Expression.ne]."""
    return self.ne(other)

__lshift__ ¤

__lshift__(other: IntoExpression) -> BinaryOp

Alias lshift.

Source code in synt/expr/expr.py
def __lshift__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`lshift`][synt.expr.expr.Expression.lshift]."""
    return self.lshift(other)

__rshift__ ¤

__rshift__(other: IntoExpression) -> BinaryOp

Alias rshift.

Source code in synt/expr/expr.py
def __rshift__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`rshift`][synt.expr.expr.Expression.rshift]."""
    return self.rshift(other)

__and__ ¤

__and__(other: IntoExpression) -> BinaryOp

Alias bit_and.

Source code in synt/expr/expr.py
def __and__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`bit_and`][synt.expr.expr.Expression.bit_and]."""
    return self.bit_and(other)

__or__ ¤

__or__(other: IntoExpression) -> BinaryOp

Alias bit_or.

Source code in synt/expr/expr.py
def __or__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`bit_or`][synt.expr.expr.Expression.bit_or]."""
    return self.bit_or(other)

__xor__ ¤

__xor__(other: IntoExpression) -> BinaryOp

Alias bit_xor.

Source code in synt/expr/expr.py
def __xor__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`bit_xor`][synt.expr.expr.Expression.bit_xor]."""
    return self.bit_xor(other)

__add__ ¤

__add__(other: IntoExpression) -> BinaryOp

Alias add.

Source code in synt/expr/expr.py
def __add__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`add`][synt.expr.expr.Expression.add]."""
    return self.add(other)

__sub__ ¤

__sub__(other: IntoExpression) -> BinaryOp

Alias sub.

Source code in synt/expr/expr.py
def __sub__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`sub`][synt.expr.expr.Expression.sub]."""
    return self.sub(other)

__mul__ ¤

__mul__(other: IntoExpression) -> BinaryOp

Alias mul.

Source code in synt/expr/expr.py
def __mul__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`mul`][synt.expr.expr.Expression.mul]."""
    return self.mul(other)

__truediv__ ¤

__truediv__(other: IntoExpression) -> BinaryOp

Alias div.

Source code in synt/expr/expr.py
def __truediv__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`div`][synt.expr.expr.Expression.div]."""
    return self.div(other)

__floordiv__ ¤

__floordiv__(other: IntoExpression) -> BinaryOp

Alias floor_div.

Source code in synt/expr/expr.py
def __floordiv__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`floor_div`][synt.expr.expr.Expression.floor_div]."""
    return self.floor_div(other)

__mod__ ¤

__mod__(other: IntoExpression) -> BinaryOp

Alias mod.

Source code in synt/expr/expr.py
def __mod__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`mod`][synt.expr.expr.Expression.mod]."""
    return self.mod(other)

__matmul__ ¤

__matmul__(other: IntoExpression) -> BinaryOp

Alias at.

Source code in synt/expr/expr.py
def __matmul__(self, other: IntoExpression) -> binary_op.BinaryOp:
    """Alias [`at`][synt.expr.expr.Expression.at]."""
    return self.at(other)

positive ¤

positive() -> UnaryOp

Positive operation, alias positive function.

Source code in synt/expr/expr.py
def positive(self) -> unary_op.UnaryOp:
    """Positive operation, alias [`positive` function][synt.expr.unary_op.positive]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Positive, self)

negative ¤

negative() -> UnaryOp

Negative operation, alias negative function.

Source code in synt/expr/expr.py
def negative(self) -> unary_op.UnaryOp:
    """Negative operation, alias [`negative` function][synt.expr.unary_op.negative]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Neg, self)

neg ¤

neg() -> UnaryOp

Alias negative.

Source code in synt/expr/expr.py
def neg(self) -> unary_op.UnaryOp:
    """Alias [`negative`][synt.expr.expr.Expression.negative]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Neg, self)

not_ ¤

not_() -> UnaryOp

Boolean NOT operation, alias not_ function.

Source code in synt/expr/expr.py
def not_(self) -> unary_op.UnaryOp:
    """Boolean NOT operation, alias [`not_` function][synt.expr.unary_op.not_]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.BoolNot, self)

bool_not ¤

bool_not() -> UnaryOp

Alias not_.

Source code in synt/expr/expr.py
def bool_not(self) -> unary_op.UnaryOp:
    """Alias [`not_`][synt.expr.expr.Expression.not_]."""
    return self.not_()

invert ¤

invert() -> UnaryOp

Bitwise NOT operation, alias invert function.

Source code in synt/expr/expr.py
def invert(self) -> unary_op.UnaryOp:
    """Bitwise NOT operation, alias [`invert` function][synt.expr.unary_op.invert]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.BitNot, self)

bit_not ¤

bit_not() -> UnaryOp

Alias invert.

Source code in synt/expr/expr.py
def bit_not(self) -> unary_op.UnaryOp:
    """Alias [`invert`][synt.expr.expr.Expression.invert]."""
    return self.invert()

await_ ¤

await_() -> UnaryOp

Await operation, alias await_ function.

Source code in synt/expr/expr.py
def await_(self) -> unary_op.UnaryOp:
    """Await operation, alias [`await_` function][synt.expr.unary_op.await_]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Await, self)

awaited ¤

awaited() -> UnaryOp

Alias await_

Source code in synt/expr/expr.py
def awaited(self) -> unary_op.UnaryOp:
    """Alias [`await_`][synt.expr.expr.Expression.await_]"""
    return self.await_()

unpack ¤

unpack() -> UnaryOp

Sequence unpacking operation, Alias unpack function.

Source code in synt/expr/expr.py
def unpack(self) -> unary_op.UnaryOp:
    """Sequence unpacking operation, Alias [`unpack` function][synt.expr.unary_op.unpack]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Starred, self)

starred ¤

starred() -> UnaryOp

Alias unpack.

Source code in synt/expr/expr.py
def starred(self) -> unary_op.UnaryOp:
    """Alias [`unpack`][synt.expr.expr.Expression.unpack]."""
    return self.unpack()

unpack_seq ¤

unpack_seq() -> UnaryOp

Alias unpack.

Source code in synt/expr/expr.py
def unpack_seq(self) -> unary_op.UnaryOp:
    """Alias [`unpack`][synt.expr.expr.Expression.unpack]."""
    return self.unpack()

unpack_kv ¤

unpack_kv() -> UnaryOp

K-V pair unpacking operation, Alias unpack_kv function.

Source code in synt/expr/expr.py
def unpack_kv(self) -> unary_op.UnaryOp:
    """K-V pair unpacking operation, Alias [`unpack_kv` function][synt.expr.unary_op.unpack_kv]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.DoubleStarred, self)

double_starred ¤

double_starred() -> UnaryOp

Alias unpack_kv.

Source code in synt/expr/expr.py
def double_starred(self) -> unary_op.UnaryOp:
    """Alias [`unpack_kv`][synt.expr.expr.Expression.unpack_kv]."""
    return self.unpack_kv()

unpack_dict ¤

unpack_dict() -> UnaryOp

Alias unpack_kv.

Source code in synt/expr/expr.py
def unpack_dict(self) -> unary_op.UnaryOp:
    """Alias [`unpack_kv`][synt.expr.expr.Expression.unpack_kv]."""
    return self.unpack_kv()

yield_ ¤

yield_() -> UnaryOp

Yield operation, alias yield_ function.

Source code in synt/expr/expr.py
def yield_(self) -> unary_op.UnaryOp:
    """Yield operation, alias [`yield_` function][synt.expr.unary_op.yield_]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.Yield, self)

yield_from ¤

yield_from() -> UnaryOp

Yield from operation, alias yield_from function.

Source code in synt/expr/expr.py
def yield_from(self) -> unary_op.UnaryOp:
    """Yield from operation, alias [`yield_from` function][synt.expr.unary_op.yield_from]."""
    return unary_op.UnaryOp(unary_op.UnaryOpType.YieldFrom, self)

__neg__ ¤

__neg__() -> UnaryOp

Alias neg.

Source code in synt/expr/expr.py
def __neg__(self) -> unary_op.UnaryOp:
    """Alias [`neg`][synt.expr.expr.Expression.neg]."""
    return self.neg()

__not__ ¤

__not__() -> UnaryOp

Alias not_.

Source code in synt/expr/expr.py
def __not__(self) -> unary_op.UnaryOp:
    """Alias [`not_`][synt.expr.expr.Expression.not_]."""
    return self.not_()

__invert__ ¤

__invert__() -> UnaryOp

Alias invert.

Source code in synt/expr/expr.py
def __invert__(self) -> unary_op.UnaryOp:
    """Alias [`invert`][synt.expr.expr.Expression.invert]."""
    return self.invert()

named ¤

named(expr: IntoExpression) -> NamedExpr

Assign the expression to self.

Parameters:

Name Type Description Default
expr IntoExpression

The expression to assign.

required

Raises:

Type Description
ValueError

If self is not an identifier.

Examples:

named_expr = id_('a').expr().named(litint(1))
assert named_expr.into_code() == "a := 1"
Source code in synt/expr/expr.py
def named(self, expr: IntoExpression) -> named_expr.NamedExpr:
    """Assign the expression to `self`.

    Args:
        expr: The expression to assign.

    Raises:
        ValueError: If `self` is not an identifier.

    Examples:
        ```python
        named_expr = id_('a').expr().named(litint(1))
        assert named_expr.into_code() == "a := 1"
        ```
    """
    return named_expr.NamedExpr(self.ensure_identifier(), expr)

named_as ¤

named_as(target: Identifier) -> NamedExpr

Assign self to the target.

Parameters:

Name Type Description Default
target Identifier

The target identifier.

required

Examples:

named_as_expr = (litint(1) + litint(2)).named_as(id_('a')).is_(TRUE)
assert named_as_expr.into_code() == "(a := 1 + 2) is True"
Source code in synt/expr/expr.py
def named_as(self, target: Identifier) -> named_expr.NamedExpr:
    """Assign self to the target.

    Args:
        target: The target identifier.

    Examples:
        ```python
        named_as_expr = (litint(1) + litint(2)).named_as(id_('a')).is_(TRUE)
        assert named_as_expr.into_code() == "(a := 1 + 2) is True"
        ```
    """
    return named_expr.NamedExpr(target, self)

attr ¤

attr(attr: str) -> Attribute

attribute.Attribute getting operation.

Parameters:

Name Type Description Default
attr str

The attribute name.

required

Examples:

attr_expr = id_('a').expr().attr('b').expr().call(litint(1), litint(2))
assert attr_expr.into_code() == "a.b(1, 2)"
Source code in synt/expr/expr.py
def attr(self, attr: str) -> attribute.Attribute:
    """attribute.Attribute getting operation.

    Args:
        attr: The attribute name.

    Examples:
        ```python
        attr_expr = id_('a').expr().attr('b').expr().call(litint(1), litint(2))
        assert attr_expr.into_code() == "a.b(1, 2)"
        ```
    """
    return attribute.Attribute(self, attr)

call ¤

1
2
3
4
5
call(
    *args: IntoExpression
    | tuple[Identifier, IntoExpression],
    **kwargs: IntoExpression
) -> Call

Calling a function or object.

Parameters:

Name Type Description Default
*args IntoExpression | tuple[Identifier, IntoExpression]

Positional arguments.

()
**kwargs IntoExpression

Keyword arguments.

{}

Raises:

Type Description
ValueError

If any argument is not IntoExpression.

Examples:

call_expr = id_('a').expr().call(litint(1), litint(2)).call(kw=litint(3))
assert call_expr.into_code() == "a(1, 2)(kw=3)"
With dynamic keyword arguments:
call_expr = id_('a').expr().call(kwarg(id_('b'), litint(42)))
assert call_expr.into_code() == "a(b=42)"

Source code in synt/expr/expr.py
def call(
    self,
    *args: IntoExpression | tuple[Identifier, IntoExpression],
    **kwargs: IntoExpression,
) -> call.Call:
    """Calling a function or object.

    Args:
        *args: Positional arguments.
        **kwargs: Keyword arguments.

    Raises:
        ValueError: If any argument is not [`IntoExpression`][synt.expr.expr.IntoExpression].

    Examples:
        ```python
        call_expr = id_('a').expr().call(litint(1), litint(2)).call(kw=litint(3))
        assert call_expr.into_code() == "a(1, 2)(kw=3)"
        ```
        With dynamic keyword arguments:
        ```python
        call_expr = id_('a').expr().call(kwarg(id_('b'), litint(42)))
        assert call_expr.into_code() == "a(b=42)"
        ```
    """
    from synt.tokens.ident import Identifier

    kwarg: list[call.Keyword] = []
    arg: list[IntoExpression] = []
    for a in args:
        if isinstance(a, tuple):
            kwarg.append(call.Keyword(a[0], a[1]))
            continue
        if type_check.is_into_expr(a):
            arg.append(a)
            continue
        raise ValueError(f"Invalid argument: {a}")

    for k, v in kwargs.items():
        kwarg.append(call.Keyword(Identifier(k), v))

    return call.Call(self, arg, kwarg)

for_ ¤

Initialize a new comprehension.

Parameters:

Name Type Description Default
target Identifier

The target of the iteration.

()

Examples:

1
2
3
4
5
6
# `comp_expr` here only implements `IntoExpression` and `IntoCode`
# because it's still a non-finished builder
comp_expr = list_comp(id_('x').expr()
    .for_(id_('x')).in_(id_('range').expr().call(litint(5)))
    .if_((id_('x').expr() % litint(2)) == litint(0)))
assert comp_expr.into_code() == "[x for x in range(5) if x % 2 == 0]"
Source code in synt/expr/expr.py
def for_(self, *target: Identifier) -> comprehension.ComprehensionNodeBuilder:
    """Initialize a new comprehension.

    Args:
        target: The target of the iteration.

    Examples:
        ```python
        # `comp_expr` here only implements `IntoExpression` and `IntoCode`
        # because it's still a non-finished builder
        comp_expr = list_comp(id_('x').expr()
            .for_(id_('x')).in_(id_('range').expr().call(litint(5)))
            .if_((id_('x').expr() % litint(2)) == litint(0)))
        assert comp_expr.into_code() == "[x for x in range(5) if x % 2 == 0]"
        ```
    """
    return comprehension.ComprehensionBuilder.init(self, list(target), False)

async_for ¤

async_for(*target: Identifier) -> ComprehensionNodeBuilder

Initialize a new async comprehension.

Parameters:

Name Type Description Default
target Identifier

The iterator expression of the comprehension.

()

Examples:

1
2
3
4
5
6
# `comp_expr` here only implements `IntoExpression` and `IntoCode`
# because it's still a non-finished builder
comp_expr = list_comp(id_('x').expr()
    .async_for(id_('x')).in_(id_('range').expr().call(litint(5)))
    .if_((id_('x').expr() % litint(2)) == litint(0)))
assert comp_expr.into_code() == "[x async for x in range(5) if x % 2 == 0]"
Source code in synt/expr/expr.py
def async_for(self, *target: Identifier) -> comprehension.ComprehensionNodeBuilder:
    """Initialize a new async comprehension.

    Args:
        target: The iterator expression of the comprehension.

    Examples:
        ```python
        # `comp_expr` here only implements `IntoExpression` and `IntoCode`
        # because it's still a non-finished builder
        comp_expr = list_comp(id_('x').expr()
            .async_for(id_('x')).in_(id_('range').expr().call(litint(5)))
            .if_((id_('x').expr() % litint(2)) == litint(0)))
        assert comp_expr.into_code() == "[x async for x in range(5) if x % 2 == 0]"
        ```
    """
    return comprehension.ComprehensionBuilder.init(self, list(target), True)

if_ ¤

Initialize a new condition expression.

Parameters:

Name Type Description Default
cond IntoExpression

The condition expression.

required

Examples:

cond_expr = id_('a').expr().if_(id_('b').expr() > litint(0)).else_(litstr('foo'))
assert cond_expr.into_code() == "a if b > 0 else 'foo'"
Source code in synt/expr/expr.py
def if_(self, cond: IntoExpression) -> condition.ConditionBuilder:
    """Initialize a new condition expression.

    Args:
        cond: The condition expression.

    Examples:
        ```python
        cond_expr = id_('a').expr().if_(id_('b').expr() > litint(0)).else_(litstr('foo'))
        assert cond_expr.into_code() == "a if b > 0 else 'foo'"
        ```
    """
    return condition.ConditionBuilder(cond, self)

subscribe ¤

subscribe(*slices: Slice | IntoExpression) -> Subscript

Subscribe to the expression.

Parameters:

Name Type Description Default
slices Slice | IntoExpression

The slice expressions.

()

Examples:

1
2
3
subscript_expr = id_('a').expr().subscribe(id_('b').expr(), slice_(litint(2), litint(3)))
# also alias: ...[...]
assert subscript_expr.into_code() == "a[b, 2:3]"
Source code in synt/expr/expr.py
def subscribe(
    self, *slices: subscript.Slice | IntoExpression
) -> subscript.Subscript:
    """Subscribe to the expression.

    Args:
        slices: The slice expressions.

    Examples:
        ```python
        subscript_expr = id_('a').expr().subscribe(id_('b').expr(), slice_(litint(2), litint(3)))
        # also alias: ...[...]
        assert subscript_expr.into_code() == "a[b, 2:3]"
        ```
    """
    return subscript.Subscript(self, list(slices))

__getitem__ ¤

1
2
3
4
5
6
7
__getitem__(
    items: (
        tuple[Slice | IntoExpression, ...]
        | Slice
        | IntoExpression
    )
) -> Subscript

Alias subscribe.

Source code in synt/expr/expr.py
def __getitem__(
    self,
    items: tuple[subscript.Slice | IntoExpression, ...]
    | subscript.Slice
    | IntoExpression,
) -> subscript.Subscript:
    """Alias [`subscribe`][synt.expr.expr.Expression.subscribe]."""
    if isinstance(items, tuple):
        return self.subscribe(*items)
    else:
        return self.subscribe(items)

wrap ¤

wrap() -> Wrapped

Wrap self in a pair of parentheses, alias Wrapped.

Source code in synt/expr/expr.py
def wrap(self) -> expr_wrapped.Wrapped:
    """Wrap `self` in a pair of parentheses, alias [`Wrapped`][synt.expr.wrapped.Wrapped]."""
    return expr_wrapped.Wrapped(self)

wrapped ¤

wrapped() -> Wrapped

Alias wrap.

Source code in synt/expr/expr.py
def wrapped(self) -> expr_wrapped.Wrapped:
    """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
    return self.wrap()

par ¤

par() -> Wrapped

Alias wrap.

Source code in synt/expr/expr.py
def par(self) -> expr_wrapped.Wrapped:
    """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
    return self.wrap()

atom ¤

atom() -> Wrapped

Alias wrap.

Source code in synt/expr/expr.py
def atom(self) -> expr_wrapped.Wrapped:
    """Alias [`wrap`][synt.expr.expr.Expression.wrap]."""
    return self.wrap()

assign ¤

assign(value: IntoExpression) -> Assignment

Create a new assignment statement, take self as the target.

Parameters:

Name Type Description Default
value IntoExpression

The value to assign.

required
Source code in synt/expr/expr.py
def assign(self, value: IntoExpression) -> stmt_assign.Assignment:
    """Create a new assignment statement, take `self` as the target.

    Args:
        value: The value to assign.
    """
    return stmt_assign.Assignment(self).assign(value)

assign_to ¤

assign_to(target: IntoExpression) -> Assignment

Create a new assignment statement, take self as the value.

Parameters:

Name Type Description Default
target IntoExpression

The value to be assigned.

required
Source code in synt/expr/expr.py
def assign_to(self, target: IntoExpression) -> stmt_assign.Assignment:
    """Create a new assignment statement, take `self` as the value.

    Args:
        target: The value to be assigned.
    """
    return stmt_assign.Assignment(target.into_expression()).assign(self)

type ¤

Create a new typed assignment statement, take self as the target.

Parameters:

Name Type Description Default
ty IntoExpression

The type of the target.

required
Source code in synt/expr/expr.py
def type(self, ty: IntoExpression) -> stmt_assign.Assignment:
    """Create a new typed assignment statement, take `self` as the target.

    Args:
        ty: The type of the target.
    """
    return stmt_assign.Assignment(self).type(ty)

ty ¤

Alias type.

Source code in synt/expr/expr.py
def ty(self, ty: IntoExpression) -> stmt_assign.Assignment:
    """Alias [`type`][synt.expr.expr.Expression.type]."""
    return self.type(ty)

stmt ¤

stmt() -> Statement

Convert the expression into a statement.

Source code in synt/expr/expr.py
def stmt(self) -> Statement:
    """Convert the expression into a statement."""
    from synt.stmt.expression import stmt

    return stmt(self)

as_ ¤

as_(target: Identifier) -> Alias

Convert the expression into an alias.

Parameters:

Name Type Description Default
target Identifier

The target identifier.

required
Source code in synt/expr/expr.py
def as_(self, target: Identifier) -> Alias:
    """Convert the expression into an alias.

    Args:
        target: The target identifier.
    """
    from synt.expr.alias import Alias

    return Alias(self, target)