diff options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | astroid/builder.py | 5 | ||||
-rw-r--r-- | astroid/nodes/scoped_nodes/scoped_nodes.py | 60 | ||||
-rw-r--r-- | astroid/rebuilder.py | 1 | ||||
-rw-r--r-- | tests/test_nodes_lineno.py | 2 | ||||
-rw-r--r-- | tests/test_scoped_nodes.py | 23 |
6 files changed, 19 insertions, 73 deletions
@@ -43,6 +43,7 @@ Release date: TBA - ``nodes.IfExp`` - ``nodes.Keyword`` - ``nodes.Lambda`` + - ``nodes.Module`` - ``nodes.Name`` - ``nodes.Raise`` - ``nodes.Return`` diff --git a/astroid/builder.py b/astroid/builder.py index 957b20ac..dc1738ef 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -281,8 +281,9 @@ class AstroidBuilder(raw_building.InspectBuilder): def build_namespace_package_module(name: str, path: Sequence[str]) -> nodes.Module: - # TODO: Typing: Remove the cast to list and just update typing to accept Sequence - return nodes.Module(name, path=list(path), package=True) + module = nodes.Module(name, path=path, package=True) + module.postinit(body=[], doc_node=None) + return module def parse( diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index cf72e1e0..2c1daf4c 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -15,12 +15,11 @@ import itertools import os import sys import warnings -from collections.abc import Generator, Iterator +from collections.abc import Generator, Iterator, Sequence from functools import lru_cache from typing import TYPE_CHECKING, ClassVar, NoReturn, TypeVar, overload from astroid import bases, util -from astroid import decorators as decorators_mod from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS from astroid.context import ( CallContext, @@ -188,11 +187,8 @@ class Module(LocalsDictNodeNG): _astroid_fields = ("doc_node", "body") - fromlineno: Literal[0] = 0 - """The first line that this node appears on in the source code.""" - - lineno: Literal[0] = 0 - """The line that this node appears on in the source code.""" + doc_node: Const | None + """The doc node associated with this node.""" # attributes below are set by the builder module or by raw factories @@ -224,41 +220,18 @@ class Module(LocalsDictNodeNG): ) _other_other_fields = ("locals", "globals") - col_offset: None - end_lineno: None - end_col_offset: None - parent: None - - @decorators_mod.deprecate_arguments(doc="Use the postinit arg 'doc_node' instead") def __init__( self, name: str, - doc: str | None = None, file: str | None = None, - path: list[str] | None = None, - package: bool | None = None, - parent: None = None, - pure_python: bool | None = True, + path: Sequence[str] | None = None, + package: bool = False, + pure_python: bool = True, ) -> None: - """ - :param name: The name of the module. - - :param doc: The module docstring. - - :param file: The path to the file that this ast has been extracted from. - - :param path: - - :param package: Whether the node represents a package or a module. - - :param parent: The parent node in the syntax tree. - - :param pure_python: Whether the ast was built from source. - """ self.name = name """The name of the module.""" - self._doc = doc + self._doc = None """The module docstring.""" self.file = file @@ -282,26 +255,21 @@ class Module(LocalsDictNodeNG): self.locals = self.globals = {} """A map of the name of a local variable to the node defining the local.""" - self.body: list[node_classes.NodeNG] | None = [] + self.body: list[node_classes.NodeNG] = [] """The contents of the module.""" - self.doc_node: Const | None = None - """The doc node associated with this node.""" - self.future_imports: set[str] = set() """The imports from ``__future__``.""" - super().__init__(lineno=0, parent=parent) + super().__init__( + lineno=0, parent=None, col_offset=0, end_lineno=None, end_col_offset=None + ) # pylint: enable=redefined-builtin - def postinit(self, body=None, *, doc_node: Const | None = None): - """Do some setup after initialisation. - - :param body: The contents of the module. - :type body: list(NodeNG) or None - :param doc_node: The doc node associated with this node. - """ + def postinit( + self, body: list[node_classes.NodeNG], *, doc_node: Const | None = None + ): self.body = body self.doc_node = doc_node if doc_node: diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index c7a940cd..011b6f15 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -248,7 +248,6 @@ class TreeRebuilder: file=modpath, path=[modpath], package=package, - parent=None, ) newnode.postinit( [self.visit(child, newnode) for child in node.body], diff --git a/tests/test_nodes_lineno.py b/tests/test_nodes_lineno.py index 962fe483..bbc1a300 100644 --- a/tests/test_nodes_lineno.py +++ b/tests/test_nodes_lineno.py @@ -1241,6 +1241,6 @@ class TestLinenoColOffset: module = astroid.parse(code) assert isinstance(module, nodes.Module) assert module.lineno == 0 - assert module.col_offset is None + assert module.col_offset == 0 assert module.end_lineno is None assert module.end_col_offset is None diff --git a/tests/test_scoped_nodes.py b/tests/test_scoped_nodes.py index 3e8256dd..0cfe411c 100644 --- a/tests/test_scoped_nodes.py +++ b/tests/test_scoped_nodes.py @@ -2947,26 +2947,3 @@ def test_deprecation_of_doc_attribute() -> None: with pytest.warns(DeprecationWarning) as records: assert node_func.doc == "Docstring" assert len(records) == 1 - - # If 'doc' is passed to Module, ClassDef, FunctionDef, - # a DeprecationWarning should be raised - doc_node = nodes.Const("Docstring") - with pytest.warns(DeprecationWarning) as records: - node_module = nodes.Module(name="MyModule", doc="Docstring") - node_class = nodes.ClassDef( - name="MyClass", - lineno=0, - col_offset=0, - end_lineno=0, - end_col_offset=0, - parent=nodes.Unknown(), - ) - node_func = nodes.FunctionDef( - name="MyFunction", - lineno=0, - col_offset=0, - parent=node_module, - end_lineno=0, - end_col_offset=0, - ) - assert len(records) == 1 |