How to represent the different kinds of Python expressions as Expression Trees
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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)
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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])
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
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.
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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.
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.
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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.
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
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
Given the following snippet of code:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Rationale: The type of types can be somewhat difficult to parse and recursive.
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Given the following snippet of code:
From this context, we know that:
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.
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
Given the following snippet of code:
From this context, we know that:
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.
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.
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.
Given the following snippet of code:
From this context, we know that:
The Expression Tree for the highlighted expression is:
Rationale: The yield from expression evaluates to the value returned by its iterable.
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.
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.