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 | collect |
Returns a list of ast.Assign or ast.AnnAssign declared in the given scope. It does not include assignments in nested scopes. |
| Function | dottedname2node |
Reverse operation of node2dottedname. |
| 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 |
Retreive the literal value of an argument from the BoundArguments. Only works with purely literal values (no Name or Attribute). |
| 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 | 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 |
Utility function to iterate decorators. |
| 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 | safe |
Binds the arguments of a function call to that function's signature. |
| 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 |
| Class | _V |
Undocumented |
| Function | _annotation |
Undocumented |
| Function | _annotation |
Undocumented |
| Function | _collect |
Undocumented |
| Function | _get |
Helper function for get_literal_arg. |
| Function | _is |
Undocumented |
| Constant | _SCOPE |
Undocumented |
| Type Variable | _T |
Undocumented |
| Type Alias | _ |
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. |
Returns a list of ast.Assign or ast.AnnAssign declared in the given scope. It does not include assignments in nested scopes.
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.
Return the docstring node for the given class, function or module or None if no docstring can be found.
BoundArguments, name: str, default: _T, typecheck: type[ _T] | tuple[ type[ _T], ...], lineno: int, ctx: model.Documentable) -> _T:
(source)
¶
Retreive the literal value of an argument from the BoundArguments. Only works with purely literal values (no Name or Attribute).
| Parameters | |
args:BoundArguments | The BoundArguments instance. |
name:str | The name of the argument |
default:_T | The default value of the argument, this value is returned if the argument is not found. |
typecheck:type[ | The type of the literal value this argument is expected to have. |
lineno:int | The lineumber of the callsite, used for error reporting. |
ctx:model.Documentable | Undocumented |
| module | Module that contains the call, used for error reporting. |
| Returns | |
_T | The value of the argument if we can infer it, otherwise returns the default value. |
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.
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.
list[ ast.expr], ctx: model.Documentable) -> Iterator[ tuple[ str | None, ast.expr]]:
(source)
¶
Utility function to iterate decorators.
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
Signature, call: ast.AST, ctx: model.Documentable) -> inspect.BoundArguments | None:
(source)
¶
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 |
|
ast.AST, typecheck: _ClassInfo, stop_typecheck: _ClassInfo = _SCOPE_TYPES) -> Sequence[ ast.AST]:
(source)
¶
Undocumented
BoundArguments, name: str, typecheck: type[ _T] | tuple[ type[ _T], ...]) -> Literal[ _V.NoValue] | _T:
(source)
¶
Helper function for get_literal_arg.
If the value is not present in the arguments, returns _V.NoValue.
| Raises | |
ValueError | If the passed value is not a literal or if it's not the right type. |
Undocumented
| Value |
|