Module¶
- class astroid.nodes.Module(name: str, file: str | None = None, path: Sequence[str] | None = None, package: bool = False, pure_python: bool = True)[source]¶
Bases:
LocalsDictNodeNG
Class representing an
ast.Module
node.>>> import astroid >>> node = astroid.extract_node('import astroid') >>> node <Import l.1 at 0x7f23b2e4e5c0> >>> node.parent <Module l.0 at 0x7f23b2e4eda0>
- absolute_import_activated() bool [source]¶
Whether PEP 328 absolute import behaviour has been enabled.
- Returns:
Whether PEP 328 has been enabled.
- block_range(lineno: int) tuple[int, int] [source]¶
Get a range from where this node starts to where this node ends.
- Parameters:
lineno – Unused.
- Returns:
The range of line numbers that this node belongs to.
- bool_value(context: InferenceContext | None = None) bool [source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node. For a
Module
this is alwaysTrue
.
- display_type() str [source]¶
A human readable type of this node.
- Returns:
The type of this node.
- Return type:
- file¶
The path to the file that this ast has been extracted from.
This will be
None
when the representation has been built from a built-in module.
- file_encoding: str | None = None¶
The encoding of the source file.
This is used to get unicode out of a source file. Python 2 only.
- frame(*, future: Literal[None, True] = None) _T [source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- fully_defined() bool [source]¶
Check if this module has been build from a .py file.
If so, the module contains a complete representation, including the code.
- Returns:
Whether the module has been built from a .py file.
- globals: dict[str, list[InferenceResult]]¶
A map of the name of a global variable to the node defining the global.
- igetattr(name: str, context: InferenceContext | None = None) Iterator[InferenceResult] [source]¶
Infer the possible values of the given variable.
- Parameters:
name – The name of the variable to infer.
- Returns:
The inferred possible values.
- import_module(modname: str, relative_only: bool = False, level: int | None = None, use_cache: bool = True) Module [source]¶
Get the ast for a given module as if imported from this module.
- Parameters:
modname – The name of the module to “import”.
relative_only – Whether to only consider relative imports.
level – The level of relative import.
use_cache – Whether to use the astroid_cache of modules.
- Returns:
The imported module ast.
- locals: dict[str, list[InferenceResult]]¶
A map of the name of a local variable to the node defining the local.
- name¶
The name of the module.
- next_sibling()[source]¶
The next sibling statement node.
- Returns:
The next sibling statement node.
- Return type:
NodeNG or None
- package¶
Whether the node represents a package or a module.
- previous_sibling()[source]¶
The previous sibling statement.
- Returns:
The previous sibling statement node.
- Return type:
NodeNG or None
- pure_python¶
Whether the ast was built from source.
- pytype() Literal['builtins.module'] [source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- relative_to_absolute_name(modname: str, level: int | None) str [source]¶
Get the absolute module name for a relative import.
The relative import can be implicit or explicit.
- Parameters:
modname – The module name to convert.
level – The level of relative import.
- Returns:
The absolute module name.
- Raises:
TooManyLevelsError – When the relative import refers to a module too far above this one.
- scope_attrs: ClassVar[set[str]] = {'__doc__', '__file__', '__name__', '__package__', '__path__'}¶
The names of module attributes available through the global scope.
- scope_lookup(node: LookupMixIn, name: str, offset: int = 0) tuple[LocalsDictNodeNG, list[node_classes.NodeNG]] [source]¶
Lookup where the given variable is assigned.
- Parameters:
node – The node to look for assignments up to. Any assignments after the given node are ignored.
name – The name of the variable to find assignments for.
offset – The line offset to filter statements up to.
- Returns:
This scope node and the list of assignments associated to the given name according to the scope where it has been found (locals, globals or builtin).
- special_attributes¶
The names of special attributes that this module has.
- statement(*, future: Literal[None, True] = None) NoReturn [source]¶
The first parent node, including self, marked as statement node.
When called on a
Module
this raises a StatementMissing.
- stream()[source]¶
Get a stream to the underlying file or bytes.
- Type:
file or io.BytesIO or None