API Reference¶
Public Interface¶
-
walrus.convert(code, filename=None, *, source_version=None, linesep=None, indentation=None, pep8=None)[source]¶ Convert the given Python source code string.
- Parameters
- Keyword Arguments
source_version (Optional[str]) – parse the code as this Python version (uses the latest version by default)
linesep (Optional[
Linesep]) – line separator of code (LF,CRLF,CR) (auto detect by default)indentation (Optional[Union[int, str]]) – code indentation style, specify an integer for the number of spaces, or
't'/'tab'for tabs (auto detect by default)pep8 (Optional[bool]) – whether to make code insertion PEP 8 compliant
- Environment Variables
WALRUS_SOURCE_VERSION– same as thesource_versionargument and the--source-versionoptionin CLI
WALRUS_LINESEP– same as the linesep argument and the--linesepoption in CLIWALRUS_INDENTATION– same as theindentationargument and the--indentationoption in CLIWALRUS_PEP8– same as thepep8argument and the--no-pep8option in CLI (logical negation)
- Returns
converted source code
- Return type
-
walrus.walrus(filename, *, source_version=None, linesep=None, indentation=None, pep8=None, quiet=None, dry_run=False)[source]¶ Convert the given Python source code file. The file will be overwritten.
- Parameters
filename (str) – the file to convert
- Keyword Arguments
source_version (Optional[str]) – parse the code as this Python version (uses the latest version by default)
linesep (Optional[
Linesep]) – line separator of code (LF,CRLF,CR) (auto detect by default)indentation (Optional[Union[int, str]]) – code indentation style, specify an integer for the number of spaces, or
't'/'tab'for tabs (auto detect by default)pep8 (Optional[bool]) – whether to make code insertion PEP 8 compliant
quiet (Optional[bool]) – whether to run in quiet mode
dry_run (bool) – if
True, only print the name of the file to convert but do not perform any conversion
- Environment Variables
WALRUS_SOURCE_VERSION– same as thesource-versionargument and the--source-versionoptionin CLI
WALRUS_LINESEP– same as thelinesepargument and the--linesepoption in CLIWALRUS_INDENTATION– same as theindentationargument and the--indentationoption in CLIWALRUS_PEP8– same as thepep8argument and the--no-pep8option in CLI (logical negation)WALRUS_QUIET– same as thequietargument and the--quietoption in CLI
- Return type
-
walrus.main(argv=None)[source]¶ Entry point for walrus.
- Parameters
argv (Optional[List[str]]) – CLI arguments
- Returns
program exit code
- Return type
- Environment Variables
WALRUS_QUIET– same as the--quietoption in CLIWALRUS_CONCURRENCY– same as the--concurrencyoption in CLIWALRUS_DO_ARCHIVE– same as the--no-archiveoption in CLI (logical negation)WALRUS_ARCHIVE_PATH– same as the--archive-pathoption in CLIWALRUS_SOURCE_VERSION– same as the--source-versionoption in CLIWALRUS_LINESEP– same as the--linesepoption in CLIWALRUS_INDENTATION– same as the--indentationoption in CLIWALRUS_PEP8– same as the--no-pep8option in CLI (logical negation)
Conversion Implementation¶
The main logic of the walrus conversion is to wrap assignment expressions
as functions which manipulates variable namespaces to implement the assignment
part and evaluates original code blocks to archive the expression part.
For conversion algorithms and details, please refer to Algorithms.
Data Structures¶
During conversion, we utilised bpc_utils.Config to store and deliver the
configurations over the conversion Context instances, which should
be as following:
-
class
walrus.Config[source]¶ Configuration object shared over the conversion process of a single source file.
-
linesep: Literal['\n', '\r\n', '\r']¶ Line separator.
-
Since conversion of assignment expressions in different statements has different processing logics and templates, we hereby describe two data structures representing such information.
The FunctionEntry represents an assignment expression at most circumstances. It
will be provided to FUNC_TEMPLATE to render the wrapper function for
the conversion.
-
class
walrus.FunctionEntry¶ - Bases
-
name: str¶ Function name, as the original left-hand-side variable name from the assignment expression.
-
scope_keyword: Literal['global', 'nonlocal']¶ Scope manipulation keyword. If
nameis declared in global namespace, then it will be'global', else'nonlocal'.Note
If the left-hand-side variable name is recorded in the global context (
Context.global_stmt), then the converted function should use'global'as its scope keyword.
The LambdaEntry represents an assignment expression declared in lambda
statements. It will be provided to LAMBDA_FUNC_TEMPLATE to render
the wrapper function for the conversion.
Conversion Templates¶
For general conversion scenarios, the converted wrapper functions will be rendered based on the following templates.
-
walrus.NAME_TEMPLATE: List[str]¶ ['if False:', '%(indentation)s%(name_list)s = NotImplemented']
Declares variables in the current scope for using
globaland/ornonlocalstatements.- Variables
indentation – indentation sequence as defined in
Config.indentationname_list – equal (
=) separated list of variable names
Important
This is a rather hack way to fool the Python interpreter that such variables had been declared in the current scope, whilst not actually declaring such variables at runtime.
-
walrus.CALL_TEMPLATE: str¶ '_walrus_wrapper_%(name)s_%(uuid)s(%(expr)s)'
Wrapper function call to replace the original assignment expression.
- Variables
name – left-hand-side variable name
uuid – UUID text
expr – right-hand-side expression
-
walrus.FUNC_TEMPLATE: List[str]¶ ['def _walrus_wrapper_%(name)s_%(uuid)s(expr):', '%(indentation)s"""Wrapper function for assignment expression."""', '%(indentation)s%(scope_keyword)s %(name)s', '%(indentation)s%(name)s = expr', '%(indentation)sreturn %(name)s']
Wrapper function call to replace the original assignment expression.
- Variables
indentation – indentation sequence as defined in
Config.indentation**kwargs – function record as described in
Function
For assignment expression in lambda expressions, the converted wrapper function will be rendered based on the following templates.
-
walrus.LAMBDA_CALL_TEMPLATE: str¶ '_walrus_wrapper_lambda_%(uuid)s'
Wrapper function call to replace the original assignment expression.
- Variables
uuid – UUID text
-
walrus.LAMBDA_FUNC_TEMPLATE: List[str]¶ ['def _walrus_wrapper_lambda_%(uuid)s(%(param)s):', '%(indentation)s"""Wrapper function for lambda definitions."""', '%(indentation)s%(suite)s']
Wrapper function call to replace the original assignment expression.
- Variables
indentation – indentation sequence as defined in
Config.indentation**kwargs – function record as described in
LambdaEntry
For assignment expression in class variables
(ClassVar), the converted wrapper function will be rendered based on
the following templates.
Conversion Contexts¶
-
class
walrus.Context(node, config, *, indent_level=0, scope_keyword=None, context=None, raw=False)[source]¶ Bases:
bpc_utils.context.BaseContextGeneral conversion context.
- Parameters
node (parso.tree.NodeOrLeaf) – parso AST
config (Config) – conversion configurations
- Keyword Arguments
Important
rawshould beTrueonly if thenodeis in the clause of another context, where the converted wrapper functions should be inserted.Typically, only if
nodeis an assignment expression (namedexpr_test) node,rawwill be set asTrue, in consideration of nesting assignment expressions.For the
Contextclass ofwalrusmodule, it will process nodes with following methods:namedexpr_testlambdefargument-
ClassContext._process_strings()
-
ClassContext._process_fstring()
Initialize BaseContext.
- Parameters
node (
NodeOrLeaf) – parso ASTconfig (
Config) – conversion configurationsindent_level (
int) – current indentation levelraw (
bool) – raw processing flag
-
_concat()[source]¶ Concatenate final string.
This method tries to inserted the recorded wrapper functions and variables at the very location where starts to contain assignment expressions, i.e. between the converted code as
self._prefixandself._suffix.The inserted code include variable declaration rendered from
NAME_TEMPLATE, wrapper function definitions rendered fromFUNC_TEMPLATEand extracted lambda expressions rendered fromLAMBDA_FUNC_TEMPLATE. Ifself._pep8isTrue, it will insert the code in compliance with PEP 8.- Return type
-
_process_argument(node)[source]¶ Process function argument (
argument).- Parameters
node (parso.python.tree.PythonNode) – argument node
This method processes arguments from function argument list.
- Return type
-
_process_classdef(node)[source]¶ Process class definition (
classdef).- Parameters
node (parso.python.tree.Class) – class node
This method converts the whole class suite context with
_process_suite_node()throughClassContextrespectively.- Return type
-
_process_for_stmt(node)[source]¶ Process for statement (
for_stmt).- Parameters
node (parso.python.tree.ForStmt) – for node
This method processes the indented suite under the for and optional else statements.
- Return type
-
_process_fstring(node)[source]¶ Process formatted strings (
f_string).- Parameters
node (parso.python.tree.PythonNode) – formatted strings node
- Return type
-
_process_funcdef(node)[source]¶ Process function definition (
funcdef).- Parameters
node (parso.python.tree.Function) – function node
This method converts the function suite with
_process_suite_node().- Return type
-
_process_global_stmt(node)[source]¶ Process function definition (
global_stmt).- Parameters
node (parso.python.tree.GlobalStmt) – global statement node
This method records all variables declared in a global statement into
self._context.- Return type
-
_process_if_stmt(node)[source]¶ Process if statement (
if_stmt).- Parameters
node (parso.python.tree.IfStmt) – if node
This method processes each indented suite under the if, elif, and else statements.
- Return type
-
_process_lambdef(node)[source]¶ Process lambda definition (
lambdef).- Parameters
node (parso.python.tree.Lambda) – lambda node
This method first checks if
nodecontains assignment expressions. If not, it will append the original source code directly to the buffer.For lambda expressions with assignment expressions, this method will extract the parameter list and initialise a
LambdaContextinstance to convert the lambda suite. Such information will be recorded asLambdaEntryinself._lamb.Note
For
LambdaContext,scope_keywordshould always be'nonlocal'.Then it will replace the original lambda expression with a wrapper function call rendered from
LAMBDA_CALL_TEMPLATE.- Return type
-
_process_namedexpr_test(node)[source]¶ Process assignment expression (
namedexpr_test).- Parameters
node (parso.python.tree.PythonNode) – assignment expression node
This method converts the assignment expression into wrapper function and extracts related records for inserting converted code.
The left-hand-side variable name will be recorded in
self._vars.The right-hand-side expression will be converted using another
Contextinstance and replaced with a wrapper function call rendered fromCALL_TEMPLATE; information described asFunctionEntrywill be recorded intoself._func.
- Return type
-
_process_string_context(node)[source]¶ Process string contexts (
stringliteral).- Parameters
node (parso.python.tree.PythonNode) – string literals node
This method first checks if
nodecontains assignment expression. If not, it will not perform any processing, rather just append the original source code to context buffer. Later it will check ifnodecontains debug f-string. If not, it will process the regular processing on each child of suchnode.See also
The method calls
f2format.Context.has_debug_fstring()to detect debug f-strings.Otherwise, it will initialise a new
StringContextinstance to perform the conversion process on suchnode, which will first usef2formatto convert those formatted string literals.Important
When initialisation,
rawparameter must be set toTrue; as the converted wrapper functions should be inserted in the outer context, rather than the newStringContextinstance.After conversion, the method will keep records of converted wrapper functions (
Context.functions()), converted lambda expressions (Context.lambdef()) and left-hand-side variable names (Context.variables()) into current instance as well.- Return type
-
_process_strings(node)[source]¶ Process concatenable strings (
stringliteral).- Parameters
node (parso.python.tree.PythonNode) – concatentable strings node
As in Python, adjacent string literals can be concatenated in certain cases, as described in the `documentation`_. Such concatenable strings may contain formatted string literals (f-string) within its scope.
_documentation: https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation
- Return type
-
_process_suite_node(node, func=False, raw=False, cls_ctx=None)[source]¶ Process indented suite (
suiteor others).- Parameters
node (parso.tree.NodeOrLeaf) – suite node
func (bool) – if
nodeis suite from function definitionraw (bool) – raw processing flag
cls_ctx (Optional[str]) – class name if
nodeis in class context
This method first checks if
nodecontains assignment expression. If not, it will not perform any processing, rather just append the original source code to context buffer.If
nodecontains assignment expression, then it will initialise anotherContextinstance to perform the conversion process on suchnode; whilst ifcls_ctxis provided, then it will initialise aClassContextinstance instead.Note
If
funcis True, when initiating theContextinstance,scope_keywordwill be set as'nonlocal', as in the wrapper function it will refer the original left-hand-side variable from the outer function scope rather than global namespace.The method will keep global statements (
Context.global_stmt()) from the temporaryContext(orClassContext) instance in the current instance.And if
rawis set asTrue, the method will keep records of converted wrapper functions (Context.functions()), converted lambda expressions (Context.lambdef()) and left-hand-side variable names (Context.variables()) into current instance as well.Important
rawshould beTrueonly if thenodeis in the clause of another context, where the converted wrapper functions should be inserted.- Return type
-
_process_try_stmt(node)[source]¶ Process try statement (
try_stmt).- Parameters
node (parso.python.tree.TryStmt) – try node
This method processes the indented suite under the try, except, else, and finally statements.
- Return type
-
_process_while_stmt(node)[source]¶ Process while statement (
while_stmt).- Parameters
node (parso.python.tree.WhileStmt) – while node
This method processes the indented suite under the while and optional else statements.
- Return type
-
_process_with_stmt(node)[source]¶ Process with statement (
with_stmt).- Parameters
node (parso.python.tree.WithStmt) – with node
This method processes the indented suite under the with statement.
- Return type
-
classmethod
determine_scope_keyword(node)[source]¶ Determine scope keyword based on node position.
- Parameters
node (parso.tree.NodeOrLeaf) – parso AST
- Returns
scope keyword
- Return type
Literal[‘global’, ‘nonlocal’]
This method recursively perform the following checks on the parents of
node:If current
nodeis a module (parso.python.tree.Module), or a direct child of module then returns'global'.If the direct parrent of current
nodeis a function (parso.python.tree.Function) and/or class (parso.python.tree.Class) definition, then returns'nonlocal'.
-
classmethod
has_expr(node)[source]¶ Check if node has assignment expression (
namedexpr_test).- Parameters
node (parso.tree.NodeOrLeaf) – parso AST
- Returns
if
nodehas assignment expression- Return type
-
classmethod
has_walrus(node)¶ Check if node has assignment expression (
namedexpr_test).- Parameters
node (parso.tree.NodeOrLeaf) – parso AST
- Returns
if
nodehas assignment expression- Return type
-
static
is_walrus(node)[source]¶ Check if
nodeis assignment expression.- Parameters
node (parso.tree.NodeOrLeaf) – parso AST
- Returns
if
nodeis assignment expression- Return type
-
_abc_impl= <_abc_data object>¶
-
_func: List[FunctionEntry]¶ Converted wrapper functions described as
FunctionEntry.- Type
List[FunctionEntry]
-
_lamb: List[LambdaEntry]¶ Converted lambda expressions described as
LambdaEntry.- Type
List[LambdaEntry]
-
property
functions¶ Assignment expression wrapper function records (
self._func).- Return type
List[FunctionEntry]
-
property
global_stmt¶ List of variables declared in the
globalstatements.If current root node (
self._root) is a function definition (parso.python.tree.Function), then returns an empty list; else returnsself._context.- Return type
List[str]
-
property
lambdef¶ Lambda definitions (
self._lamb).- Return type
List[LambdaEntry]
-
property
variables¶ Assignment expression variable records (
self._vars).The variables are the left-hand-side variable name of the assignment expressions.
- Return type
List[str]
-
class
walrus.StringContext(node, config, *, indent_level=0, scope_keyword=None, context=None, raw=True)[source]¶ Bases:
walrus.ContextString (f-string) conversion context.
This class is mainly used for converting formatted string literals.
- Parameters
node (parso.python.tree.PythonNode) – parso AST
config (Config) – conversion configurations
- Keyword Arguments
Note
rawshould always beTrue.
As the conversion in
Contextchanges the original expression, which may change the content of debug f-string.Initialize BaseContext.
- Parameters
node (
PythonNode) – parso ASTconfig (
Config) – conversion configurationsindent_level (
int) – current indentation levelraw (
Literal[True]) – raw processing flag
-
_abc_impl= <_abc_data object>¶
-
_func: List[FunctionEntry]¶ Converted wrapper functions described as
FunctionEntry.- Type
List[FunctionEntry]
-
_lamb: List[LambdaEntry]¶ Converted lambda expressions described as
LambdaEntry.- Type
List[LambdaEntry]
-
_linesep: Final[Linesep]¶ Line seperator.
- Type
Final[
Linesep]
-
_node_before_expr: Optional[parso.tree.NodeOrLeaf]¶ Preceding node with the target expression, i.e. the insertion point.
-
_prefix_or_suffix: bool¶ Flag to indicate whether buffer is now
self._prefix.
-
_root: Final[parso.tree.NodeOrLeaf]¶ Root node given by the
nodeparameter.
-
_uuid_gen: Final[UUID4Generator]¶ UUID generator.
-
class
walrus.LambdaContext(node, config, *, indent_level=0, scope_keyword=None, context=None, raw=False)[source]¶ Bases:
walrus.ContextLambda (suite) conversion context.
This class is mainly used for converting lambda expressions.
- Parameters
node (parso.python.tree.Lambda) – parso AST
config (Config) – conversion configurations
- Keyword Arguments
Note
scope_keywordshould always be'nonlocal'.rawshould always beFalse.
Initialize BaseContext.
- Parameters
node (
NodeOrLeaf) – parso ASTconfig (
Config) – conversion configurationsindent_level (
int) – current indentation levelraw (
Literal[False]) – raw processing flag
-
_concat()[source]¶ Concatenate final string.
Since conversion of lambda expressions doesn’t involve inserting points, this method first simply adds wrapper code to the buffer (
self._buffer); then it adds a return statement yielding the converted lambda suite stored inself._prefixandself._suffix.The wrapper code include variable declaration rendered from
NAME_TEMPLATE, wrapper function definitions rendered fromFUNC_TEMPLATEand extracted lambda expressions rendered fromLAMBDA_FUNC_TEMPLATE. Ifself._pep8isTrue, it will insert the code in compliance with PEP 8.- Return type
-
_abc_impl= <_abc_data object>¶
-
_func: List[FunctionEntry]¶ Converted wrapper functions described as
FunctionEntry.- Type
List[FunctionEntry]
-
_lamb: List[LambdaEntry]¶ Converted lambda expressions described as
LambdaEntry.- Type
List[LambdaEntry]
-
_linesep: Final[Linesep]¶ Line seperator.
- Type
Final[
Linesep]
-
_node_before_expr: Optional[parso.tree.NodeOrLeaf]¶ Preceding node with the target expression, i.e. the insertion point.
-
_prefix_or_suffix: bool¶ Flag to indicate whether buffer is now
self._prefix.
-
_root: Final[parso.tree.NodeOrLeaf]¶ Root node given by the
nodeparameter.
-
_uuid_gen: Final[UUID4Generator]¶ UUID generator.
-
class
walrus.ClassContext(node, config, *, cls_ctx, cls_var=None, indent_level=0, scope_keyword=None, context=None, raw=False, external=None)[source]¶ Bases:
walrus.ContextClass (suite) conversion context.
This class is mainly used for converting :term:`class variable <class-variable>`.
- Parameters
node (parso.python.tree.Class) – parso AST
config (Config) – conversion configurations
- Keyword Arguments
cls_ctx (str) – class context name
cls_var (Dict[str, str]) – mapping for assignment variable and its UUID
indent_level (int) – current indentation level
scope_keyword (Optional[Literal['global', 'nonlocal']]) – scope keyword for wrapper function
raw (Literal[False]) – raw context processing flag
external (Optional[Dict[str, Literal['global', 'nonlocal']]]) – mapping of class variable declared in
globaland/ornonlocalstatements
Important
rawshould beTrueonly if thenodeis in the clause of another context, where the converted wrapper functions should be inserted.Typically, only if
nodeis an assignment expression (namedexpr_test) node,rawwill be set asTrue, in consideration of nesting assignment expressions.Initialize BaseContext.
- Parameters
-
_concat()[source]¶ Concatenate final string.
This method tries to inserted the recorded wrapper functions and variables at the very location where starts to contain assignment expressions, i.e. between the converted code as
self._prefixandself._suffix.For special class variable declared in
globaland/ornonlocalstatements, when assigning to to such variables, i.e. they are on left-hand-side of assignment expressions, the expressions will be assigned with a wrapper function rendered fromFUNC_TEMPLATE.- Return type
-
_process_defined_name(node)[source]¶ Process defined name (
name).- Parameters
node (parso.python.tree.Name) – defined name node
This method processes name of defined class variable. The original variable name will be recorded in
self._vars; its corresponding UUID will be recorded inself._cls_var; information described asFunctionEntrywill be recorded intoself._func.Note
If the left-hand-side variable was declared in
globaland/ornonlocal, then it shall NOT be considered as class variable.- Return type
-
_process_expr_stmt(node)[source]¶ Process variable name (
expr_stmt).- Parameters
node (parso.python.tree.ExprStmt) – expression statement
This method processes expression statements in the class context. It will search for left-hand-side variable names of defined class variables and call
_process_defined_name()to process such nodes.- Return type
-
_process_global_stmt(node)[source]¶ Process function definition (
global_stmt).- Parameters
node (parso.python.tree.GlobalStmt) – global statement node
This method records all variables declared in a global statement into
self._contextandself._ext_vars.- Return type
-
_process_namedexpr_test(node)[source]¶ Process assignment expression (
namedexpr_test).- Parameters
node (parso.python.tree.PythonNode) – assignment expression node
This method converts the assignment expression into wrapper function and extracts related records for inserting converted code.
The left-hand-side variable name will be recorded in
self._vars; and its corresponding UUID will be recorded inself._cls_var.The right-hand-side expression will be converted using another
ClassContextinstance and replaced with a wrapper tuple with attribute setting fromCLS_TEMPLATE; information described asFunctionEntrywill be recorded intoself._func.
Important
ClassContextwill mangle left-hand-side variable name throughself.manglewhen converting.For special class variable declared in
globaland/ornonlocalstatements:The left-hand-side variable name will NOT be considered as class variable, thus shall NOT be recorded.
The expression will be replaced with a wrapper function call rendered from
CALL_TEMPLATE; information described asFunctionEntrywill be recorded intoself._ext_funcinstead.
- Return type
-
_process_nonlocal_stmt(node)[source]¶ Process function definition (
nonlocal_stmt).- Parameters
node (parso.python.tree.KeywordStatement) – nonlocal statement node
This method records all variables declared in a nonlocal statement into
self._ext_vars.- Return type
-
_process_string_context(node)[source]¶ Process string contexts (
stringliteral).- Parameters
node (parso.python.tree.PythonNode) – string literals node
This method first checks if
nodecontains assignment expression. If not, it will not perform any processing, rather just append the original source code to context buffer.If
nodecontains assignment expression, then it will initialise a newClassStringContextinstance to perform the conversion process on suchnode, which will first usef2formatto convert those formatted string literals.Important
When initialisation,
rawparameter must be set toTrue; as the converted wrapper functions should be inserted in the outer context, rather than the newClassStringContextinstance.After conversion, the method will keep records of converted wrapper functions (
Context.functions), converted lambda expressions (Context.lambdef) and left-hand-side variable names (Context.variables), class variable (ClassContext.cls_var), external variables (ClassContext.external_variables), wrapper functions for external variables (ClassContext.external_functions) into current instance as well.- Return type
-
_process_suite_node(node, func=False, raw=False, cls_ctx=None)[source]¶ Process indented suite (
suiteor others).- Parameters
node (parso.tree.NodeOrLeaf) – suite node
func (bool) – if
nodeis suite from function definitionraw (bool) – raw processing flag
cls_ctx (Optional[str]) – class name if
nodeis in class context
This method first checks if
nodecontains assignment expression. If not, it will not perform any processing, rather just append the original source code to context buffer.If
nodecontains assignment expression, then it will initialise anotherClassContextinstance to perform the conversion process on suchnode; whilst iffuncis provided, then it will initialise aContextinstance instead.Note
If
funcis True, when initiating theContextinstance,scope_keywordwill be set as'nonlocal', as in the wrapper function it will refer the original left-hand-side variable from the outer function scope rather than global namespace.The method will keep global statements (
Context.global_stmt()) from the temporaryContext(orClassContext) instance in the current instance.And if
rawis set asTrue, the method will keep records of converted wrapper functions (Context.functions()), converted lambda expressions (Context.lambdef()) and left-hand-side variable names (Context.variables()), class variable (ClassContext.cls_var()), external variables (ClassContext.external_variables()), wrapper functions for external variables (ClassContext.external_functions()) into current instance as well.Important
rawshould beTrueonly if thenodeis in the clause of another context, where the converted wrapper functions should be inserted.- Return type
-
_abc_impl= <_abc_data object>¶
-
_ext_func: List[FunctionEntry]¶ Converted wrapper functions for class variable declared in
globaland/ornonlocalstatements described asFunctionEntry.- Type
List[FunctionEntry]
-
_ext_vars: Dict[str, ScopeKeyword]¶ Original left-hand-side variable names in assignment expressions for class variable declared in
globaland/ornonlocalstatements.- Type
Dict[str, Literal[‘global’, ‘nonlocal’]]
-
_func: List[FunctionEntry]¶ Converted wrapper functions described as
FunctionEntry.- Type
List[FunctionEntry]
-
_lamb: List[LambdaEntry]¶ Converted lambda expressions described as
LambdaEntry.- Type
List[LambdaEntry]
-
_linesep: Final[Linesep]¶ Line seperator.
- Type
Final[
Linesep]
-
_node_before_expr: Optional[parso.tree.NodeOrLeaf]¶ Preceding node with the target expression, i.e. the insertion point.
-
_prefix_or_suffix: bool¶ Flag to indicate whether buffer is now
self._prefix.
-
_root: Final[parso.tree.NodeOrLeaf]¶ Root node given by the
nodeparameter.
-
_uuid_gen: Final[UUID4Generator]¶ UUID generator.
-
property
cls_var¶ Mapping for assignment variable and its UUID (
self._cls_var).
-
property
external_functions¶ Assignment expression wrapper function records (
self._ext_func) for class variable declared inglobaland/ornonlocalstatements.- Return type
List[FunctionEntry]
-
property
external_variables¶ Assignment expression variable records (
self._ext_vars).The variables are the left-hand-side variable name of the assignment expressions for class variable declared in
globaland/ornonlocalstatements.- Return type
Dict[str, Literal[‘global’, ‘nonlocal’]]
-
class
walrus.ClassStringContext(node, config, *, cls_ctx, cls_var=None, indent_level=0, scope_keyword=None, context=None, raw=True, external=None)[source]¶ Bases:
walrus.ClassContextString (f-string) conversion context.
This class is mainly used for converting formatted string literals inside a class suite (ClassVar).
- Parameters
node (parso.python.tree.PythonNode) – parso AST
config (Config) – conversion configurations
- Keyword Arguments
cls_ctx (str) – class context name
cls_var (Dict[str, str]) – mapping for assignment variable and its UUID
indent_level (int) – current indentation level
scope_keyword (Optional[Literal['global', 'nonlocal']]) – scope keyword for wrapper function
raw (Literal[True]) – raw context processing flag
external (Optional[Dict[str, Literal['global', 'nonlocal']]]) – mapping of class variable declared in
globaland/ornonlocalstatements
Note
rawshould always beTrue.As the conversion in
ClassContextintroduced quotes (') into the converted code, it may cause conflicts on the string parsing if the assignment expression was inside a formatted string literal.Therefore, we will use
f2formatahead to convert such formatted string literals into normalstr.format()calls then convert any assignment expressions it may contain.Initialize BaseContext.
- Parameters
node (
PythonNode) – parso ASTconfig (
Config) – conversion configurationsindent_level (
int) – current indentation levelraw (
Literal[True]) – raw processing flag
-
_abc_impl= <_abc_data object>¶
-
_ext_func: List[FunctionEntry]¶ Converted wrapper functions for class variable declared in
globaland/ornonlocalstatements described asFunctionEntry.- Type
List[FunctionEntry]
-
_ext_vars: Dict[str, ScopeKeyword]¶ Original left-hand-side variable names in assignment expressions for class variable declared in
globaland/ornonlocalstatements.- Type
Dict[str, Literal[‘global’, ‘nonlocal’]]
-
_func: List[FunctionEntry]¶ Converted wrapper functions described as
FunctionEntry.- Type
List[FunctionEntry]
-
_lamb: List[LambdaEntry]¶ Converted lambda expressions described as
LambdaEntry.- Type
List[LambdaEntry]
-
_linesep: Final[Linesep]¶ Line seperator.
- Type
Final[
Linesep]
-
_node_before_expr: Optional[parso.tree.NodeOrLeaf]¶ Preceding node with the target expression, i.e. the insertion point.
-
_prefix_or_suffix: bool¶ Flag to indicate whether buffer is now
self._prefix.
-
_root: Final[parso.tree.NodeOrLeaf]¶ Root node given by the
nodeparameter.
-
_uuid_gen: Final[UUID4Generator]¶ UUID generator.
Internal Auxiliaries¶
Options & Defaults¶
-
walrus.WALRUS_SOURCE_VERSIONS= ['3.8', '3.9', '3.10']¶ Get supported source versions.
Below are option getter utility functions. Option value precedence is:
explicit value (CLI/API arguments) > environment variable > default value
-
walrus._get_quiet_option(explicit=None)[source]¶ Get the value for the
quietoption.- Parameters
explicit (Optional[bool]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
quietoption- Return type
- Environment Variables
WALRUS_QUIET– the value in environment variable
See also
-
walrus._get_concurrency_option(explicit=None)[source]¶ Get the value for the
concurrencyoption.- Parameters
explicit (Optional[int]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
concurrencyoption;Nonemeans auto detection at runtime- Return type
Optional[int]
- Environment Variables
WALRUS_CONCURRENCY– the value in environment variable
See also
-
walrus._get_do_archive_option(explicit=None)[source]¶ Get the value for the
do_archiveoption.- Parameters
explicit (Optional[bool]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
do_archiveoption- Return type
- Environment Variables
WALRUS_DO_ARCHIVE– the value in environment variable
See also
-
walrus._get_archive_path_option(explicit=None)[source]¶ Get the value for the
archive_pathoption.- Parameters
explicit (Optional[str]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
archive_pathoption- Return type
- Environment Variables
WALRUS_ARCHIVE_PATH– the value in environment variable
See also
-
walrus._get_source_version_option(explicit=None)[source]¶ Get the value for the
source_versionoption.- Parameters
explicit (Optional[str]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
source_versionoption- Return type
- Environment Variables
WALRUS_SOURCE_VERSION– the value in environment variable
See also
-
walrus._get_linesep_option(explicit=None)[source]¶ Get the value for the
linesepoption.- Parameters
explicit (Optional[str]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
linesepoption;Nonemeans auto detection at runtime- Return type
Optional[Literal[‘\n’, ‘\r\n’, ‘\r’]]
- Environment Variables
WALRUS_LINESEP– the value in environment variable
See also
-
walrus._get_indentation_option(explicit=None)[source]¶ Get the value for the
indentationoption.- Parameters
explicit (Optional[Union[str, int]]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
indentationoption;Nonemeans auto detection at runtime- Return type
Optional[str]
- Environment Variables
WALRUS_INDENTATION– the value in environment variable
See also
-
walrus._get_pep8_option(explicit=None)[source]¶ Get the value for the
pep8option.- Parameters
explicit (Optional[bool]) – the value explicitly specified by user,
Noneif not specified- Returns
the value for the
pep8option- Return type
- Environment Variables
WALRUS_PEP8– the value in environment variable
See also
The following variables are used for fallback default values of options.
-
walrus._default_quiet= False¶ Default value for the
quietoption.
-
walrus._default_concurrency= None¶ Default value for the
concurrencyoption.
-
walrus._default_do_archive= True¶ Default value for the
do_archiveoption.
-
walrus._default_archive_path= 'archive'¶ Default value for the
archive_pathoption.
-
walrus._default_source_version= '3.10'¶ Default value for the
source_versionoption.
-
walrus._default_linesep= None¶ Default value for the
linesepoption.
-
walrus._default_indentation= None¶ Default value for the
indentationoption.
-
walrus._default_pep8= True¶ Default value for the
pep8option.
Important
For _default_concurrency, _default_linesep and _default_indentation,
None means auto detection during runtime.
CLI Utilities¶
The following variables are used for help messages in the argument parser.
-
walrus.__cwd__: str¶ Current working directory returned by
os.getcwd().
-
walrus.__walrus_quiet__: Literal['quiet mode', 'non-quiet mode']¶ Default value for the
--quietoption.See also
-
walrus.__walrus_concurrency__: Union[int, Literal['auto detect']]¶ Default value for the
--concurrencyoption.See also
-
walrus.__walrus_do_archive__: Literal['will do archive', 'will not do archive']¶ Default value for the
--no-archiveoption.See also
-
walrus.__walrus_linesep__: Literal['LF', 'CRLF', 'CR', 'auto detect']¶ Default value for the
--linesepoption.See also
-
walrus.__walrus_pep8__: Literal['will conform to PEP 8', 'will not conform to PEP 8']¶ Default value for the
--no-pep8option.See also