API Reference¶
Public Interface¶
- poseur.convert(code, filename=None, *, source_version=None, linesep=None, indentation=None, pep8=None, dismiss=None, decorator=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[str]) – 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:
POSEUR_SOURCE_VERSION– same as thesource_versionargument and the--source-versionoptionin CLI
POSEUR_LINESEP– same as the linesep argument and the--linesepoption in CLIPOSEUR_INDENTATION– same as theindentationargument and the--indentationoption in CLIPOSEUR_PEP8– same as thepep8argument and the--no-pep8option in CLI (logical negation)POSEUR_DISMISS– same as the--dismiss-runtimeoption in CLIPOSEUR_DECORATOR– same as the--decorator-nameoption in CLI
- Returns:
converted source code
- Return type:
- Raises:
ValueError – if
decoratoris not a valid identifier name or starts with double underscore
- poseur.poseur(filename, *, source_version=None, linesep=None, indentation=None, pep8=None, dismiss=None, decorator=None, quiet=None, dry_run=False)[source]¶
Convert the given Python source code file. The file will be overwritten.
- Parameters:
- Keyword Arguments:
source_version (Optional[str]) – parse the code as this Python version (uses the latest version by default)
linesep (Optional[str]) – 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:
POSEUR_SOURCE_VERSION– same as thesource-versionargument and the--source-versionoptionin CLI
POSEUR_LINESEP– same as thelinesepargument and the--linesepoption in CLIPOSEUR_INDENTATION– same as theindentationargument and the--indentationoption in CLIPOSEUR_PEP8– same as thepep8argument and the--no-pep8option in CLI (logical negation)POSEUR_QUIET– same as thequietargument and the--quietoption in CLIPOSEUR_DISMISS– same as the--dismiss-runtimeoption in CLIPOSEUR_DECORATOR– same as the--decorator-nameoption in CLI
- Return type:
- poseur.main(argv=None)[source]¶
Entry point for poseur.
- Parameters:
argv (Optional[List[str]]) – CLI arguments
- Environment Variables:
POSEUR_QUIET– same as the--quietoption in CLIPOSEUR_CONCURRENCY– same as the--concurrencyoption in CLIPOSEUR_DO_ARCHIVE– same as the--no-archiveoption in CLI (logical negation)POSEUR_ARCHIVE_PATH– same as the--archive-pathoption in CLIPOSEUR_SOURCE_VERSION– same as the--source-versionoption in CLIPOSEUR_LINESEP– same as the--linesepoption in CLIPOSEUR_INDENTATION– same as the--indentationoption in CLIPOSEUR_PEP8– same as the--no-pep8option in CLI (logical negation)POSEUR_DISMISS– same as the--dismiss-runtimeoption in CLIPOSEUR_DECORATOR– same as the--decorator-nameoption in CLI
- Return type:
Runtime Decorator¶
As you may wish to provide runtime positional-only parameter checks for
your own code, poseur exposed the decorator function for
developers to use by themselves.
- poseur.decorator(*poseur)¶
Positional-only parameters runtime checker.
- Parameters:
*poseur – Name list of positional-only parameters.
- Raises:
TypeError – If any position-only parameters were passed as keyword parameters.
The decorator function may decorate regular function and/or lambda function to provide runtime checks on the original positional-only parameters.
Conversion Implementation¶
The main logic of the poseur conversion is to extract all positional-only
parameters and add a :term:`decorator` for the function and/or lambda
definition to provide runtime checks with the extracted parameters.
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 poseur.Config[source]¶
Configuration object shared over the conversion process of a single source file.
- linesep: Literal[\'\\n\', \'\\r\\n\', \'\\r\']¶
Line separator.
- source_version: str | None¶
Parse the code as this Python version (uses the latest version by default).
Conversion Templates¶
For general conversion scenarios, the decorator function will be rendered based on the following templates.
- poseur.DECORATOR_TEMPLATE: List[str]¶
['def %(decorator)s(*poseur):', '%(indentation)s"""Positional-only parameters runtime checker.', '%(indentation)s', '%(indentation)s Args:', '%(indentation)s *poseur: Name list of positional-only parameters.', '%(indentation)s', '%(indentation)s Raises:', '%(indentation)s TypeError: If any position-only parameters were passed as', '%(indentation)s keyword parameters.', '%(indentation)s', '%(indentation)s The decorator function may decorate regular :term:`function` and/or', '%(indentation)s :term:`lambda` function to provide runtime checks on the original', '%(indentation)s positional-only parameters.', '%(indentation)s', '%(indentation)s"""', '%(indentation)simport functools', '%(indentation)sdef caller(func):', '%(indentation)s%(indentation)s@functools.wraps(func)', '%(indentation)s%(indentation)sdef wrapper(*args, **kwargs):', '%(indentation)s%(indentation)s%(indentation)sposeur_args = set(poseur).intersection(kwargs)', '%(indentation)s%(indentation)s%(indentation)sif poseur_args:', "%(indentation)s%(indentation)s%(indentation)s%(indentation)sraise TypeError('%%s() got some positional-only arguments passed as keyword arguments: %%r' %% (func.__name__, ', '.join(poseur_args)))", '%(indentation)s%(indentation)s%(indentation)sreturn func(*args, **kwargs)', '%(indentation)s%(indentation)sreturn wrapper', '%(indentation)sreturn caller']
Decorator function to provide runtime checks on the original positional-only parameters.
- Variables:
decorator – decorator function name as defined in
Config.decoratorindentation – indentation sequence as defined in
Config.indentation
Important
Actually, the
poseur.decorator()function is rendered and evaluated at runtime using this template.
Conversion Contexts¶
- class poseur.Context(node, config, *, clx_ctx=None, indent_level=0, raw=False)[source]¶
Bases:
BaseContextGeneral conversion context.
- Parameters:
node (parso.tree.NodeOrLeaf) – parso AST
config (Config) – conversion configurations
clx_ctx (str | None) –
indent_level (int) –
raw (bool) –
- Keyword Arguments:
Important
rawshould beTrueonly if thenodeis in the clause of another context, where the converted wrapper functions should be inserted.For the
Contextclass ofposeurmodule, it will process nodes with following methods:suitefuncdeflambdefasync_funcdefasync_stmtclassdefif_stmtwhile_stmtfor_stmtwith_stmttry_stmtstringliteralf_string
Initialize BaseContext.
- Parameters:
node (
NodeOrLeaf) – parso ASTconfig (
Config) – conversion configurationsindent_level (
int) – current indentation levelraw (
bool) – raw processing flagclx_ctx (str | None) –
- classmethod _check_funcdef(node)[source]¶
Check if function definition contains positional-only parameters.
- Parameters:
node (parso.python.tree.Function) – function definition
- Returns:
if function definition contains positional-only parameters
- Return type:
- classmethod _check_lambdef(node)[source]¶
Check if lambda definition contains positional-only parameters.
- Parameters:
node (parso.python.tree.Lambda) – lambda definition
- Returns:
if lambda definition contains positional-only parameters
- Return type:
- _concat()[source]¶
Concatenate final string.
This method tries to inserted the runtime check decorator function at the very location where starts to contain positional-only parameters, i.e. between the converted code as
self._prefixandself._suffix.The inserted code is rendered from
DECORATOR_TEMPLATE. Ifself._pep8isTrue, it will insert the code in compliance with PEP 8.- Return type:
- _process_async_funcdef(node)[source]¶
Process
asyncfunction definition (async_funcdef).- Parameters:
node (parso.python.tree.PythonNode) –
asyncfunction node- Return type:
This method processes an
asyncfunction node. It will extract theasynckeyword node (parso.python.tree.Keyword) and the function node (parso.python.tree.Function) then pass on the processing toself._process_funcdef.
- _process_async_stmt(node)[source]¶
Process
asyncstatement (async_stmt).- Parameters:
node (parso.python.tree.PythonNode) –
asyncstatement node- Return type:
This method processes an
asyncstatement node. If such statement is an async function, then it will pass on the processing toself._process_funcdef.
- _process_classdef(node)[source]¶
Process class definition (
classdef).- Parameters:
node (parso.python.tree.Class) – class node
- Return type:
This method converts the whole class suite context with
_process_suite_node()throughContextrespectively.
- _process_for_stmt(node)[source]¶
Process for statement (
for_stmt).- Parameters:
node (parso.python.tree.ForStmt) – for node
- Return type:
This method processes the indented suite under the for and optional else statements.
- _process_fstring(node)[source]¶
Process formatted strings (
f_string).- Parameters:
node (parso.python.tree.PythonNode) – formatted strings node
- Return type:
- _process_funcdef(node, *, async_ctx=None)[source]¶
Process function definition (
funcdef).- Parameters:
node (parso.python.tree.Function) – function node
async_ctx (Keyword | None) –
- Keyword Arguments:
async_ctx (Optional[parso.python.tree.Keyword]) –
asynckeyword AST node- Return type:
- _process_if_stmt(node)[source]¶
Process if statement (
if_stmt).- Parameters:
node (parso.python.tree.IfStmt) – if node
- Return type:
This method processes each indented suite under the if, elif, and else statements.
- _process_lambdef(node)[source]¶
Process lambda definition (
lambdef).- Parameters:
node (parso.python.tree.Lambda) – lambda node
- Return type:
- _process_string_context(node)[source]¶
Process string contexts (
stringliteral).- Parameters:
node (parso.python.tree.PythonNode) – string literals node
- Return type:
This method first checks if
nodecontains position-only parameters. 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 initiate 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.
- _process_strings(node)[source]¶
Process concatenable strings (
stringliteral).- Parameters:
node (parso.python.tree.PythonNode) – concatentable strings node
- Return type:
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.
- _process_suite_node(node, *, cls_ctx=None)[source]¶
Process indented suite (
suiteor others).- Parameters:
node (parso.tree.NodeOrLeaf) – suite node
cls_ctx (str | None) –
- Keyword Arguments:
cls_ctx (Optional[str]) – class context name
- Return type:
This method first checks if
nodecontains positional-only parameters. If not, it will not perform any processing, rather just append the original source code to context buffer.If
nodecontains positional-only parameters, then it will initiate another Context` instance to perform the conversion process on suchnode.
- _process_try_stmt(node)[source]¶
Process try statement (
try_stmt).- Parameters:
node (parso.python.tree.TryStmt) – try node
- Return type:
This method processes the indented suite under the try, except, else, and finally statements.
- _process_while_stmt(node)[source]¶
Process while statement (
while_stmt).- Parameters:
node (parso.python.tree.WhileStmt) – while node
- Return type:
This method processes the indented suite under the while and optional else statements.
- _process_with_stmt(node)[source]¶
Process with statement (
with_stmt).- Parameters:
node (parso.python.tree.WithStmt) – with node
- Return type:
This method processes the indented suite under the with statement.
- classmethod has_expr(node)[source]¶
Check if node has positional-only parameters.
- Parameters:
node (parso.tree.NodeOrLeaf) – parso AST
- Returns:
if
nodehas positional-only parameters- Return type:
- classmethod has_poseur(node)¶
Check if node has positional-only parameters.
- Parameters:
node (parso.tree.NodeOrLeaf) – parso AST
- Returns:
if
nodehas positional-only parameters- Return type:
- normalizer(name)[source]¶
Variable name normalizer.
If
self._cls_ctxisNone, the methold will simply classnormalize()on the variable name; otherwise, it will callmangle()on the variable name.
- _abc_impl = <_abc_data object>¶
- _cls_ctx: Optional[str]¶
Class context name. This is rather useful as we might need to mangle and/or normalize variable names in certain scenario.
- Type:
Optional[str]
- pattern_funcdef = re.compile('^\\s*(async\\s+)?def\\s', re.ASCII)¶
Pattern to find the function definition line.
- Type:
re.Pattern
- class poseur.StringContext(node, config, *, clx_ctx=None, indent_level=0, raw=True)[source]¶
Bases:
ContextString (f-string) conversion context.
This class is mainly used for converting formatted strings.
- Parameters:
node (parso.python.tree.PythonNode) – parso AST
config (Config) – conversion configurations
clx_ctx (str | None) –
indent_level (int) –
raw (typing_extensions.Literal[True]) –
- 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 flagclx_ctx (str | None) –
- _abc_impl = <_abc_data object>¶
- _buffer: str¶
Final converted result.
- _cls_ctx: Optional[str]¶
Class context name. This is rather useful as we might need to mangle and/or normalize variable names in certain scenario.
- Type:
Optional[str]
- _indent_level: Final[int]¶
Current indentation level.
- _indentation: Final[str] # type: ignore[attr-defined]¶
Indentation sequence.
- _linesep: Final[Linesep] # type: ignore[attr-defined]¶
Line seperator.
- Type:
Final[
Linesep]
- _node_before_expr: Optional[parso.tree.NodeOrLeaf]¶
Preceding node with the target expression, i.e. the insertion point.
- _prefix: str¶
Code before 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.
- _suffix: str¶
Code after insertion point.
- _uuid_gen: Final[UUID4Generator]¶
UUID generator.
- config: Final[Config]¶
Internal configurations.
Internal Auxiliaries¶
Options & Defaults¶
- poseur.POSEUR_SOURCE_VERSIONS = ['3.8', '3.9']¶
Get supported source versions.
Below are option getter utility functions. Option value precedence is:
explicit value (CLI/API arguments) > environment variable > default value
- poseur._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:
POSEUR_QUIET– the value in environment variable
See also
- poseur._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:
POSEUR_CONCURRENCY– the value in environment variable
See also
- poseur._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:
POSEUR_DO_ARCHIVE– the value in environment variable
See also
- poseur._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:
POSEUR_ARCHIVE_PATH– the value in environment variable
See also
- poseur._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:
POSEUR_SOURCE_VERSION– the value in environment variable
See also
- poseur._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:
POSEUR_LINESEP– the value in environment variable
See also
- poseur._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:
POSEUR_INDENTATION– the value in environment variable
See also
- poseur._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:
POSEUR_PEP8– the value in environment variable
See also
The following variables are used for fallback default values of options.
- poseur._default_quiet = False¶
Default value for the
quietoption.
- poseur._default_concurrency = None¶
Default value for the
concurrencyoption.
- poseur._default_do_archive = True¶
Default value for the
do_archiveoption.
- poseur._default_archive_path = 'archive'¶
Default value for the
archive_pathoption.
- poseur._default_source_version = '3.9'¶
Default value for the
source_versionoption.
- poseur._default_linesep = None¶
Default value for the
linesepoption.
- poseur._default_indentation = None¶
Default value for the
indentationoption.
- poseur._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.
- poseur.__cwd__: str¶
Current working directory returned by
os.getcwd().
- poseur.__poseur_quiet__: Literal[\'quiet mode\', \'non-quiet mode\']¶
Default value for the
--quietoption.See also
- poseur.__poseur_concurrency__: Union[int, Literal[\'auto detect\']]¶
Default value for the
--concurrencyoption.See also
- poseur.__poseur_do_archive__: Literal[\'will do archive\', \'will not do archive\']¶
Default value for the
--no-archiveoption.See also
- poseur.__poseur_linesep__: Literal[\'LF\', \'CRLF\', \'CR\', \'auto detect\']¶
Default value for the
--linesepoption.See also
- poseur.__poseur_pep8__: Literal[\'will conform to PEP 8\', \'will not conform to PEP 8\']¶
Default value for the
--no-pep8option.See also