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.

body: list[node_classes.NodeNG]

The contents of the module.

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

display_type() str[source]

A human readable type of this node.

Returns:

The type of this node.

Return type:

str

doc_node: Const | None

The doc node associated with this node.

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_bytes: str | bytes | None = None

The string/bytes that this ast was built from.

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

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.

future_imports: set[str]

The imports from __future__.

get_children()[source]

Get the child nodes below this node.

getattr(name, context: InferenceContext | None = None, ignore_locals=False)[source]
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.

postinit(body: list[NodeNG], *, doc_node: Const | None = None)[source]
previous_sibling()[source]

The previous sibling statement.

Returns:

The previous sibling statement node.

Return type:

NodeNG or None

public_names()[source]

The list of the names that are publicly available in this module.

Returns:

The list of public names.

Return type:

list(str)

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

wildcard_import_names()[source]

The list of imported names when this module is ‘wildcard imported’.

It doesn’t include the ‘__builtins__’ name which is added by the current CPython implementation of wildcard imports.

Returns:

The list of imported names.

Return type:

list(str)