diff options
| author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2022-03-06 23:54:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-06 23:54:15 +0100 |
| commit | 40e1a64ad67d4613b0bef461b0227653c8f085a1 (patch) | |
| tree | 4eb64539b53614c6c5858dfea5499590b3b68d29 /tests | |
| parent | 393b27660f63c5c93d6e3bdb23da3534065564b2 (diff) | |
| download | astroid-git-40e1a64ad67d4613b0bef461b0227653c8f085a1.tar.gz | |
Deprecate accessing and setting the ``doc`` attribute on nodes (#1434)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unittest_builder.py | 24 | ||||
| -rw-r--r-- | tests/unittest_nodes.py | 8 | ||||
| -rw-r--r-- | tests/unittest_raw_building.py | 11 | ||||
| -rw-r--r-- | tests/unittest_scoped_nodes.py | 90 |
4 files changed, 123 insertions, 10 deletions
diff --git a/tests/unittest_builder.py b/tests/unittest_builder.py index 9a3e636e..3a80ad98 100644 --- a/tests/unittest_builder.py +++ b/tests/unittest_builder.py @@ -769,7 +769,11 @@ class FileBuildTest(unittest.TestCase): """test base properties and method of an astroid module""" module = self.module self.assertEqual(module.name, "data.module") - self.assertEqual(module.doc, "test module for astroid\n") + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(module.doc, "test module for astroid\n") + assert len(records) == 1 + assert isinstance(module.doc_node, nodes.Const) + self.assertEqual(module.doc_node.value, "test module for astroid\n") self.assertEqual(module.fromlineno, 0) self.assertIsNone(module.parent) self.assertEqual(module.frame(), module) @@ -811,7 +815,11 @@ class FileBuildTest(unittest.TestCase): module = self.module function = module["global_access"] self.assertEqual(function.name, "global_access") - self.assertEqual(function.doc, "function test") + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(function.doc, "function test") + assert len(records) + assert isinstance(function.doc_node, nodes.Const) + self.assertEqual(function.doc_node.value, "function test") self.assertEqual(function.fromlineno, 11) self.assertTrue(function.parent) self.assertEqual(function.frame(), function) @@ -834,7 +842,11 @@ class FileBuildTest(unittest.TestCase): module = self.module klass = module["YO"] self.assertEqual(klass.name, "YO") - self.assertEqual(klass.doc, "hehe\n haha") + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(klass.doc, "hehe\n haha") + assert len(records) == 1 + assert isinstance(klass.doc_node, nodes.Const) + self.assertEqual(klass.doc_node.value, "hehe\n haha") self.assertEqual(klass.fromlineno, 25) self.assertTrue(klass.parent) self.assertEqual(klass.frame(), klass) @@ -888,7 +900,11 @@ class FileBuildTest(unittest.TestCase): method = klass2["method"] self.assertEqual(method.name, "method") self.assertEqual([n.name for n in method.args.args], ["self"]) - self.assertEqual(method.doc, "method\n test") + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(method.doc, "method\n test") + assert len(records) == 1 + assert isinstance(method.doc_node, nodes.Const) + self.assertEqual(method.doc_node.value, "method\n test") self.assertEqual(method.fromlineno, 48) self.assertEqual(method.type, "method") # class method diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index f07fd857..36fe963f 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -1605,7 +1605,9 @@ def test_get_doc() -> None: """ ) node: nodes.FunctionDef = astroid.extract_node(code) # type: ignore[assignment] - assert node.doc == "Docstring" + with pytest.warns(DeprecationWarning) as records: + assert node.doc == "Docstring" + assert len(records) == 1 assert isinstance(node.doc_node, nodes.Const) assert node.doc_node.value == "Docstring" assert node.doc_node.lineno == 2 @@ -1621,7 +1623,9 @@ def test_get_doc() -> None: """ ) node = astroid.extract_node(code) - assert node.doc is None + with pytest.warns(DeprecationWarning) as records: + assert node.doc is None + assert len(records) == 1 assert node.doc_node is None diff --git a/tests/unittest_raw_building.py b/tests/unittest_raw_building.py index dececbcb..3a5fbf85 100644 --- a/tests/unittest_raw_building.py +++ b/tests/unittest_raw_building.py @@ -17,6 +17,7 @@ import platform import unittest import _io +import pytest from astroid.builder import AstroidBuilder from astroid.raw_building import ( @@ -44,12 +45,18 @@ class RawBuildingTC(unittest.TestCase): def test_build_class(self) -> None: node = build_class("MyClass") self.assertEqual(node.name, "MyClass") - self.assertEqual(node.doc, None) + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(node.doc, None) + assert len(records) == 1 + self.assertEqual(node.doc_node, None) def test_build_function(self) -> None: node = build_function("MyFunction") self.assertEqual(node.name, "MyFunction") - self.assertEqual(node.doc, None) + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(node.doc, None) + assert len(records) == 1 + self.assertEqual(node.doc_node, None) def test_build_function_args(self) -> None: args = ["myArgs1", "myArgs2"] diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 25118662..34741fe2 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -41,12 +41,22 @@ import os import sys import textwrap import unittest +import warnings from functools import partial from typing import Any, List, Union import pytest -from astroid import MANAGER, builder, nodes, objects, parse, test_utils, util +from astroid import ( + MANAGER, + builder, + extract_node, + nodes, + objects, + parse, + test_utils, + util, +) from astroid.bases import BoundMethod, Generator, Instance, UnboundMethod from astroid.const import PY38_PLUS, PY310_PLUS, WIN32 from astroid.exceptions import ( @@ -956,7 +966,12 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): self.assertEqual( len(cls.getattr("__doc__")), 1, (cls, cls.getattr("__doc__")) ) - self.assertEqual(cls.getattr("__doc__")[0].value, cls.doc) + with pytest.warns(DeprecationWarning) as records: + self.assertEqual(cls.getattr("__doc__")[0].value, cls.doc) + # TODO: This should be 1 once we update the use of 'doc' internally + assert len(records) == 2 + # TODO: The doc_node attribute needs to be set in the raw_building process + # self.assertEqual(cls.getattr("__doc__")[0].value, cls.doc_node.value) self.assertEqual(len(cls.getattr("__module__")), 4) self.assertEqual(len(cls.getattr("__dict__")), 1) self.assertEqual(len(cls.getattr("__mro__")), 1) @@ -2637,5 +2652,76 @@ class TestFrameNodes: assert module.body[1].value.locals["x"][0].frame(future=True) == module +def test_deprecation_of_doc_attribute() -> None: + code = textwrap.dedent( + """\ + def func(): + "Docstring" + return 1 + """ + ) + node: nodes.FunctionDef = extract_node(code) # type: ignore[assignment] + with pytest.warns(DeprecationWarning) as records: + assert node.doc == "Docstring" + assert len(records) == 1 + with pytest.warns(DeprecationWarning) as records: + node.doc = None + assert len(records) == 1 + + code = textwrap.dedent( + """\ + class MyClass(): + '''Docstring''' + """ + ) + node: nodes.ClassDef = extract_node(code) # type: ignore[assignment] + with pytest.warns(DeprecationWarning) as records: + assert node.doc == "Docstring" + assert len(records) == 1 + with pytest.warns(DeprecationWarning) as records: + node.doc = None + assert len(records) == 1 + + code = textwrap.dedent( + """\ + '''Docstring''' + """ + ) + node = parse(code) + with pytest.warns(DeprecationWarning) as records: + assert node.doc == "Docstring" + assert len(records) == 1 + with pytest.warns(DeprecationWarning) as records: + node.doc = None + assert len(records) == 1 + + # If 'doc' isn't passed to Module, ClassDef, FunctionDef, + # no DeprecationWarning should be raised + doc_node = nodes.Const("Docstring") + with warnings.catch_warnings(): + # Modify warnings filter to raise error for DeprecationWarning + warnings.simplefilter("error", DeprecationWarning) + node_module = nodes.Module(name="MyModule") + node_module.postinit(body=[], doc_node=doc_node) + assert node_module.doc_node == doc_node + 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.postinit(args=nodes.Arguments(), body=[], doc_node=doc_node) + assert node_func.doc_node == doc_node + + # Test 'doc' attribute if only 'doc_node' is passed + with pytest.warns(DeprecationWarning) as records: + assert node_module.doc == "Docstring" + assert len(records) == 1 + with pytest.warns(DeprecationWarning) as records: + assert node_class.doc == "Docstring" + assert len(records) == 1 + with pytest.warns(DeprecationWarning) as records: + assert node_func.doc == "Docstring" + assert len(records) == 1 + + if __name__ == "__main__": unittest.main() |
