diff options
| author | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-04-22 11:27:19 +0200 |
|---|---|---|
| committer | Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> | 2023-04-22 11:48:05 +0200 |
| commit | b670d91328e70a51833eaa109f62c5fe6acc3089 (patch) | |
| tree | 5c27b9d9cf6343962285cfee0596d870ea19e4b1 | |
| parent | 687afe0308e73f22a49dd0eec65c9cae2c06e55e (diff) | |
| download | astroid-git-b670d91328e70a51833eaa109f62c5fe6acc3089.tar.gz | |
Fix constructors of ``FunctionDef``
Co-authored-by: Nick Drozd <nicholasdrozd@gmail.com>
| -rw-r--r-- | ChangeLog | 1 | ||||
| -rw-r--r-- | astroid/interpreter/objectmodel.py | 29 | ||||
| -rw-r--r-- | astroid/nodes/scoped_nodes/scoped_nodes.py | 78 | ||||
| -rw-r--r-- | astroid/objects.py | 20 | ||||
| -rw-r--r-- | astroid/raw_building.py | 9 | ||||
| -rw-r--r-- | tests/test_scoped_nodes.py | 20 |
6 files changed, 92 insertions, 65 deletions
@@ -37,6 +37,7 @@ Release date: TBA - ``nodes.ExceptHandler`` - ``nodes.Expr`` - ``nodes.For`` + - ``nodes.FunctionDef`` - ``nodes.If`` - ``nodes.IfExp`` - ``nodes.Keyword`` diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index 6ff64afc..5090c536 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -376,6 +376,8 @@ class FunctionModel(ObjectModel): lineno=func.lineno, col_offset=func.col_offset, parent=func.parent, + end_lineno=func.end_lineno, + end_col_offset=func.end_col_offset, ) # pylint: disable=no-member new_func.postinit( @@ -860,7 +862,14 @@ class PropertyModel(ObjectModel): """Model for a builtin property.""" def _init_function(self, name): - function = nodes.FunctionDef(name=name, parent=self._instance) + function = nodes.FunctionDef( + name=name, + parent=self._instance, + lineno=self._instance.lineno, + col_offset=self._instance.col_offset, + end_lineno=self._instance.end_lineno, + end_col_offset=self._instance.end_col_offset, + ) args = nodes.Arguments(parent=function, vararg=None, kwarg=None) args.postinit( @@ -895,7 +904,14 @@ class PropertyModel(ObjectModel): caller=caller, context=context ) - property_accessor = PropertyFuncAccessor(name="fget", parent=self._instance) + property_accessor = PropertyFuncAccessor( + name="fget", + parent=self._instance, + lineno=self._instance.lineno, + col_offset=self._instance.col_offset, + end_lineno=self._instance.end_lineno, + end_col_offset=self._instance.end_col_offset, + ) property_accessor.postinit(args=func.args, body=func.body) return property_accessor @@ -935,7 +951,14 @@ class PropertyModel(ObjectModel): ) yield from func_setter.infer_call_result(caller=caller, context=context) - property_accessor = PropertyFuncAccessor(name="fset", parent=self._instance) + property_accessor = PropertyFuncAccessor( + name="fset", + parent=self._instance, + lineno=self._instance.lineno, + col_offset=self._instance.col_offset, + end_lineno=self._instance.end_lineno, + end_col_offset=self._instance.end_col_offset, + ) property_accessor.postinit(args=func_setter.args, body=func_setter.body) return property_accessor diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 628e6ad4..e5a1523e 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -42,7 +42,7 @@ from astroid.exceptions import ( from astroid.interpreter.dunder_lookup import lookup from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel from astroid.manager import AstroidManager -from astroid.nodes import Arguments, Const, NodeNG, _base_nodes, node_classes +from astroid.nodes import Arguments, Const, NodeNG, Unknown, _base_nodes, node_classes from astroid.nodes.scoped_nodes.mixin import ComprehensionScope, LocalsDictNodeNG from astroid.nodes.scoped_nodes.utils import builtin_lookup from astroid.nodes.utils import Position @@ -1233,9 +1233,16 @@ class FunctionDef( _astroid_fields = ("decorators", "args", "returns", "doc_node", "body") _multi_line_block_fields = ("body",) returns = None - decorators: node_classes.Decorators | None = None + + decorators: node_classes.Decorators | None """The decorators that are applied to this method or function.""" + doc_node: Const | None + """The doc node associated with this node.""" + + args: Arguments + """The arguments that the function takes.""" + is_function = True """Whether this node indicates a function. @@ -1272,64 +1279,27 @@ class FunctionDef( special_attributes = FunctionModel() """The names of special attributes that this function has.""" - @decorators_mod.deprecate_arguments(doc="Use the postinit arg 'doc_node' instead") def __init__( self, - name=None, - doc: str | None = None, - lineno=None, - col_offset=None, - parent=None, + name: str, + lineno: int, + col_offset: int, + parent: NodeNG, *, - end_lineno=None, - end_col_offset=None, - ): - """ - :param name: The name of the function. - :type name: str or None - - :param doc: The function docstring. - - :param lineno: The line that this node appears on in the source code. - :type lineno: int or None - - :param col_offset: The column that this node appears on in the - source code. - :type col_offset: int or None - - :param parent: The parent node in the syntax tree. - :type parent: NodeNG or None - - :param end_lineno: The last line this node appears on in the source code. - :type end_lineno: Optional[int] - - :param end_col_offset: The end column this node appears on in the - source code. Note: This is after the last symbol. - :type end_col_offset: Optional[int] - """ + end_lineno: int | None, + end_col_offset: int | None, + ) -> None: self.name = name - """The name of the function. + """The name of the function.""" - :type name: str or None - """ - - self._doc = doc - """The function docstring.""" - - self.doc_node: Const | None = None - """The doc node associated with this node.""" + self._doc = None + """DEPRECATED: The function docstring.""" self.locals = {} """A map of the name of a local variable to the node defining it.""" - self.args: Arguments - """The arguments that the function takes.""" - - self.body = [] - """The contents of the function body. - - :type: list(NodeNG) - """ + self.body: list[NodeNG] = [] + """The contents of the function body.""" self.instance_attrs: dict[str, list[NodeNG]] = {} @@ -1340,14 +1310,14 @@ class FunctionDef( end_col_offset=end_col_offset, parent=parent, ) - if parent: + if parent and not isinstance(parent, Unknown): frame = parent.frame(future=True) frame.set_local(name, self) def postinit( self, args: Arguments, - body, + body: list[NodeNG], decorators: node_classes.Decorators | None = None, returns=None, type_comment_returns=None, @@ -1361,11 +1331,9 @@ class FunctionDef( :param args: The arguments that the function takes. :param body: The contents of the function body. - :type body: list(NodeNG) :param decorators: The decorators that are applied to this method or function. - :type decorators: Decorators or None :params type_comment_returns: The return type annotation passed via a type comment. :params type_comment_args: diff --git a/astroid/objects.py b/astroid/objects.py index 8d1a241b..32fda287 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -278,8 +278,15 @@ class PartialFunction(scoped_nodes.FunctionDef): def __init__( self, call, name=None, doc=None, lineno=None, col_offset=None, parent=None ): - # TODO: Pass end_lineno and end_col_offset as well - super().__init__(name, lineno=lineno, col_offset=col_offset, parent=None) + # TODO: Pass end_lineno, end_col_offset and parent as well + super().__init__( + name, + lineno=lineno, + col_offset=col_offset, + parent=node_classes.Unknown(), + end_col_offset=0, + end_lineno=0, + ) # Assigned directly to prevent triggering the DeprecationWarning. self._doc = doc # A typical FunctionDef automatically adds its name to the parent scope, @@ -330,7 +337,14 @@ class Property(scoped_nodes.FunctionDef): self, function, name=None, doc=None, lineno=None, col_offset=None, parent=None ): self.function = function - super().__init__(name, lineno=lineno, col_offset=col_offset, parent=parent) + super().__init__( + name, + lineno=lineno, + col_offset=col_offset, + parent=parent, + end_col_offset=function.end_col_offset, + end_lineno=function.end_lineno, + ) # Assigned directly to prevent triggering the DeprecationWarning. self._doc = doc diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 637cf6f4..f0c4a319 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -131,7 +131,14 @@ def build_function( ) -> nodes.FunctionDef: """create and initialize an astroid FunctionDef node""" # first argument is now a list of decorators - func = nodes.FunctionDef(name) + func = nodes.FunctionDef( + name, + lineno=0, + col_offset=0, + parent=node_classes.Unknown(), + end_col_offset=0, + end_lineno=0, + ) argsnode = nodes.Arguments(parent=func, vararg=None, kwarg=None) # If args is None we don't have any information about the signature diff --git a/tests/test_scoped_nodes.py b/tests/test_scoped_nodes.py index d3a890ab..3795df56 100644 --- a/tests/test_scoped_nodes.py +++ b/tests/test_scoped_nodes.py @@ -2915,7 +2915,14 @@ def test_deprecation_of_doc_attribute() -> None: node_class = nodes.ClassDef(name="MyClass") node_class.postinit(bases=[], body=[], decorators=[], doc_node=doc_node) assert node_class.doc_node == doc_node - node_func = nodes.FunctionDef(name="MyFunction") + node_func = nodes.FunctionDef( + name="MyFunction", + lineno=0, + col_offset=0, + parent=node_module, + end_lineno=0, + end_col_offset=0, + ) node_func.postinit( args=nodes.Arguments(parent=node_func, vararg=None, kwarg=None), body=[], @@ -2940,5 +2947,12 @@ def test_deprecation_of_doc_attribute() -> None: with pytest.warns(DeprecationWarning) as records: node_module = nodes.Module(name="MyModule", doc="Docstring") node_class = nodes.ClassDef(name="MyClass", doc="Docstring") - node_func = nodes.FunctionDef(name="MyFunction", doc="Docstring") - assert len(records) == 3 + node_func = nodes.FunctionDef( + name="MyFunction", + lineno=0, + col_offset=0, + parent=node_module, + end_lineno=0, + end_col_offset=0, + ) + assert len(records) == 2 |
