astatine

Some handy helper functions for Python’s AST module.

Astatine is a chemical element with the symbol At and atomic number 85. Its name comes from the Greek astatos (ἄστατος), meaning “unstable”. It’s a fitting name for Python’s ast module, which has been unstable between releases.

Docs

Documentation Build Status Docs Check Status

Tests

Linux Test Status Windows Test Status macOS Test Status Coverage

PyPI

PyPI - Package Version PyPI - Supported Python Versions PyPI - Supported Implementations PyPI - Wheel

Anaconda

Conda - Package Version Conda - Platform

Activity

GitHub last commit GitHub commits since tagged version Maintenance PyPI - Downloads

QA

CodeFactor Grade Flake8 Status mypy status

Other

License GitHub top language Requirements Status

Installation

python3 -m pip install astatine --user

Contents

astatine

Some handy helper functions for Python’s AST module.

Functions:

get_attribute_name(node)

Returns the elements of the dotted attribute name for the given AST node.

get_constants(module)

Returns a name: value mapping of constants in the given module.

get_contextmanagers(with_node)

For the given with block, returns a mapping of the contextmanager names to the individual nodes.

get_docstring_lineno(node)

Returns the line number of the start of the docstring for node.

get_toplevel_comments(source)

Returns a list of comment lines from source which occur before the first line of source code (including before module-level docstrings).

is_type_checking(node)

Returns whether the given if block is if typing.TYPE_CHECKING or equivalent.

kwargs_from_node(node, posarg_names)

Returns a mapping of argument names to the AST nodes representing their values, for the given function call.

mark_text_ranges(node, source)

Recursively add the end_lineno and end_col_offset attributes to each child of node which already has the attributes lineno and col_offset.

get_attribute_name(node)[source]

Returns the elements of the dotted attribute name for the given AST node.

New in version 0.3.1.

Parameters

node (AST)

Raises

NotImplementedError – if the name contains an unknown node (i.e. not ast.Name, ast.Attribute, or ast.Call)

Return type

Iterable[str]

get_constants(module)[source]

Returns a name: value mapping of constants in the given module.

New in version 0.3.1.

Parameters

module (Module)

Return type

Dict[str, Any]

get_contextmanagers(with_node)[source]

For the given with block, returns a mapping of the contextmanager names to the individual nodes.

New in version 0.3.1.

Parameters

with_node (With)

Return type

Dict[Tuple[str, …], withitem]

get_docstring_lineno(node)[source]

Returns the line number of the start of the docstring for node.

Parameters

node (Union[FunctionDef, ClassDef, Module])

Warning

On CPython 3.6 and 3.7 the line number may not be correct, due to https://bugs.python.org/issue16806.

CPython 3.8 and above are unaffected, as are PyPy 3.6 and 3.7

Accurate line numbers on CPython 3.6 and 3.7 may be obtained by using https://github.com/domdfcoding/typed_ast, which contains the backported fix from Python 3.8.

Return type

Optional[int]

get_toplevel_comments(source)[source]

Returns a list of comment lines from source which occur before the first line of source code (including before module-level docstrings).

Parameters

source (str)

Return type

StringList

is_type_checking(node)[source]

Returns whether the given if block is if typing.TYPE_CHECKING or equivalent.

Parameters

node (AST)

Return type

bool

kwargs_from_node(node, posarg_names)[source]

Returns a mapping of argument names to the AST nodes representing their values, for the given function call.

New in version 0.3.1.

Parameters
  • node (Call)

  • posarg_names (Union[Iterable[str], Callable]) – Either a list of positional argument names for the function, or the function object.

Return type

Dict[str, AST]

mark_text_ranges(node, source)[source]

Recursively add the end_lineno and end_col_offset attributes to each child of node which already has the attributes lineno and col_offset.

Parameters
  • node (AST) – An AST node created with ast.parse().

  • source (str) – The corresponding source code for the node.

Downloading source code

The astatine source code is available on GitHub, and can be accessed from the following URL: https://github.com/python-formate/astatine

If you have git installed, you can clone the repository with the following command:

git clone https://github.com/python-formate/astatine
Cloning into 'astatine'...
remote: Enumerating objects: 47, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 173 (delta 16), reused 17 (delta 6), pack-reused 126
Receiving objects: 100% (173/173), 126.56 KiB | 678.00 KiB/s, done.
Resolving deltas: 100% (66/66), done.
Alternatively, the code can be downloaded in a ‘zip’ file by clicking:
Clone or download –> Download Zip
Downloading a 'zip' file of the source code.

Downloading a ‘zip’ file of the source code

Building from source

The recommended way to build astatine is to use tox:

tox -e build

The source and wheel distributions will be in the directory dist.

If you wish, you may also use pep517.build or another PEP 517-compatible build tool.

License

astatine is licensed under the MIT License

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

Permissions Conditions Limitations
  • Commercial use
  • Modification
  • Distribution
  • Private use
  • Liability
  • Warranty

Copyright (c) 2021-2022 Dominic Davis-Foster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

View the Function Index or browse the Source Code.

Browse the GitHub Repository