Expression Reference

How to represent the different kinds of Python expressions as Expression Trees

Excavator operator at work.

Operations

Boolean Operation - and

An 'and' boolean operation
Specification
OperationsBoolean

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Boolean Operation - or

An 'or' boolean operation
Specification
OperationsBoolean

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Boolean Operation - sequence of and

A sequence of 'and' boolean operations
Specification
OperationsBoolean

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The Python AST represents a sequence of boolops as a single node, but to better represent the structure of the code and the grammar of the language, we represent long boolops as composed binary operations

Boolean Operation - mixing and & or

A boolean operations mixing 'and', 'or' in its expression. Operator 'and' has the precedence
Specification
OperationsAssociativityBoolean

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Named Expression/Walrus Operator

An assignment expression, also known as the Walrus Operator, from PEP 572.
Specification
Operationsassignment

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - sum

A binary operation performing the sum between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - difference

A binary operation performing the difference between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - multiplication

A binary operation performing the multiplication between 1 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - division

A binary operation performing the division between 2 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - floor division

A binary operation performing floor division between 4 and 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - modulo

A binary operation performing 10 modulo 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - exponentiation

A binary operation performing 2 to the power of 10
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: Exponentiation can result in either a float (as in 1.5**2) or an int (as in 2**2 or 2e90**2)

Binary Operation - bitwise shift left

A binary operation performing 1 bit shift to left on number 2
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise shift right

A binary operation performing 1 bit shift to right on number 4
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise or

A binary operation performing a bitwise 'or' between 6 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise xor

A binary operation performing the bitwise exclusive or between 1 and 1
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - bitwise and

A binary operation performing the bitwise and between 15 and 15
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Binary Operation - 2 consecutive operations

2 consecutive sum binary operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - unary positive

A unary plus operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - unary negation

A negative unary operation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Unary Operation - bitwise negation

A bitwise negation
Specification
Operations

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Lambda

Lambda - one parameter

Lambda, an anonymous function containing only one parameter and one expression
Specification
function

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Lambda - no parameters

Lambda, an anonymous function containing only one expression and no parameters
Specification
function

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Lambda - full parameters kind

A Lambda function containing every single type of possible parameter
Specification
function

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Conditionals

If Expression

If expression with 1 as the result if the condition is True, else 0
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Collections

List - one element

List expression with only one single element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List - no elements

List expression containing no elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List - multiple elements

List expression containing multiple elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List - unpacking

List expression containing multiple elements, including a starred element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The unpacking is represented in the AST as a Starred node, containing the expression to be unpacked, but following the principle that subexpressions should be extractable as an expression in their own right, we chose not to represent the Starred node as a node in the expression tree, as you can't, for example, write an assignment with a Starred node on its right hand side (x = *[1, 2])

Tuple - one element

Tuple expression with only one single element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - no elements

Tuple expression containing no elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - multiple elements

Tuple expression containing multiple elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Tuple - unpacking

Tuple expression containing multiple elements, including a Starred element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - one element

Dictionary expression with only one single element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - no elements

Dictionary expression with no elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary - unpacking

Dictionary expression with only one keyword argument
Specification
sequence

Given the following snippet of code:

From this context, we know that:

  • myotherdict is of type Any

The Expression Tree for the highlighted expression is:

Dictionary - multiple arguments

Dictionary expression with multiple arguments and kwargs
Specification
sequence

Given the following snippet of code:

From this context, we know that:

  • myotherdict2 is of type Any
  • myotherdict1 is of type Any

The Expression Tree for the highlighted expression is:

Set - one element

Set expression with only one single element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set - multiple elements

Set expression containing multiple elements
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set - unpacking

Set expression containing multiple elements, including a starred element
Specification
sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Comprehension

List Comprehension - one generator

A list comprehension with only one generator
Specification
sequencerepetition

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

List Comprehension - generators and filters

A list comprehension with 2 generators and filters
Specification
sequencerepetition

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Set Comprehension - one generator

A set comprehension with only one generator
Specification
sequencerepetition

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Dictionary Comprehension - one generator

A dictionary comprehension giving for each number in numbers the number as key and its power of 2 as value
Specification
sequencerepetition

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Generator Expression - one generator

A generator expression with only one generator
Specification
sequencerepetition

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Keyword

Await expression

Await expression returning number 404
Specification

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Yield expression

Yield expression returning number 404
Specification

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Yield From expression

Yield From expression yielding from variable mylist
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

Comparison

Compare - equality

Compare expression using equality operator
Specification
compare

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - difference

Compare expression using difference comparison operator
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - less than

Compare expression using less than comparison operator
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - less than or equal to

Compare expression using less than or equal to comparison operator
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - greater than

Compare expression using greater than comparison operator
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - greater than or equal to

Compare expression using greater than or equal to comparison operator
Specification
conditional

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Compare - identity equality operator

Compare expression using the identity equality operator stating if the objects compared are the same
Specification
conditional

Given the following snippet of code:

From this context, we know that:

  • obj2 is of type Any
  • obj1 is of type Any

The Expression Tree for the highlighted expression is:

Compare - identity difference operator

Compare expression using the identity difference operator stating if the objects compared are not the same
Specification
conditional

Given the following snippet of code:

From this context, we know that:

  • obj2 is of type Any
  • obj1 is of type Any

The Expression Tree for the highlighted expression is:

Compare - 'in' membership operator

Compare expression using the 'in' membership operator to test if a sequence is present in an object
Specification
conditional

Given the following snippet of code:

From this context, we know that:

  • numbers is of type Any

The Expression Tree for the highlighted expression is:

Compare expression using the 'not in' membership operator to test if a sequence is not present in an object

Compare expression
Specification
conditional

