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.
Installation¶
python3 -m pip install astatine --user
First add the required channels
conda config --add channels https://conda.anaconda.org/conda-forge
conda config --add channels https://conda.anaconda.org/domdfcoding
Then install
conda install astatine
python3 -m pip install git+https://github.com/python-formate/astatine@master --user
Contents¶
astatine¶
Some handy helper functions for Python’s AST module.
Functions:
|
Returns the elements of the dotted attribute name for the given AST node. |
|
Returns a |
|
For the given |
|
Returns the line number of the start of the docstring for |
|
Returns a list of comment lines from |
|
Returns whether the given |
|
Returns a mapping of argument names to the AST nodes representing their values, for the given function call. |
|
Recursively add the |
-
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
, orast.Call
)- Return type
-
get_constants
(module)[source]¶ Returns a
name: value
mapping of constants in the given module.New in version 0.3.1.
-
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.
-
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.
-
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
-
is_type_checking
(node)[source]¶ Returns whether the given
if
block isif typing.TYPE_CHECKING
or equivalent.
-
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.
-
mark_text_ranges
(node, source)[source]¶ Recursively add the
end_lineno
andend_col_offset
attributes to each child ofnode
which already has the attributeslineno
andcol_offset
.- Parameters
node (
AST
) – An AST node created withast.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.

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 |
---|---|---|
|
|
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.