Various bits of reusable code related to ast.AST node processing.
| Class | |
Generic AST node visitor. This class does not work like ast.NodeVisitor, it only visits statements directly within a body. Also, visitor methods can't return anything. |
| Class | |
Undocumented |
| Class | op |
This class provides data and functions for mapping AST nodes to symbols and precedences. |
| Class | |
Add parent attribute to ast nodes instances. |
| Class | |
Wraps ast.Constant/ast.Str for `isinstance` checks and annotations. Ensures that the value is actually a string. Do not try to instanciate this class. |
| Function | bind |
Binds the arguments of a function call to that function's signature. |
| Function | extract |
Extract docstring information from an ast node that represents the docstring. |
| Function | extract |
In older CPython versions, the AST only tells us the end line number and we must approximate the start line number. This approximation is correct if the docstring does not contain explicit newlines ('\n') or joined lines ('\' at end of line). |
| Function | get |
Get the docstring for a ast.Assign or ast.AnnAssign node. |
| Function | get |
Return the docstring node for the given class, function or module or None if no docstring can be found. |
| Function | get |
Undocumented |
| Function | get |
Tell in wich block the given node lives in. |
| Function | get |
Undocumented |
| Function | get |
Once nodes have the .parent attribute with {Parentage}, use this function to get a iterator on all parents of the given node up to the root module. |
| Function | get |
Undocumented |
| Function | has |
Returns True if the is a comment line in between node1 and node2. |
| Function | infer |
Infer a literal expression's type. |
| Function | is |
Returns whether or not the given ast.Compare is equal to __name__ == '__main__'. |
| Function | is |
Does this AST node represent the literal constant None? |
| Function | is |
Returns True if the module is a pre PEP 420 namespace package: |
| Function | is |
Whether this annotation node refers to a typing alias. |
| Function | is |
Detect if this expr is firstly composed by one of the specified annotation(s)' full name. |
| Function | is |
Undocumented |
| Function | is |
Undocumented |
| Function | iter |
Undocumented |
| Function | iterassign |
Utility function to iterate assignments targets. |
| Function | node2dottedname |
Resove expression composed by ast.Attribute and ast.Name nodes to a list of names. |
| Function | node2fullname |
Undocumented |
| Function | unstring |
Replace all strings in the given expression by parsed versions. |
| Function | upgrade |
Transform the annotation to use python 3.10+ syntax. |
| Constant | DEPRECATED |
Undocumented |
| Constant | SUBSCRIPTABLE |
Undocumented |
| Constant | TYPING |
Undocumented |
| Class | _ |
Implementation of unstring_annotation(). |
| Class | _ |
Undocumented |
| Class | _ |
Undocumented |
| Class | _ |
Undocumented |
| Function | _annotation |
Undocumented |
| Function | _annotation |
Undocumented |
| Function | _is |
Undocumented |
| Type Alias | _ |
Undocumented |
| Variable | _deprecated |
Undocumented |
| Variable | _op |
Undocumented |
| Variable | _precedence |
Undocumented |
| Variable | _symbol |
Undocumented |
Binds the arguments of a function call to that function's signature.
| Raises | |
TypeError | If the arguments do not match the signature. |
Extract docstring information from an ast node that represents the docstring.
| Returns | |
tuple[ |
|
In older CPython versions, the AST only tells us the end line number and we must approximate the start line number. This approximation is correct if the docstring does not contain explicit newlines ('\n') or joined lines ('\' at end of line).
Leading blank lines are stripped by cleandoc(), so we must return the line number of the first non-blank line.
Get the docstring for a ast.Assign or ast.AnnAssign node.
This helper function relies on the non-standard .parent attribute on AST nodes to navigate upward in the tree and determine this node direct siblings.
| Note | |
This does not validate whether there is a comment in between the assigment and the docstring node since the function operates on AST solely. Use has_comment_line for that. |
Return the docstring node for the given class, function or module or None if no docstring can be found.
Tell in wich block the given node lives in.
A block is defined by a tuple: (parent node, fieldname)
| Raises | |
ValueError | If the assignment parent is missing or boggus. |
Once nodes have the .parent attribute with {Parentage}, use this function to get a iterator on all parents of the given node up to the root module.
ast.expr | ast.stmt, node2: ast.expr | ast.stmt, lines: Sequence[ str]) -> bool:
(source)
¶
Returns True if the is a comment line in between node1 and node2.
>>> from pydoctor.model import ParsedAstModule >>> from pydoctor.astbuilder import SyntaxTreeParser >>> src = 'var = 1\n# this is a comment\nfoo = 2\n\n\nplum = 3' >>> parsed = SyntaxTreeParser().parseString(src, None) >>> has_comment_line(parsed.root.body[0], parsed.root.body[1], parsed.lines) True >>> has_comment_line(parsed.root.body[1], parsed.root.body[2], parsed.lines) False
| Raises | |
IndexError | If the line numbers coming from node1 or node2 are not present in the given lines. |
Infer a literal expression's type.
| Parameters | |
expr:ast.expr | The expression's AST. |
| Returns | |
ast.expr | None | A type annotation, or None if the expression has no obvious type. |
Returns True if the module is a pre PEP 420 namespace package:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
# OR
import pkg_resources
pkg_resources.declare_namespace(__name__)
# OR
__import__('pkg_resources').declare_namespace(__name__)
# OR
import pkg_resources
pkg_resources.declare_namespace(name=__name__)
The following code will return False, tho:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__ + '.impl')
ast.AST | None, annotations: Sequence[ str], ctx: model.Documentable) -> bool:
(source)
¶
Detect if this expr is firstly composed by one of the specified annotation(s)' full name.
Utility function to iterate assignments targets.
Useful for all the following AST assignments:
var:int=2
self.var = target = node.astext()
ol = ['extensions']
NOT Useful for the following AST assignments:
x, y = [1,2]
Example:
>>> from pydoctor.astutils import iterassign >>> from ast import parse >>> node = parse('self.var = target = thing[0] = node.astext()').body[0] >>> list(iterassign(node)) [['self', 'var'], ['target'], None]
ast.AST | None, ctx: model.Documentable | None = None, *, expandName: Callable[ [ str], str] | None = None) -> str | None:
(source)
¶
Undocumented
ast.expr, ctx: model.Documentable, section: str = 'annotation') -> ast.expr:
(source)
¶
Replace all strings in the given expression by parsed versions.
| Returns | |
ast.expr | The unstringed node. If parsing fails, an error is logged and the original node is returned. |
ast.expr, ctx: model.Documentable, section: str = 'annotation') -> ast.expr:
(source)
¶
Transform the annotation to use python 3.10+ syntax.
Undocumented
| Value |
|
Undocumented
| Value |
|