Given the following snippet of code:

From this context, we know that:

  • numbers is of type Any

The Expression Tree for the highlighted expression is:

Functions

Call expression

Function call expression with every possible type of parameter
Specification
function

Given the following snippet of code:

From this context, we know that:

  • j is of type Any
  • h is of type Any
  • g is of type Any
  • f is of type Any
  • e is of type Any
  • d is of type Any
  • c is of type Any
  • z is of type Any
  • y is of type Any
  • x is of type Any
  • func is of type Any
  • m is of type Any
  • l is of type Any

The Expression Tree for the highlighted expression is:

Rationale: Order of parameters it not perfectly preserved in the AST: starargs which are placed after a keyword arg cannot be distinguished from starargs placed immediately before the first keyword arg. Hence starargs after keyword args are reordered to before the keyword args. This is the most common usage pattern and reflects the method definition too.

Constant

Integer constant expression

Constant literal expression of a integer number
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

String constant expression

Constant literal expression of a string
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Concatenated string constant expression

Constant literal expression of a concatenated string
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: Python concatenates adjacent string literals in source code. This concatenation happens while parsing the source code, before compilation and execution, so we represent it as such too.

Concatenated f-string constant expression

Constant literal expression of a concatenated string
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: In violation of PEP 498, which specifies that f-strings should be concatenated at runtime, the parser concatenates f-strings too, though it enforces the requirement that a FormattedValue must be fully contained in a single string and may not cross a concatenated string boundary.

Constant expression

Constant literal expression of a float
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant literal expression of a boolean
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant literal expression of a NoneType
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant literal expression of an ellipsis
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Constant literal expression of an ellipsis
Specification
literal

Given the following snippet of code:

From this context, we know that:

  • Ellipsis is of type ellipsis

The Expression Tree for the highlighted expression is:

Attribute

Attribute

An object attribute expression
Specification
field

Given the following snippet of code:

From this context, we know that:

  • snake is of type Any

The Expression Tree for the highlighted expression is:

Subscript

Subscript expression

A subscript expression of a sequence with the zero position being accessed
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

Sliced Subscription

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

A subscript giving a slice of the sequence
Specification

Given the following snippet of code:

From this context, we know that:

  • mylist is of type Any

The Expression Tree for the highlighted expression is:

Name

Name expression

A variable name being accessed
Specification
namevariable

Given the following snippet of code:

From this context, we know that:

  • myvar is of type Any

The Expression Tree for the highlighted expression is:

f-string

Formatted String - no formatted value

A simple formatted string used without inner formatted values
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Formatted String - single formatted value

A formatted string with an inner formatted value
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Formatted String - single formatted value with format string

A formatted string with an inner formatted value
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The format string is a JoinedStr contained in the FormattedValue node, and as such could be considered a subexpression, however, it is quite different from other JoinedStrs, so we decided to represent it as part of the FormattedValue. Unlike regular f-strings it is not wrapped in quotes, it can contain only interpolations with a depth of one, and it is different in content as it may not be an arbitrary string but rather follows the format mini-language.

Formatted String - single formatted value with nested format string

A formatted string with an inner formatted value
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Formatted String - single formatted value with = formatting

A formatted string with an inner formatted value
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The formatting of an =-terminated formatted value is performed while parsing

Formatted String - single formatted value with conversion specifier

A formatted string with an inner formatted value
Specification
string

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Odd Types

Type of tuple

A reference to the `tuple` type/constructor
Specification
type

Given the following snippet of code:

From this context, we know that:

  • tuple is of type Type[Tuple[Any, ...]]

The Expression Tree for the highlighted expression is:

Rationale: The type of types can be somewhat difficult to parse and recursive.

Type of a class name

A reference to a user-defined class
Specification
class

Given the following snippet of code:

From this context, we know that:

  • C is of type Type[C]

The Expression Tree for the highlighted expression is:

Type of a class name - nonglobal

A reference to a user-defined class, declared in a function body
Specification
class

Given the following snippet of code:

From this context, we know that:

  • C is of type Type[Any]

The Expression Tree for the highlighted expression is:

Rationale: For unknown reasons, pytype assigns the wrong type to class definitions not in the global scope.

Type of None

A reference to the None singleton
Specification
literal

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: Formally, None has the type NoneType, however, None is accepted as a type hint for variables containing None

The NoReturn type

A function with the NoReturn return type
Specification
type

Given the following snippet of code:

From this context, we know that:

  • NoReturn is of type nothing

The Expression Tree for the highlighted expression is:

Rationale: The NoReturn type is a special type which contains no values and is used to label functions that are not expected to return at all (i.e. they run forever or terminate exceptionally). Pytype, however, incorrectly labels NoReturn with the type nothing.

Type of yield

A yield expression
Specification
type

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The type of an arbitrary yield expression is Any because the value it evaluates to depends on the caller.

A yield expression in a type-hinted function
Specification

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: The type of a yield expression in a function that is type hinted to return a Generator is determined by the type hint.

Type of yield from

A yield from expression with a generator
Specification

Given the following snippet of code:

From this context, we know that:

  • bar is of type Callable[[], Any]

The Expression Tree for the highlighted expression is:

Rationale: The yield from expression evaluates to the value returned by its iterable.

A yield from expression with a list
Specification

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: Yielding from a list evaluates to None, but pytype incorrectly assigns it listiterator instead.

The 'nothing' type

A list containing nothing
Specification
Sequence

Given the following snippet of code:

The Expression Tree for the highlighted expression is:

Rationale: Pytype uses the 'nothing' type to represent the "empty" type which contains no values. This type is not provided by the standard library, nor indeed by any publicly available package, but made up by pytype, so it cannot be used in type hints except when using pytype.